| [case testEmptyFile] | |
| [out] | |
| [case testAssignmentAndVarDef] | |
| a: A | |
| b: B | |
| if int(): | |
| a = a | |
| if int(): | |
| a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| class A: pass | |
| class B: pass | |
| [case testConstructionAndAssignment] | |
| class A: | |
| def __init__(self): pass | |
| class B: | |
| def __init__(self): pass | |
| x: A | |
| x = A() | |
| if int(): | |
| x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| [case testInheritInitFromObject] | |
| class A(object): pass | |
| class B(object): pass | |
| x: A | |
| if int(): | |
| x = A() | |
| if int(): | |
| x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| [case testImplicitInheritInitFromObject] | |
| class A: pass | |
| class B: pass | |
| x: A | |
| o: object | |
| if int(): | |
| x = o # E: Incompatible types in assignment (expression has type "object", variable has type "A") | |
| if int(): | |
| x = A() | |
| if int(): | |
| o = x | |
| [case testTooManyConstructorArgs] | |
| import typing | |
| object(object()) | |
| [out] | |
| main:2: error: Too many arguments for "object" | |
| [case testVarDefWithInit] | |
| import typing | |
| class A: pass | |
| a = A() # type: A | |
| b = object() # type: A # E: Incompatible types in assignment (expression has type "object", variable has type "A") | |
| [case testInheritanceBasedSubtyping] | |
| import typing | |
| class A: pass | |
| class B(A): pass | |
| x = B() # type: A | |
| y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B") | |
| [case testDeclaredVariableInParentheses] | |
| (x) = 2 # type: int | |
| if int(): | |
| x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") | |
| if int(): | |
| x = 1 | |
| [case testIncompatibleAssignmentAmbiguousShortnames] | |
| class Any: pass | |
| class List: pass | |
| class Dict: pass | |
| class Iterator: pass | |
| x = Any() | |
| x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Any") | |
| y = List() | |
| y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.List") | |
| z = Dict() | |
| z = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Dict") | |
| w = Iterator() | |
| w = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Iterator") | |
| -- Simple functions and calling | |
| -- ---------------------------- | |
| [case testFunction] | |
| import typing | |
| class A: pass | |
| class B: pass | |
| def f(x: 'A') -> None: pass | |
| f(A()) | |
| f(B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A" | |
| [case testNotCallable] | |
| import typing | |
| class A: pass | |
| A()() # E: "A" not callable | |
| [case testSubtypeArgument] | |
| import typing | |
| class A: pass | |
| class B(A): pass | |
| def f(x: 'A', y: 'B') -> None: pass | |
| f(B(), A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B" | |
| f(B(), B()) | |
| [case testInvalidArgumentCount] | |
| import typing | |
| def f(x, y) -> None: pass | |
| f(object()) | |
| f(object(), object(), object()) | |
| [out] | |
| main:3: error: Missing positional argument "y" in call to "f" | |
| main:4: error: Too many arguments for "f" | |
| [case testMissingPositionalArguments] | |
| class Foo: | |
| def __init__(self, bar: int): | |
| pass | |
| c = Foo() | |
| def foo(baz: int, bas: int):pass | |
| foo() | |
| [out] | |
| main:4: error: Missing positional argument "bar" in call to "Foo" | |
| main:6: error: Missing positional arguments "baz", "bas" in call to "foo" | |
| -- Locals | |
| -- ------ | |
| [case testLocalVariables] | |
| def f() -> None: | |
| x: A | |
| y: B | |
| if int(): | |
| x = x | |
| x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| class A: pass | |
| class B: pass | |
| [case testLocalVariableScope] | |
| def f() -> None: | |
| x: A | |
| x = A() | |
| def g() -> None: | |
| x: B | |
| x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B") | |
| class A: pass | |
| class B: pass | |
| [case testFunctionArguments] | |
| import typing | |
| def f(x: 'A', y: 'B') -> None: | |
| if int(): | |
| x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| x = x | |
| y = B() | |
| class A: pass | |
| class B: pass | |
| [case testLocalVariableInitialization] | |
| import typing | |
| def f() -> None: | |
| a = A() # type: A | |
| b = B() # type: A # Fail | |
| class A: pass | |
| class B: pass | |
| [out] | |
| main:4: error: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| [case testVariableInitializationWithSubtype] | |
| import typing | |
| class A: pass | |
| class B(A): pass | |
| x = B() # type: A | |
| y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B") | |
| -- Misc | |
| -- ---- | |
| [case testInvalidReturn] | |
| import typing | |
| def f() -> 'A': | |
| return B() | |
| class A: pass | |
| class B: pass | |
| [out] | |
| main:3: error: Incompatible return value type (got "B", expected "A") | |
| [case testTopLevelContextAndInvalidReturn] | |
| import typing | |
| class A: pass | |
| class B: pass | |
| def f() -> 'A': | |
| return B() # E: Incompatible return value type (got "B", expected "A") | |
| a = B() # type: A # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| [case testEmptyReturnInAnyTypedFunction] | |
| from typing import Any | |
| def f() -> Any: | |
| return | |
| [case testEmptyYieldInAnyTypedFunction] | |
| from typing import Any | |
| def f() -> Any: | |
| yield | |
| [case testModuleImplicitAttributes] | |
| import typing | |
| class A: pass | |
| reveal_type(__name__) # N: Revealed type is "builtins.str" | |
| reveal_type(__doc__) # N: Revealed type is "builtins.str" | |
| reveal_type(__file__) # N: Revealed type is "builtins.str" | |
| reveal_type(__package__) # N: Revealed type is "builtins.str" | |
| reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]" | |
| # This will actually reveal Union[importlib.machinery.ModuleSpec, None] | |
| reveal_type(__spec__) # N: Revealed type is "builtins.object | None" | |
| import module | |
| reveal_type(module.__name__) # N: Revealed type is "builtins.str" | |
| # This will actually reveal importlib.machinery.ModuleSpec | |
| reveal_type(module.__spec__) # N: Revealed type is "builtins.object" | |
| [file module.py] | |
| [builtins fixtures/primitives.pyi] | |
| -- Scoping and shadowing | |
| -- --------------------- | |
| [case testLocalVariableShadowing] | |
| class A: pass | |
| class B: pass | |
| a: A | |
| if int(): | |
| a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| a = A() | |
| def f() -> None: | |
| a: B | |
| if int(): | |
| a = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B") | |
| a = B() | |
| a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A") | |
| a = A() | |
| [case testGlobalDefinedInBlockWithType] | |
| class A: pass | |
| while 1: | |
| a: A | |
| if int(): | |
| a = A() | |
| a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A") | |
| -- # type: signatures | |
| -- ------------------ | |
| [case testFunctionSignatureAsComment] | |
| def f(x): # type: (int) -> str | |
| return 1 | |
| f('') | |
| [out] | |
| main:2: error: Incompatible return value type (got "int", expected "str") | |
| main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" | |
| [case testMethodSignatureAsComment] | |
| class A: | |
| def f(self, x): | |
| # type: (int) -> str | |
| self.f('') # Fail | |
| return 1 | |
| A().f('') # Fail | |
| [out] | |
| main:4: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" | |
| main:5: error: Incompatible return value type (got "int", expected "str") | |
| main:6: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" | |
| [case testTrailingCommaParsing] | |
| x = 1 | |
| x in 1, # E: Unsupported right operand type for in ("int") | |
| [builtins fixtures/tuple.pyi] | |
| [case testTrailingCommaInIfParsing] | |
| if x in 1, : pass | |
| [out] | |
| main:1: error: Expected `:`, found `,` | |
| main:1: error: Expected a statement | |
| [case testInitReturnTypeError] | |
| class C: | |
| def __init__(self): | |
| # type: () -> int | |
| pass | |
| [out] | |
| main:2: error: The return type of "__init__" must be None | |
| -- WritesCache signals to testcheck to do the cache validation | |
| [case testWritesCache-writescache] | |
| import a | |
| import d | |
| [file a.py] | |
| import b | |
| import c | |
| [file b.py] | |
| [file c.py] | |
| [file d.py] | |
| [case testWritesCacheErrors-writescache] | |
| import a | |
| import d | |
| [file a.py] | |
| import b | |
| import c | |
| [file b.py] | |
| [file c.py] | |
| [file d.py] | |
| import e | |
| [file e.py] | |
| 1+'no' # E: Unsupported operand types for + ("int" and "str") | |
| [case testModuleAsTypeNoCrash] | |
| import mock | |
| from typing import Union | |
| class A: ... | |
| class B: ... | |
| x: Union[mock, A] # E: Module "mock" is not valid as a type \ | |
| # N: Perhaps you meant to use a protocol matching the module structure? | |
| if isinstance(x, B): | |
| pass | |
| [file mock.py] | |
| [builtins fixtures/isinstance.pyi] | |
| [out] | |
| [case testModuleAsTypeNoCrash2] | |
| import mock | |
| from typing import overload, Any, Union | |
| @overload | |
| def f(x: int) -> int: ... | |
| @overload | |
| def f(x: str) -> Union[mock, str]: ... # E: Module "mock" is not valid as a type \ | |
| # N: Perhaps you meant to use a protocol matching the module structure? | |
| def f(x): | |
| pass | |
| x: Any | |
| f(x) | |
| [file mock.py] | |
| [builtins fixtures/isinstance.pyi] | |
| [out] | |
| [case testPartialTypeComments] | |
| def foo( | |
| a, # type: str | |
| b, | |
| args=None, | |
| ): | |
| # type: (...) -> None | |
| pass | |
| [case testNoneHasBool] | |
| def main() -> None: | |
| none = None | |
| b = none.__bool__() | |
| reveal_type(b) # N: Revealed type is "Literal[False]" | |
| [builtins fixtures/bool.pyi] | |
| [case testAssignmentInvariantNoteForList] | |
| from typing import List | |
| x: List[int] | |
| y: List[float] | |
| y = x # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[float]") \ | |
| # N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ | |
| # N: Consider using "Sequence" instead, which is covariant | |
| [builtins fixtures/list.pyi] | |
| [case testAssignmentInvariantNoteForDict] | |
| from typing import Dict | |
| x: Dict[str, int] | |
| y: Dict[str, float] | |
| y = x # E: Incompatible types in assignment (expression has type "dict[str, int]", variable has type "dict[str, float]") \ | |
| # N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ | |
| # N: Consider using "Mapping" instead, which is covariant in the value type | |
| [builtins fixtures/dict.pyi] | |
| [case testDistinctTypes] | |
| import b | |
| [file a.py] | |
| from typing import NamedTuple, TypedDict | |
| from enum import Enum | |
| class A: pass | |
| N = NamedTuple('N', [('x', int)]) | |
| D = TypedDict('D', {'x': int}) | |
| class B(Enum): | |
| b = 10 | |
| [file b.py] | |
| from typing import Final, List, Literal, Optional, Union, Sequence, NamedTuple, Tuple, Type, TypedDict | |
| from enum import Enum | |
| import a | |
| class A: pass | |
| N = NamedTuple('N', [('x', int)]) | |
| class B(Enum): | |
| b = 10 | |
| D = TypedDict('D', {'y': int}) | |
| def foo() -> Optional[A]: | |
| b = True | |
| return a.A() if b else None # E: Incompatible return value type (got "a.A | None", expected "b.A | None") | |
| def bar() -> List[A]: | |
| l = [a.A()] | |
| return l # E: Incompatible return value type (got "list[a.A]", expected "list[b.A]") | |
| def baz() -> Union[A, int]: | |
| b = True | |
| return a.A() if b else 10 # E: Incompatible return value type (got "a.A | int", expected "b.A | int") | |
| def spam() -> Optional[A]: | |
| return a.A() # E: Incompatible return value type (got "a.A", expected "b.A | None") | |
| def eggs() -> Sequence[A]: | |
| x = [a.A()] | |
| return x # E: Incompatible return value type (got "list[a.A]", expected "Sequence[b.A]") | |
| def eggs2() -> Sequence[N]: | |
| x = [a.N(0)] | |
| return x # E: Incompatible return value type (got "list[a.N]", expected "Sequence[b.N]") | |
| def asdf1() -> Sequence[Tuple[a.A, A]]: | |
| x = [(a.A(), a.A())] | |
| return x # E: Incompatible return value type (got "list[tuple[a.A, a.A]]", expected "Sequence[tuple[a.A, b.A]]") | |
| def asdf2() -> Sequence[Tuple[A, a.A]]: | |
| x = [(a.A(), a.A())] | |
| return x # E: Incompatible return value type (got "list[tuple[a.A, a.A]]", expected "Sequence[tuple[b.A, a.A]]") | |
| def arg() -> Tuple[A, A]: | |
| return A() # E: Incompatible return value type (got "A", expected "tuple[A, A]") | |
| def types() -> Sequence[Type[A]]: | |
| x = [a.A] | |
| return x # E: Incompatible return value type (got "list[type[a.A]]", expected "Sequence[type[b.A]]") | |
| def literal() -> Sequence[Literal[B.b]]: | |
| x = [a.B.b] # type: List[Literal[a.B.b]] | |
| return x # E: Incompatible return value type (got "list[Literal[a.B.b]]", expected "Sequence[Literal[b.B.b]]") | |
| def typeddict() -> Sequence[D]: | |
| x = [{'x': 0}] # type: List[a.D] | |
| return x # E: Incompatible return value type (got "list[a.D]", expected "Sequence[b.D]") | |
| a = (a.A(), A()) # E: Incompatible types in assignment (expression has type "tuple[a.A, b.A]", variable has type Module) | |
| a.x # E: Module has no attribute "x" | |
| [builtins fixtures/dict.pyi] | |
| [typing fixtures/typing-full.pyi] | |
| [case testReturnAnyFromFunctionDeclaredToReturnObject] | |
| # flags: --warn-return-any | |
| from typing import Any | |
| def f() -> object: | |
| x: Any = 1 | |
| return x | |
| [case testImportModuleAsClassMember] | |
| import test | |
| class A: | |
| def __init__(self) -> None: | |
| self.test = test | |
| def __call__(self) -> None: | |
| self.test.foo("Message") | |
| [file test.py] | |
| def foo(s: str) -> None: ... | |
| [case testLocalImportModuleAsClassMember] | |
| class A: | |
| def __init__(self) -> None: | |
| import test | |
| self.test = test | |
| def __call__(self) -> None: | |
| self.test.foo("Message") | |
| [file test.py] | |
| def foo(s: str) -> None: ... | |
| [case testInlineAssertions] | |
| import a, b | |
| s1: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | |
| [file a.py] | |
| s2: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | |
| [file b.py] | |
| s3: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | |
| [file c.py] | |
| s3: str = 'foo' | |
| [case testMultilineQuotedAnnotation] | |
| x: """ | |
| int | | |
| str | |
| """ | |
| reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" | |
| y: """( | |
| int | | |
| str | |
| ) | |
| """ | |
| reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |