blob: 014bf4d846a923538d1452353a59e51bd69f885c [file] [log] [blame] [edit]
[case testEmptyFile]
[out]
[case testAssignmentAndVarDef]
a = None # type: A
b = None # type: 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]
x = None # type: A
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A:
def __init__(self): pass
class B:
def __init__(self): pass
[case testInheritInitFromObject]
x = None # type: A
if int():
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A(object): pass
class B(object): pass
[case testImplicitInheritInitFromObject]
x = None # type: A
o = None # type: 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
class A: pass
class B: pass
[out]
[case testTooManyConstructorArgs]
import typing
object(object())
[out]
main:2: error: Too many arguments for "object"
[case testVarDefWithInit]
import typing
a = A() # type: A
b = object() # type: A
class A: pass
[out]
main:3: error: Incompatible types in assignment (expression has type "object", variable has type "A")
[case testInheritanceBasedSubtyping]
import typing
x = B() # type: A
y = A() # type: B # Fail
class A: pass
class B(A): pass
[out]
main:3: error: Incompatible types in assignment (expression has type "A", variable has type "B")
[case testDeclaredVariableInParentheses]
(x) = None # 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
def f(x: 'A') -> None: pass
f(A())
f(B()) # Fail
class A: pass
class B: pass
[out]
main:4: error: Argument 1 to "f" has incompatible type "B"; expected "A"
[case testNotCallable]
import typing
A()()
class A: pass
[out]
main:2: error: "A" not callable
[case testSubtypeArgument]
import typing
def f(x: 'A', y: 'B') -> None: pass
f(B(), A()) # Fail
f(B(), B())
class A: pass
class B(A): pass
[out]
main:3: error: Argument 2 to "f" has incompatible type "A"; expected "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 = None # type: A
y = None # type: 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
x = B() # type: A
y = A() # type: B # Fail
class A: pass
class B(A): pass
[out]
main:3: error: 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
def f() -> 'A':
return B()
a = B() # type: A
class A: pass
class B: pass
[out]
main:3: error: Incompatible return value type (got "B", expected "A")
main:4: error: 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 testModule__name__]
import typing
x = __name__ # type: str
a = __name__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
class A: pass
[builtins fixtures/primitives.pyi]
[case testModule__doc__]
import typing
x = __doc__ # type: str
a = __doc__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
class A: pass
[builtins fixtures/primitives.pyi]
[case testModule__file__]
import typing
x = __file__ # type: str
a = __file__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
class A: pass
[builtins fixtures/primitives.pyi]
[case test__package__]
import typing
x = __package__ # type: str
a = __file__ # type: int # E: Incompatible types in assignment (expression has type "str", variable has type "int")
-- Scoping and shadowing
-- ---------------------
[case testLocalVariableShadowing]
a = None # type: A
if int():
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = A()
def f() -> None:
a = None # type: 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()
class A: pass
class B: pass
[case testGlobalDefinedInBlockWithType]
class A: pass
while A:
a = None # type: 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-skip]
x = 1
x in 1,
if x in 1, :
pass
[out]
[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]
import a
import d
[file a.py]
import b
import c
[file b.py]
[file c.py]
[file d.py]
[case testWritesCacheErrors]
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
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
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]
none = None
b = none.__bool__()
reveal_type(b) # N: Revealed type is 'builtins.bool'
[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 http://mypy.readthedocs.io/en/latest/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 http://mypy.readthedocs.io/en/latest/common_issues.html#variance \
# N: Consider using "Mapping" instead, which is covariant in the value type
[builtins fixtures/dict.pyi]
[case testDistinctTypes]
# flags: --strict-optional
import b
[file a.py]
from typing import NamedTuple
from typing_extensions import 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 List, Optional, Union, Sequence, NamedTuple, Tuple, Type
from typing_extensions import Literal, Final, 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 "Optional[a.A]", expected "Optional[b.A]")
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 "Union[a.A, int]", expected "Union[b.A, int]")
def spam() -> Optional[A]:
return a.A() # E: Incompatible return value type (got "a.A", expected "Optional[b.A]")
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())
a.x # E: "Tuple[a.A, b.A]" has no attribute "x"
[builtins fixtures/dict.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: ...