| [case testUnannotatedFunction] | |
| # flags: --disallow-untyped-defs | |
| def f(x): pass | |
| [out] | |
| main:2: error: Function is missing a type annotation | |
| [case testUnannotatedArgument] | |
| # flags: --disallow-untyped-defs | |
| def f(x) -> int: pass | |
| [out] | |
| main:2: error: Function is missing a type annotation for one or more arguments | |
| [case testNoArgumentFunction] | |
| # flags: --disallow-untyped-defs | |
| def f() -> int: pass | |
| [out] | |
| [case testUnannotatedReturn] | |
| # flags: --disallow-untyped-defs | |
| def f(x: int): pass | |
| [out] | |
| main:2: error: Function is missing a return type annotation | |
| [case testUnannotatedReturnWithFastParser] | |
| # flags: --disallow-untyped-defs | |
| def f(x: int): pass | |
| [out] | |
| main:2: error: Function is missing a return type annotation | |
| [case testLambda] | |
| # flags: --disallow-untyped-defs | |
| lambda x: x | |
| [out] | |
| [case testUntypedDef] | |
| # flags: --disallow-untyped-defs | |
| def f(): | |
| 1 + "str" | |
| [out] | |
| main:2: error: Function is missing a return type annotation | |
| main:2: note: Use "-> None" if function does not return a value | |
| [case testUnannotatedReturnWithOnlySelfArgument] | |
| # flags: --disallow-untyped-defs | |
| def f(self): pass | |
| [out] | |
| main:2: error: Function is missing a return type annotation | |
| main:2: note: Use "-> None" if function does not return a value | |
| [case testUnannotatedReturnWithNontrivialReturn] | |
| # flags: --disallow-untyped-defs | |
| def f(): return 1 | |
| [out] | |
| main:2: error: Function is missing a return type annotation | |
| [case testUntypedAsyncDef] | |
| # flags: --disallow-untyped-defs | |
| async def f(): # E: Function is missing a return type annotation \ | |
| # N: Use "-> None" if function does not return a value | |
| pass | |
| [builtins fixtures/async_await.pyi] | |
| [typing fixtures/typing-full.pyi] | |
| [case testAsyncUnannotatedArgument] | |
| # flags: --disallow-untyped-defs | |
| async def f(x) -> None: # E: Function is missing a type annotation for one or more arguments | |
| pass | |
| [builtins fixtures/async_await.pyi] | |
| [typing fixtures/typing-full.pyi] | |
| [case testAsyncUnannotatedReturn] | |
| # flags: --disallow-untyped-defs | |
| from typing import Any | |
| async def f(x: int): # E: Function is missing a return type annotation | |
| pass | |
| # Make sure explicit Any is allowed. | |
| async def g(x: int) -> Any: | |
| pass | |
| [builtins fixtures/async_await.pyi] | |
| [typing fixtures/typing-full.pyi] | |
| [case testDisallowUntypedDefsUntypedDecorator] | |
| # flags: --disallow-untyped-decorators | |
| def d(p): | |
| return p | |
| @d # E: Untyped decorator makes function "f" untyped | |
| def f(i: int) -> int: | |
| return i | |
| [case testDisallowUntypedDecoratorsUnresolvedDecorator] | |
| # flags: --disallow-untyped-decorators --ignore-missing-imports | |
| from nonexistent import d | |
| @d # E: Untyped decorator makes function "f" untyped | |
| def f(i: int) -> int: | |
| return i | |
| [case testDisallowUntypedDecoratorUntypedDef] | |
| # flags: --disallow-untyped-decorators | |
| def d(p): | |
| return p | |
| @d # no error | |
| def f(): pass | |
| [case testDisallowUntypedDecoratorsPartialFunction] | |
| # flags: --disallow-untyped-decorators | |
| def d(p): | |
| return p | |
| @d # E: Untyped decorator makes function "f" untyped | |
| def f(x) -> None: pass | |
| @d # E: Untyped decorator makes function "g" untyped | |
| def g(x, y: int): pass | |
| @d # E: Untyped decorator makes function "h" untyped | |
| def h(x: int): pass | |
| [case testDisallowUntypedDecoratorsImpreciseDecorator] | |
| # flags: --disallow-untyped-decorators | |
| from typing import Any | |
| def d(p) -> Any: | |
| return p | |
| @d # no error | |
| def f() -> None: pass | |
| [case testDisallowUntypedDecoratorsMultipleDecorators] | |
| # flags: --disallow-untyped-decorators | |
| from typing import Any | |
| def d1(p): | |
| return p | |
| def d2(p): | |
| return p | |
| def d3(p) -> Any: | |
| return p | |
| @d1 # E: Untyped decorator makes function "f" untyped | |
| @d2 # E: Untyped decorator makes function "f" untyped | |
| @d3 # no error | |
| @d1 # E: Untyped decorator makes function "f" untyped | |
| def f() -> None: pass | |
| [case testDisallowUntypedDecoratorsCallableInstance] | |
| # flags: --disallow-untyped-decorators | |
| from typing import Callable | |
| class TypedDecorator: | |
| def __call__(self, c: Callable) -> Callable: | |
| return function | |
| class UntypedDecorator: | |
| def __call__(self, c): | |
| return function | |
| @TypedDecorator() | |
| def f() -> None: pass | |
| @UntypedDecorator() # E: Untyped decorator makes function "g" untyped | |
| def g() -> None: pass | |
| @TypedDecorator() | |
| @UntypedDecorator() # E: Untyped decorator makes function "h" untyped | |
| def h() -> None: pass | |
| @UntypedDecorator() # E: Untyped decorator makes function "i" untyped | |
| @TypedDecorator() | |
| def i() -> None: pass | |
| reveal_type(f) # N: Revealed type is 'def (*Any, **Any) -> Any' | |
| reveal_type(g) # N: Revealed type is 'Any' | |
| reveal_type(h) # N: Revealed type is 'def (*Any, **Any) -> Any' | |
| reveal_type(i) # N: Revealed type is 'Any' | |
| [case testDisallowUntypedDecoratorsNonCallableInstance] | |
| # flags: --disallow-untyped-decorators | |
| class Decorator: | |
| pass | |
| @Decorator() # E: "Decorator" not callable | |
| def f() -> None: pass | |
| [case testSubclassingAny] | |
| # flags: --disallow-subclassing-any | |
| from typing import Any | |
| FakeClass = None # type: Any | |
| class Foo(FakeClass): pass # E: Class cannot subclass 'FakeClass' (has type 'Any') | |
| [out] | |
| [case testSubclassingAnyMultipleBaseClasses] | |
| # flags: --disallow-subclassing-any | |
| from typing import Any | |
| FakeClass = None # type: Any | |
| class ActualClass: pass | |
| class Foo(ActualClass, FakeClass): pass # E: Class cannot subclass 'FakeClass' (has type 'Any') | |
| [out] | |
| [case testSubclassingAnySilentImports] | |
| # flags: --disallow-subclassing-any --follow-imports=skip | |
| # cmd: mypy -m main | |
| [file main.py] | |
| from ignored_module import BaseClass | |
| class Foo(BaseClass): pass | |
| [file ignored_module.py] | |
| class BaseClass: pass | |
| [out] | |
| tmp/main.py:2: error: Class cannot subclass 'BaseClass' (has type 'Any') | |
| [case testSubclassingAnySilentImports2] | |
| # flags: --disallow-subclassing-any --follow-imports=skip | |
| # cmd: mypy -m main | |
| [file main.py] | |
| import ignored_module | |
| class Foo(ignored_module.BaseClass): pass | |
| [file ignored_module.py] | |
| class BaseClass: pass | |
| [out] | |
| tmp/main.py:2: error: Class cannot subclass 'BaseClass' (has type 'Any') | |
| [case testWarnNoReturnIgnoresTrivialFunctions] | |
| # flags: --warn-no-return | |
| def f() -> int: | |
| pass | |
| def g() -> int: | |
| ... | |
| def h() -> int: | |
| """with docstring""" | |
| pass | |
| def i() -> int: | |
| """with docstring""" | |
| ... | |
| def j() -> int: | |
| u"""with unicode docstring""" | |
| pass | |
| def k() -> int: | |
| """docstring only""" | |
| [case testWarnNoReturnWorksWithAlwaysTrue] | |
| # flags: --warn-no-return | |
| PY3 = True | |
| def f() -> int: | |
| if PY3: | |
| return 0 | |
| else: | |
| return 0 | |
| [builtins fixtures/bool.pyi] | |
| [case testWarnNoReturnWorksWithAlwaysFalse] | |
| # flags: --warn-no-return | |
| PY2 = False | |
| def f() -> int: | |
| if PY2: | |
| return 0 | |
| else: | |
| return 0 | |
| [builtins fixtures/bool.pyi] | |
| [case testWarnNoReturnWorksWithMypyTrue] | |
| # flags: --warn-no-return | |
| MYPY = False | |
| def f() -> int: | |
| if MYPY: | |
| return 0 | |
| else: | |
| return 0 | |
| [builtins fixtures/bool.pyi] | |
| [case testNoReturnDisallowsReturn] | |
| # flags: --warn-no-return | |
| from mypy_extensions import NoReturn | |
| def f() -> NoReturn: | |
| if bool(): | |
| return 5 # E: Return statement in function which does not return | |
| else: | |
| return # E: Return statement in function which does not return | |
| [builtins fixtures/dict.pyi] | |
| [case testNoReturnWithoutImplicitReturn] | |
| # flags: --warn-no-return | |
| from mypy_extensions import NoReturn | |
| def no_return() -> NoReturn: pass | |
| def f() -> NoReturn: | |
| no_return() | |
| [builtins fixtures/dict.pyi] | |
| [case testNoReturnDisallowsImplicitReturn] | |
| # flags: --warn-no-return | |
| from mypy_extensions import NoReturn | |
| def f() -> NoReturn: # E: Implicit return in function which does not return | |
| non_trivial_function = 1 | |
| [builtins fixtures/dict.pyi] | |
| [case testNoReturnNoWarnNoReturn] | |
| # flags: --warn-no-return | |
| from mypy_extensions import NoReturn | |
| def no_return() -> NoReturn: pass | |
| def f() -> int: | |
| if bool(): | |
| return 0 | |
| else: | |
| no_return() | |
| [builtins fixtures/dict.pyi] | |
| [case testNoReturnInExpr] | |
| # flags: --warn-no-return | |
| from mypy_extensions import NoReturn | |
| def no_return() -> NoReturn: pass | |
| def f() -> int: | |
| return 0 | |
| reveal_type(f() or no_return()) # N: Revealed type is 'builtins.int' | |
| [builtins fixtures/dict.pyi] | |
| [case testNoReturnVariable] | |
| # flags: --warn-no-return | |
| from mypy_extensions import NoReturn | |
| x = 0 # type: NoReturn # E: Incompatible types in assignment (expression has type "int", variable has type "NoReturn") | |
| [builtins fixtures/dict.pyi] | |
| [case testNoReturnImportFromTyping] | |
| from typing import NoReturn | |
| def h() -> NoReturn: | |
| if bool(): | |
| return 5 # E: Return statement in function which does not return | |
| else: | |
| return # E: Return statement in function which does not return | |
| def no_return() -> NoReturn: pass | |
| def f() -> NoReturn: | |
| no_return() | |
| x: NoReturn = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "NoReturn") | |
| [builtins fixtures/dict.pyi] | |
| [case testShowErrorContextFunction] | |
| # flags: --show-error-context | |
| def f() -> None: | |
| 0 + "" | |
| [out] | |
| main: note: In function "f": | |
| main:3: error: Unsupported operand types for + ("int" and "str") | |
| [case testShowErrorContextClass] | |
| # flags: --show-error-context | |
| class A: | |
| 0 + "" | |
| [out] | |
| main: note: In class "A": | |
| main:3: error: Unsupported operand types for + ("int" and "str") | |
| [case testShowErrorContextMember] | |
| # flags: --show-error-context | |
| class A: | |
| def f(self, x: int) -> None: | |
| self.f("") | |
| [out] | |
| main: note: In member "f" of class "A": | |
| main:4: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" | |
| [case testShowErrorContextModule] | |
| # flags: --show-error-context | |
| import m | |
| [file m.py] | |
| 0 + "" | |
| [out] | |
| main:2: note: In module imported here: | |
| tmp/m.py:1: error: Unsupported operand types for + ("int" and "str") | |
| [case testShowErrorContextTopLevel] | |
| # flags: --show-error-context | |
| def f() -> None: | |
| 0 + "" | |
| 0 + "" | |
| [out] | |
| main: note: In function "f": | |
| main:3: error: Unsupported operand types for + ("int" and "str") | |
| main: note: At top level: | |
| main:4: error: Unsupported operand types for + ("int" and "str") | |
| [case testShowErrorContextFromHere] | |
| # flags: --show-error-context | |
| import a | |
| [file a.py] | |
| import b | |
| [file b.py] | |
| 0 + "" | |
| [out] | |
| tmp/a.py:1: note: In module imported here, | |
| main:2: note: ... from here: | |
| tmp/b.py:1: error: Unsupported operand types for + ("int" and "str") | |
| [case testFollowImportsNormal] | |
| # flags: --follow-imports=normal | |
| from mod import x | |
| x + "" | |
| [file mod.py] | |
| 1 + "" | |
| x = 0 | |
| [out] | |
| tmp/mod.py:1: error: Unsupported operand types for + ("int" and "str") | |
| main:3: error: Unsupported operand types for + ("int" and "str") | |
| [case testFollowImportsSilent] | |
| # flags: --follow-imports=silent | |
| from mod import x | |
| x + "" # E: Unsupported operand types for + ("int" and "str") | |
| [file mod.py] | |
| 1 + "" | |
| x = 0 | |
| [case testFollowImportsSilentTypeIgnore] | |
| # flags: --warn-unused-ignores --follow-imports=silent | |
| import mod | |
| [file mod.py] | |
| x = 3 # type: ignore | |
| [case testFollowImportsSkip] | |
| # flags: --follow-imports=skip | |
| from mod import x | |
| x + "" | |
| [file mod.py] | |
| this deliberate syntax error will not be reported | |
| [out] | |
| [case testFollowImportsError] | |
| # flags: --follow-imports=error | |
| from mod import x | |
| x + "" | |
| [file mod.py] | |
| deliberate syntax error | |
| [out] | |
| main:2: error: Import of 'mod' ignored | |
| main:2: note: (Using --follow-imports=error, module not passed on command line) | |
| [case testIgnoreMissingImportsFalse] | |
| from mod import x | |
| [out] | |
| main:1: error: Cannot find module named 'mod' | |
| main:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports | |
| [case testIgnoreMissingImportsTrue] | |
| # flags: --ignore-missing-imports | |
| from mod import x | |
| [out] | |
| [case testPerFileIncompleteDefsBasic] | |
| # flags: --config-file tmp/mypy.ini | |
| import standard, incomplete | |
| [file standard.py] | |
| def incomplete(x) -> int: | |
| return 0 | |
| [file incomplete.py] | |
| def incomplete(x) -> int: # E: Function is missing a type annotation for one or more arguments | |
| return 0 | |
| [file mypy.ini] | |
| \[mypy] | |
| disallow_incomplete_defs = False | |
| \[mypy-incomplete] | |
| disallow_incomplete_defs = True | |
| [case testPerFileStrictOptionalBasic] | |
| # flags: --config-file tmp/mypy.ini | |
| import standard, optional | |
| [file standard.py] | |
| x = 0 | |
| if int(): | |
| x = None | |
| [file optional.py] | |
| x = 0 | |
| if int(): | |
| x = None # E: Incompatible types in assignment (expression has type "None", variable has type "int") | |
| [file mypy.ini] | |
| \[mypy] | |
| strict_optional = False | |
| \[mypy-optional] | |
| strict_optional = True | |
| [case testPerFileStrictOptionalBasicImportStandard] | |
| # flags: --config-file tmp/mypy.ini | |
| import standard, optional | |
| [file standard.py] | |
| from typing import Optional | |
| def f(x: int) -> None: pass | |
| an_int = 0 # type: int | |
| optional_int = None # type: Optional[int] | |
| f(an_int) # ints can be used as ints | |
| f(optional_int) # optional ints can be used as ints in this file | |
| [file optional.py] | |
| import standard | |
| def f(x: int) -> None: pass | |
| standard.an_int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int") | |
| standard.optional_int = None # OK -- explicitly declared as optional | |
| f(standard.an_int) # ints can be used as ints | |
| f(standard.optional_int) # E: Argument 1 to "f" has incompatible type "None"; expected "int" | |
| [file mypy.ini] | |
| \[mypy] | |
| strict_optional = False | |
| \[mypy-optional] | |
| strict_optional = True | |
| [case testPerFileStrictOptionalBasicImportOptional] | |
| # flags: --config-file tmp/mypy.ini | |
| import standard, optional | |
| [file standard.py] | |
| import optional | |
| def f(x: int) -> None: pass | |
| f(optional.x) # OK -- in non-strict Optional context | |
| f(optional.y) # OK -- in non-strict Optional context | |
| [file optional.py] | |
| from typing import Optional | |
| def f(x: int) -> None: pass | |
| x = 0 # type: Optional[int] | |
| y = None # type: None | |
| [file mypy.ini] | |
| \[mypy] | |
| strict_optional = False | |
| \[mypy-optional] | |
| strict_optional = True | |
| [case testPerFileStrictOptionalListItemImportOptional] | |
| # flags: --config-file tmp/mypy.ini | |
| import standard, optional | |
| [file standard.py] | |
| import optional | |
| from typing import List | |
| def f(x: List[int]) -> None: pass | |
| f(optional.x) # OK -- in non-strict Optional context | |
| f(optional.y) # OK -- in non-strict Optional context | |
| [file optional.py] | |
| from typing import Optional, List | |
| def f(x: List[int]) -> None: pass | |
| x = [] # type: List[Optional[int]] | |
| y = [] # type: List[int] | |
| [file mypy.ini] | |
| \[mypy] | |
| strict_optional = False | |
| \[mypy-optional] | |
| strict_optional = True | |
| [builtins fixtures/list.pyi] | |
| [case testPerFileStrictOptionalComplicatedList] | |
| from typing import Union, Optional, List | |
| def f() -> None: | |
| x = [] # type: Union[List[Optional[str]], str] | |
| [builtins fixtures/list.pyi] | |
| [case testPerFileStrictOptionalNoneArguments] | |
| # flags: --config-file tmp/mypy.ini | |
| import standard, optional | |
| [file standard.py] | |
| def f(x: int = None) -> None: pass | |
| [file optional.py] | |
| import standard | |
| def f(x: int = None) -> None: pass | |
| standard.f(None) | |
| [file mypy.ini] | |
| \[mypy] | |
| strict_optional = False | |
| \[mypy-optional] | |
| strict_optional = True | |
| [case testDisallowImplicitTypesIgnoreMissingTypes] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from missing import MyType | |
| def f(x: MyType) -> None: # E: Argument 1 to "f" becomes "Any" due to an unfollowed import | |
| pass | |
| [case testDisallowImplicitTypes] | |
| # flags: --disallow-any-unimported | |
| from missing import MyType | |
| def f(x: MyType) -> None: | |
| pass | |
| [out] | |
| main:2: error: Cannot find module named 'missing' | |
| main:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports | |
| main:4: error: Argument 1 to "f" becomes "Any" due to an unfollowed import | |
| [case testDisallowImplicitAnyVariableDefinition] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from missing import Unchecked | |
| t: Unchecked = 12 # E: Type of variable becomes "Any" due to an unfollowed import | |
| [case testDisallowImplicitAnyGeneric] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from missing import Unchecked | |
| from typing import List | |
| def foo(l: List[Unchecked]) -> List[Unchecked]: | |
| t = [] # type: List[Unchecked] | |
| return l | |
| [builtins fixtures/list.pyi] | |
| [out] | |
| main:5: error: Return type becomes "List[Any]" due to an unfollowed import | |
| main:5: error: Argument 1 to "foo" becomes "List[Any]" due to an unfollowed import | |
| main:6: error: Type of variable becomes "List[Any]" due to an unfollowed import | |
| [case testDisallowImplicitAnyInherit] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from missing import Unchecked | |
| from typing import List | |
| class C(Unchecked): # E: Base type Unchecked becomes "Any" due to an unfollowed import | |
| pass | |
| class A(List[Unchecked]): # E: Base type becomes "List[Any]" due to an unfollowed import | |
| pass | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowImplicitAnyAlias] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from missing import Unchecked | |
| from typing import List | |
| X = List[Unchecked] | |
| def f(x: X) -> None: # E: Argument 1 to "f" becomes "List[Any]" due to an unfollowed import | |
| pass | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowImplicitAnyCast] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from missing import Unchecked | |
| from typing import List, cast | |
| foo = [1, 2, 3] | |
| cast(List[Unchecked], foo) # E: Target type of cast becomes "List[Any]" due to an unfollowed import | |
| cast(Unchecked, foo) # E: Target type of cast becomes "Any" due to an unfollowed import | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowImplicitAnyNamedTuple] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from typing import List, NamedTuple | |
| from missing import Unchecked | |
| Point = NamedTuple('Point', [('x', List[Unchecked]), | |
| ('y', Unchecked)]) | |
| [builtins fixtures/list.pyi] | |
| [out] | |
| main:5: error: NamedTuple type becomes "Tuple[List[Any], Any]" due to an unfollowed import | |
| [case testDisallowImplicitAnyTypeVarConstraints] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from typing import List, NamedTuple, TypeVar, Any | |
| from missing import Unchecked | |
| T = TypeVar('T', Unchecked, List[Unchecked], str) | |
| [builtins fixtures/list.pyi] | |
| [out] | |
| main:5: error: Constraint 1 becomes "Any" due to an unfollowed import | |
| main:5: error: Constraint 2 becomes "List[Any]" due to an unfollowed import | |
| [case testDisallowImplicitAnyNewType] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from typing import NewType, List | |
| from missing import Unchecked | |
| Baz = NewType('Baz', Unchecked) # E: Argument 2 to NewType(...) must be subclassable (got "Any") | |
| Bar = NewType('Bar', List[Unchecked]) # E: Argument 2 to NewType(...) becomes "List[Any]" due to an unfollowed import | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowImplicitAnyCallableAndTuple] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from typing import Callable, Tuple | |
| from missing import Unchecked | |
| def foo(f: Callable[[], Unchecked]) -> Tuple[Unchecked]: | |
| return f() | |
| [builtins fixtures/list.pyi] | |
| [out] | |
| main:5: error: Return type becomes "Tuple[Any]" due to an unfollowed import | |
| main:5: error: Argument 1 to "foo" becomes "Callable[[], Any]" due to an unfollowed import | |
| [case testDisallowImplicitAnySubclassingExplicitAny] | |
| # flags: --ignore-missing-imports --disallow-any-unimported --disallow-subclassing-any | |
| from typing import Any | |
| class C(Any): # E: Class cannot subclass 'Any' (has type 'Any') | |
| pass | |
| [case testDisallowImplicitAnyVarDeclaration] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from missing import Unchecked | |
| foo: Unchecked = "" | |
| foo = "" | |
| x, y = 1, 2 # type: Unchecked, Unchecked | |
| [out] | |
| main:4: error: Type of variable becomes "Any" due to an unfollowed import | |
| main:6: error: A type on this line becomes "Any" due to an unfollowed import | |
| [case testDisallowUnimportedAnyTypedDictSimple] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from mypy_extensions import TypedDict | |
| from x import Unchecked | |
| M = TypedDict('M', {'x': str, 'y': Unchecked}) # E: Type of a TypedDict key becomes "Any" due to an unfollowed import | |
| def f(m: M) -> M: pass # no error | |
| [builtins fixtures/dict.pyi] | |
| [case testDisallowUnimportedAnyTypedDictGeneric] | |
| # flags: --ignore-missing-imports --disallow-any-unimported | |
| from mypy_extensions import TypedDict | |
| from typing import List | |
| from x import Unchecked | |
| M = TypedDict('M', {'x': str, 'y': List[Unchecked]}) # E: Type of a TypedDict key becomes "List[Any]" due to an unfollowed import | |
| def f(m: M) -> M: pass # no error | |
| [builtins fixtures/dict.pyi] | |
| [case testDisallowAnyDecoratedUnannotatedDecorator] | |
| # flags: --disallow-any-decorated | |
| from typing import Any | |
| def d(f): | |
| return f | |
| @d | |
| def f(x: Any) -> Any: # E: Function is untyped after decorator transformation | |
| pass | |
| @d | |
| def h(x): # E: Function is untyped after decorator transformation | |
| pass | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyDecoratedErrorIsReportedOnlyOnce] | |
| # flags: --disallow-any-decorated | |
| def d(f): | |
| return f | |
| def d2(f): | |
| return f | |
| @d | |
| @d2 | |
| @d | |
| def f(x: int) -> None: pass # E: Function is untyped after decorator transformation | |
| [case testDisallowAnyDecoratedReturnAny] | |
| # flags: --disallow-any-decorated | |
| from typing import Any | |
| def d(f) -> Any: | |
| return f | |
| @d | |
| def f() -> None: pass # E: Function is untyped after decorator transformation | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyDecoratedReturnCallable] | |
| # flags: --disallow-any-decorated | |
| from typing import Any, Callable | |
| def d(f) -> Callable[..., None]: | |
| return f | |
| @d | |
| def g(i: int, s: str) -> None: pass # E: Type of decorated function contains type "Any" ("Callable[..., None]") | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyDecoratedNonexistentDecorator] | |
| # flags: --disallow-any-decorated --ignore-missing-imports | |
| from nonexistent import d | |
| @d | |
| def f() -> None: pass # E: Function is untyped after decorator transformation | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyDecoratedPartlyTypedCallable] | |
| # flags: --disallow-any-decorated --ignore-missing-imports | |
| from typing import Callable, Any, List | |
| def d(f) -> Callable[[int, Any], Any]: pass | |
| def d2(f) -> Callable[[int], List[Any]]: pass | |
| def d3(f) -> Callable[[Any], List[str]]: pass | |
| @d | |
| def f(i: int, s: str) -> None: # E: Type of decorated function contains type "Any" ("Callable[[int, Any], Any]") | |
| pass | |
| @d2 | |
| def g(i: int) -> None: # E: Type of decorated function contains type "Any" ("Callable[[int], List[Any]]") | |
| pass | |
| @d3 | |
| def h(i: int) -> None: # E: Type of decorated function contains type "Any" ("Callable[[Any], List[str]]") | |
| pass | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyDecoratedReturnsCallableNoParams] | |
| # flags: --disallow-any-decorated | |
| from typing import Callable | |
| def d(p) -> Callable[[], int]: | |
| return p | |
| @d | |
| def f(i): | |
| return i | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyDecoratedDecoratorReturnsNonCallable] | |
| # flags: --disallow-any-decorated | |
| def d(p) -> int: | |
| return p(0) | |
| @d | |
| def f(i): | |
| return i | |
| [case testDisallowAnyDecoratedUntypedUndecoratedFunction] | |
| # flags: --disallow-any-decorated | |
| from typing import Callable | |
| def f(i): # no error | |
| return i | |
| [case testDisallowAnyDecoratedTwoDecorators] | |
| # flags: --disallow-any-decorated | |
| from typing import Callable | |
| def typed_dec(f) -> Callable[[], int]: pass | |
| def untyped_dec(f): pass | |
| @typed_dec | |
| @untyped_dec | |
| def f(): # no error | |
| return i | |
| @untyped_dec | |
| @typed_dec | |
| def g(): # E: Function is untyped after decorator transformation | |
| return i | |
| [case testDisallowAnyExprSimple] | |
| # flags: --disallow-any-expr | |
| from typing import Any | |
| def f(s): | |
| yield s | |
| x = f(0) # E: Expression has type "Any" | |
| for x in f(0): # E: Expression has type "Any" | |
| g(x) # E: Expression has type "Any" | |
| def g(x) -> Any: | |
| yield x # E: Expression has type "Any" | |
| l = [1, 2, 3] | |
| l[f(0)] # E: Expression has type "Any" | |
| f(l) | |
| f(f(0)) # E: Expression has type "Any" | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprUnannotatedFunction] | |
| # flags: --disallow-any-expr | |
| def g(s): | |
| return s | |
| g(0) | |
| w: int = g(1) | |
| [case testDisallowAnyExprExplicitAnyParam] | |
| # flags: --disallow-any-expr | |
| from typing import Any, List | |
| def f(s: Any) -> None: | |
| pass | |
| def g(s: List[Any]) -> None: | |
| pass | |
| f(0) | |
| # type of list below is inferred with expected type of "List[Any]", so that becomes it's type | |
| # instead of List[str] | |
| g(['']) # E: Expression type contains "Any" (has type "List[Any]") | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprAllowsAnyInCast] | |
| # flags: --disallow-any-expr | |
| from typing import Any, cast | |
| class Foo: | |
| g: Any = 2 | |
| z = cast(int, Foo().g) | |
| m = cast(Any, Foo().g) # E: Expression has type "Any" | |
| k = Foo.g # E: Expression has type "Any" | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprAllowsAnyInVariableAssignmentWithExplicitTypeAnnotation] | |
| # flags: --disallow-any-expr | |
| from typing import Any | |
| class Foo: | |
| g: Any = 2 | |
| z: int = Foo().g | |
| x = Foo().g # type: int | |
| m: Any = Foo().g # E: Expression has type "Any" | |
| n = Foo().g # type: Any # E: Expression has type "Any" | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprGeneric] | |
| # flags: --disallow-any-expr | |
| from typing import List | |
| l: List = [] | |
| l.append(1) # E: Expression type contains "Any" (has type "List[Any]") | |
| k = l[0] # E: Expression type contains "Any" (has type "List[Any]") # E: Expression has type "Any" | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprTypeVar] | |
| # flags: --disallow-any-expr | |
| from typing import TypeVar | |
| T = TypeVar('T') # no error | |
| def f(t: T) -> T: | |
| return t | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprNamedTuple] | |
| # flags: --disallow-any-expr | |
| from typing import NamedTuple | |
| Point = NamedTuple('Point', [('x', int), ('y', int)]) # no error | |
| def origin() -> Point: | |
| return Point(x=0, y=0) | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprNewType] | |
| # flags: --disallow-any-expr | |
| from typing import NewType | |
| NT = NewType('NT', int) # no error | |
| def nt() -> NT: | |
| return NT(1) | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprEnum] | |
| # flags: --disallow-any-expr | |
| from enum import Enum | |
| E = Enum('E', '1, 2, 3') # no error | |
| def k(s: E) -> None: pass | |
| [builtins fixtures/list.pyi] | |
| [case testDisallowAnyExprTypedDict] | |
| # flags: --disallow-any-expr | |
| from mypy_extensions import TypedDict | |
| Movie = TypedDict('Movie', {'name': str, 'year': int}) | |
| def g(m: Movie) -> Movie: | |
| return m | |
| [builtins fixtures/dict.pyi] | |
| [case testDisallowIncompleteDefs] | |
| # flags: --disallow-incomplete-defs | |
| def f(i: int): # E: Function is missing a return type annotation | |
| pass | |
| def g(i) -> None: # E: Function is missing a type annotation for one or more arguments | |
| pass | |
| def h(i: int) -> int: # no error | |
| return i | |
| def i() -> None: # no error | |
| pass | |
| [case testDisallowIncompleteDefsNoReturn] | |
| # flags: --disallow-incomplete-defs --disallow-untyped-defs | |
| def f(i: int): # E: Function is missing a return type annotation | |
| pass | |
| [case testDisallowIncompleteDefsSelf] | |
| # flags: --disallow-incomplete-defs | |
| class C: | |
| def foo(self) -> None: # no error | |
| pass | |
| [case testDisallowIncompleteDefsPartiallyAnnotatedParams] | |
| # flags: --disallow-incomplete-defs | |
| def f(i: int, s): | |
| pass | |
| [out] | |
| main:3: error: Function is missing a return type annotation | |
| main:3: error: Function is missing a type annotation for one or more arguments | |
| [case testAlwaysTrueAlwaysFalseFlags] | |
| # flags: --always-true=YOLO --always-true=YOLO1 --always-false=BLAH1 --always-false BLAH --ignore-missing-imports | |
| from somewhere import YOLO, BLAH | |
| if not YOLO: | |
| 1+() | |
| if BLAH: | |
| 1+() | |
| [builtins fixtures/bool.pyi] | |
| [case testAlwaysTrueAlwaysFalseConfigFile] | |
| # flags: --config-file tmp/mypy.ini | |
| from somewhere import YOLO, BLAH | |
| if not YOLO: | |
| 1+() | |
| if BLAH: | |
| 1+() | |
| [file mypy.ini] | |
| \[mypy] | |
| ignore_missing_imports = True | |
| always_true = YOLO1, YOLO | |
| always_false = BLAH, BLAH1 | |
| [builtins fixtures/bool.pyi] | |
| [case testCheckDisallowAnyGenericsNamedTuple] | |
| # flags: --disallow-any-generics | |
| from typing import NamedTuple | |
| N = NamedTuple('N', [('x', N)]) # type: ignore | |
| n: N | |
| [out] | |
| [case testCheckDisallowAnyGenericsTypedDict] | |
| # flags: --disallow-any-generics | |
| from typing import Dict, Any, Optional | |
| from mypy_extensions import TypedDict | |
| VarsDict = Dict[str, Any] | |
| HostsDict = Dict[str, Optional[VarsDict]] | |
| GroupDataDict = TypedDict( | |
| "GroupDataDict", {"children": "GroupsDict", # type: ignore | |
| "vars": VarsDict, | |
| "hosts": HostsDict}, total=False | |
| ) | |
| GroupsDict = Dict[str, GroupDataDict] # type: ignore | |
| [builtins fixtures/dict.pyi] | |
| [case testCheckDefaultAllowAnyGeneric] | |
| from typing import TypeVar, Callable | |
| T = TypeVar('T') | |
| C = Callable[[], T] | |
| def f(c: C): | |
| pass | |
| [out] | |
| [case testCheckAllowAnyGenericAnyGeneric] | |
| # flags: --strict --allow-any-generics | |
| from typing import TypeVar, Callable | |
| T = TypeVar('T') | |
| C = Callable[[], T] | |
| def f(c: C) -> None: | |
| pass | |
| [out] | |
| [case testCheckDisallowAnyGenericsAnyGeneric] | |
| # flags: --disallow-any-generics | |
| from typing import TypeVar, Callable | |
| T = TypeVar('T') | |
| C = Callable[[], T] | |
| def f(c: C): # E: Missing type parameters for generic type "C" | |
| pass | |
| [out] | |
| [case testStrictAnyGeneric] | |
| # flags: --strict | |
| from typing import TypeVar, Generic | |
| T = TypeVar('T') | |
| class A(Generic[T]): | |
| pass | |
| def f(c: A) -> None: # E: Missing type parameters for generic type "A" | |
| pass | |
| [out] | |
| [case testStrictEqualityPerFile] | |
| # flags: --config-file tmp/mypy.ini | |
| import b | |
| 42 == 'no' # E: Non-overlapping equality check (left operand type: "Literal[42]", right operand type: "Literal['no']") | |
| [file b.py] | |
| 42 == 'no' | |
| [file mypy.ini] | |
| \[mypy] | |
| strict_equality = True | |
| \[mypy-b] | |
| strict_equality = False | |
| [builtins fixtures/bool.pyi] | |
| [case testNoImplicitReexport] | |
| # flags: --no-implicit-reexport | |
| from other_module_2 import a | |
| [file other_module_1.py] | |
| a = 5 | |
| [file other_module_2.py] | |
| from other_module_1 import a | |
| [out] | |
| main:2: error: Module 'other_module_2' has no attribute 'a' | |
| [case testNoImplicitReexportRespectsAll] | |
| # flags: --no-implicit-reexport | |
| from other_module_2 import a | |
| from other_module_2 import b | |
| [file other_module_1.py] | |
| a = 5 | |
| b = 6 | |
| [file other_module_2.py] | |
| from other_module_1 import a, b | |
| __all__ = ('b',) | |
| [out] | |
| main:2: error: Module 'other_module_2' has no attribute 'a' | |
| [case testNoImplicitReexportStarConsideredImplicit] | |
| # flags: --no-implicit-reexport | |
| from other_module_2 import a | |
| [file other_module_1.py] | |
| a = 5 | |
| [file other_module_2.py] | |
| from other_module_1 import * | |
| [out] | |
| main:2: error: Module 'other_module_2' has no attribute 'a' | |
| [case testNoImplicitReexportStarCanBeReexportedWithAll] | |
| # flags: --no-implicit-reexport | |
| from other_module_2 import a | |
| from other_module_2 import b | |
| [file other_module_1.py] | |
| a = 5 | |
| b = 6 | |
| [file other_module_2.py] | |
| from other_module_1 import * | |
| __all__ = ('b',) | |
| [out] | |
| main:2: error: Module 'other_module_2' has no attribute 'a' | |
| [case testNoImplicitReexportMypyIni] | |
| # flags: --config-file tmp/mypy.ini | |
| from other_module_2 import a | |
| [file other_module_1.py] | |
| a = 5 | |
| [file other_module_2.py] | |
| from other_module_1 import a | |
| [file mypy.ini] | |
| \[mypy] | |
| implicit_reexport = True | |
| \[mypy-other_module_2] | |
| implicit_reexport = False | |
| [out] | |
| main:2: error: Module 'other_module_2' has no attribute 'a' | |
| [case testImplicitAnyOKForNoArgs] | |
| # flags: --disallow-any-generics --show-column-numbers | |
| from typing import List | |
| A = List # OK | |
| B = List[A] # E:10: Missing type parameters for generic type "A" | |
| x: A # E:4: Missing type parameters for generic type "A" | |
| [builtins fixtures/list.pyi] |