blob: f21ba5253437abd9f2e6d5a67faa1c5253b618c9 [file] [log] [blame]
[case testUndefinedVariableInGlobalStatement]
import typing
x
y
[out]
main:2: error: Name "x" is not defined
main:3: error: Name "y" is not defined
[case testUndefinedVariableWithinFunctionContext]
import typing
def f() -> None:
x
y
[out]
main:3: error: Name "x" is not defined
main:4: error: Name "y" is not defined
[case testMethodScope]
import typing
class A:
def f(self): pass
f
[out]
main:4: error: Name "f" is not defined
[case testMethodScope2]
import typing
class A:
def f(self): pass
class B:
def g(self) -> None:
f # error
g # error
[out]
main:6: error: Name "f" is not defined
main:7: error: Name "g" is not defined
[case testInvalidType]
import typing
x = None # type: X
[out]
main:2: error: Name "X" is not defined
[case testInvalidGenericArg]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(Generic[t]): pass
x = 0 # type: A[y]
[out]
main:4: error: Name "y" is not defined
[case testInvalidNumberOfGenericArgsInTypeDecl]
from typing import TypeVar, Generic
t = TypeVar('t')
class A: pass
class B(Generic[t]): pass
x = 0 # type: B[A, A]
y = 0 # type: A[A]
[out]
main:5: error: "B" expects 1 type argument, but 2 given
main:6: error: "A" expects no type arguments, but 1 given
[case testInvalidNumberOfGenericArgsInUndefinedArg]
class A: pass
x = None # type: A[int] # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInNestedBlock]
class A: pass
class B:
def f(self) -> None:
while 1:
x = None # type: A[int] \
# E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInSignature]
import typing
class A: pass
def f() -> A[int]: pass # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInOverloadedSignature]
from typing import overload
class A: pass
@overload
def f(): pass
@overload
def f(x: A[int]) -> None: pass # E: "A" expects no type arguments, but 1 given
def f(*args): pass
[out]
[case testInvalidNumberOfGenericArgsInBaseType]
import typing
class A: pass
class B(A[int]): pass # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInCast]
from typing import cast
class A: pass
x = cast(A[int], 1) # E: "A" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInNestedGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]): pass
class B: pass
def f() -> A[B[int]]: pass # E: "B" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfGenericArgsInTupleType]
from typing import Tuple
class A: pass
x = None # type: Tuple[A[int]] # E: "A" expects no type arguments, but 1 given
[builtins fixtures/tuple.pyi]
[out]
[case testInvalidNumberOfGenericArgsInFunctionType]
from typing import Callable
class A: pass
x = None # type: Callable[[A[int]], int] # E: "A" expects no type arguments, but 1 given
y = None # type: Callable[[], A[int]] # E: "A" expects no type arguments, but 1 given
[out]
[case testVarOrFuncAsType]
import typing
def f(): pass
x = 1
y = 0 # type: f
z = 0 # type: x
[out]
main:5: error: Function "__main__.f" is not valid as a type
main:5: note: Perhaps you need "Callable[...]" or a callback protocol?
main:6: error: Variable "__main__.x" is not valid as a type
main:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[case testGlobalVarRedefinition]
import typing
class A: pass
x = 0 # type: A
x = 0 # type: A
[out]
main:4: error: Name "x" already defined on line 3
[case testLocalVarRedefinition]
import typing
class A: pass
def f() -> None:
x = 0 # type: A
x = 0 # type: A
[out]
main:5: error: Name "x" already defined on line 4
[case testClassVarRedefinition]
import typing
class A:
x = 0 # type: object
x = 0 # type: object
[out]
main:4: error: Name "x" already defined on line 3
[case testMultipleClassDefinitions]
import typing
class A: pass
class A: pass
[out]
main:3: error: Name "A" already defined on line 2
[case testMultipleMixedDefinitions]
import typing
x = 1
def x(): pass
class x: pass
[out]
main:3: error: Name "x" already defined on line 2
main:4: error: Name "x" already defined on line 2
[case testNameNotImported]
import typing
from m import y
x
[file m.py]
x = y = 1
[out]
main:3: error: Name "x" is not defined
[case testMissingNameInImportFrom]
import typing
from m import y
[file m.py]
x = 1
[out]
main:2: error: Module "m" has no attribute "y"
[case testMissingModule]
import typing
import m
[out]
main:2: error: Cannot find implementation or library stub for module named "m"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModule2]
import typing
from m import x
[out]
main:2: error: Cannot find implementation or library stub for module named "m"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModule3]
import typing
from m import *
[out]
main:2: error: Cannot find implementation or library stub for module named "m"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModuleRelativeImport]
import typing
import m
[file m/__init__.py]
from .x import y
[out]
tmp/m/__init__.py:1: error: Cannot find implementation or library stub for module named "m.x"
tmp/m/__init__.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testMissingModuleRelativeImport2]
import typing
import m.a
[file m/__init__.py]
[file m/a.py]
from .x import y
[out]
tmp/m/a.py:1: error: Cannot find implementation or library stub for module named "m.x"
tmp/m/a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testModuleNotImported]
import typing
import _m
_n.x
[file _m.py]
import _n
[file _n.py]
x = 1
[out]
main:3: error: Name "_n" is not defined
[case testImportAsteriskPlusUnderscore]
import typing
from _m import *
_x
__x__
[file _m.py]
_x = __x__ = 1
[out]
main:3: error: Name "_x" is not defined
main:4: error: Name "__x__" is not defined
[case testRelativeImportAtTopLevelModule]
from . import m
[out]
main:1: error: No parent module -- cannot perform relative import
[case testRelativeImportAtTopLevelModule2]
from .. import m
[out]
main:1: error: No parent module -- cannot perform relative import
[case testUndefinedTypeWithQualifiedName]
import typing
import m
def f() -> m.c: pass
def g() -> n.c: pass
[file m.py]
[out]
main:3: error: Name "m.c" is not defined
main:4: error: Name "n" is not defined
[case testMissingPackage]
import typing
import m.n
[out]
main:2: error: Cannot find implementation or library stub for module named "m.n"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Cannot find implementation or library stub for module named "m"
[case testMissingPackage2]
import typing
from m.n import x
from a.b import *
[out]
main:2: error: Cannot find implementation or library stub for module named "m.n"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:3: error: Cannot find implementation or library stub for module named "a.b"
[case testErrorInImportedModule]
import m
[file m.py]
import typing
x = y
[out]
tmp/m.py:2: error: Name "y" is not defined
[case testErrorInImportedModule2]
import m.n
[file m/__init__.py]
[file m/n.py]
import k
[file k.py]
import typing
x = y
[out]
tmp/k.py:2: error: Name "y" is not defined
[case testPackageWithoutInitFile]
# flags: --no-namespace-packages
import typing
import m.n
m.n.x
[file m/n.py]
x = 1
[out]
main:3: error: Cannot find implementation or library stub for module named "m.n"
main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:3: error: Cannot find implementation or library stub for module named "m"
[case testBreakOutsideLoop]
break
def f():
break
[out]
main:1: error: "break" outside loop
main:3: error: "break" outside loop
[case testContinueOutsideLoop]
continue
def f():
continue
[out]
main:1: error: "continue" outside loop
main:3: error: "continue" outside loop
[case testReturnOutsideFunction]
def f(): pass
return
return 1
[out]
main:2: error: "return" outside function
main:3: error: "return" outside function
[case testYieldOutsideFunction]
yield 1
yield
[out]
main:1: error: "yield" outside function
main:2: error: "yield" outside function
[case testInvalidLvalues1]
1 = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues2]
(1) = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues3]
(1, 1) = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues4]
[1, 1] = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues6]
x = y = z = 1 # ok
x, (y, 1) = 1
[out]
main:2: error: cannot assign to literal
[case testInvalidLvalues7]
x, [y, 1] = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues8]
x, [y, [z, 1]] = 1
[out]
main:1: error: cannot assign to literal
[case testInvalidLvalues9]
x, (y) = 1 # ok
x, (y, (z, z)) = 1 # ok
x, (y, (z, 1)) = 1
[out]
main:3: error: cannot assign to literal
[case testInvalidLvalues10]
x + x = 1
[out]
main:1: error: cannot assign to operator
[out version>=3.10]
main:1: error: cannot assign to expression here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues11]
-x = 1
[out]
main:1: error: cannot assign to operator
[out version>=3.10]
main:1: error: cannot assign to expression here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues12]
1.1 = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues13]
'x' = 1
[out]
main:1: error: cannot assign to literal
[out version>=3.10]
main:1: error: cannot assign to literal here. Maybe you meant '==' instead of '='?
[case testInvalidLvalues14]
x() = 1
[out]
main:1: error: cannot assign to function call
[out version>=3.10]
main:1: error: cannot assign to function call here. Maybe you meant '==' instead of '='?
[case testTwoStarExpressions]
a, *b, *c = 1
*a, (*b, c) = 1
a, (*b, *c) = 1
[*a, *b] = 1
[out]
main:1: error: Two starred expressions in assignment
main:3: error: Two starred expressions in assignment
main:4: error: Two starred expressions in assignment
[case testTwoStarExpressionsInForStmt]
z = 1
for a, *b, *c in z:
pass
for *a, (*b, c) in z:
pass
for a, (*b, *c) in z:
pass
for [*a, *b] in z:
pass
[out]
main:2: error: Two starred expressions in assignment
main:6: error: Two starred expressions in assignment
main:8: error: Two starred expressions in assignment
[case testTwoStarExpressionsInGeneratorExpr]
(a for a, *b, *c in [])
(a for *a, (*b, c) in [])
(a for a, (*b, *c) in [])
[out]
main:1: error: Name "a" is not defined
main:1: error: Two starred expressions in assignment
main:3: error: Two starred expressions in assignment
[case testStarExpressionRhs]
b = 1
c = 1
d = 1
a = *b
[out]
main:4: error: Can use starred expression only as assignment target
[case testStarExpressionInExp]
a = 1
*a + 1
[out]
main:2: error: Can use starred expression only as assignment target
[case testInvalidDel1]
x = 1
del x(1)
[out]
main:2: error: cannot delete function call
[case testInvalidDel2]
x = 1
del x + 1
[out]
main:2: error: cannot delete operator
[out version>=3.10]
main:2: error: cannot delete expression
[case testInvalidDel3]
del z # E: Name "z" is not defined
[case testFunctionTvarScope]
from typing import TypeVar
t = TypeVar('t')
def f(x: t) -> t: pass
x = 0 # type: t
[out]
main:5: error: Type variable "__main__.t" is unbound
main:5: note: (Hint: Use "Generic[t]" or "Protocol[t]" base class to bind "t" inside a class)
main:5: note: (Hint: Use "t" in function signature to bind "t" inside a function)
[case testClassTvarScope]
from typing import Generic, TypeVar
t = TypeVar('t')
class c(Generic[t]): pass
x = 0 # type: t
[out]
main:5: error: Type variable "__main__.t" is unbound
main:5: note: (Hint: Use "Generic[t]" or "Protocol[t]" base class to bind "t" inside a class)
main:5: note: (Hint: Use "t" in function signature to bind "t" inside a function)
[case testExpressionRefersToTypeVariable]
from typing import TypeVar, Generic
t = TypeVar('t')
class c(Generic[t]):
def f(self) -> None: x = t
def f(y: t): x = t
[out]
main:4: error: "t" is a type variable and only valid in type context
main:5: error: "t" is a type variable and only valid in type context
[case testMissingSelf]
import typing
class A:
def f(): pass
[out]
main:3: error: Method must have at least one argument. Did you forget the "self" argument?
[case testInvalidBaseClass]
import typing
class A(B): pass
[out]
main:2: error: Name "B" is not defined
[case testSuperOutsideClass]
class A: pass
super().x
def f() -> None: super().y
[out]
main:2: error: "super" used outside class
main:3: error: "super" used outside class
[case testMissingSelfInMethod]
import typing
class A:
def f() -> None: pass
def g(): pass
[out]
main:3: error: Method must have at least one argument. Did you forget the "self" argument?
main:4: error: Method must have at least one argument. Did you forget the "self" argument?
[case testMultipleMethodDefinition]
import typing
class A:
def f(self) -> None: pass
def g(self) -> None: pass
def f(self, x: object) -> None: pass
[out]
main:5: error: Name "f" already defined on line 3
[case testInvalidGlobalDecl]
import typing
def f() -> None:
global x
x = None
[out]
main:4: error: Name "x" is not defined
[case testInvalidNonlocalDecl]
import typing
def f():
def g() -> None:
nonlocal x
x = None
[out]
main:4: error: No binding for nonlocal "x" found
main:5: error: Name "x" is not defined
[case testNonlocalDeclNotMatchingGlobal]
import typing
x = None
def f() -> None:
nonlocal x
x = None
[out]
main:4: error: No binding for nonlocal "x" found
main:5: error: Name "x" is not defined
[case testNonlocalDeclConflictingWithParameter]
import typing
def g():
x = None
def f(x) -> None:
nonlocal x
x = None
[out]
main:5: error: Name "x" is already defined in local scope before nonlocal declaration
[case testNonlocalDeclOutsideFunction]
x = 2
nonlocal x
[out]
main:2: error: nonlocal declaration not allowed at module level
[case testGlobalAndNonlocalDecl]
import typing
x = 1
def f():
x = 1
def g() -> None:
global x
nonlocal x
x = None
[out]
main:7: error: Name "x" is nonlocal and global
[case testNonlocalAndGlobalDecl]
import typing
x = 1
def f():
x = 1
def g() -> None:
nonlocal x
global x
x = None
[out]
main:7: error: Name "x" is nonlocal and global
[case testNestedFunctionAndScoping]
import typing
def f(x) -> None:
def g(y):
z = x
z
y
x
[out]
main:5: error: Name "z" is not defined
main:6: error: Name "y" is not defined
[case testMultipleNestedFunctionDef]
import typing
def f(x) -> None:
def g(): pass
x = 1
def g(): pass
[out]
main:5: error: Name "g" already defined on line 3
[case testRedefinedOverloadedFunction]
from typing import overload, Any
def f() -> None:
@overload
def p(o: object) -> None: pass # no error
@overload
def p(o: Any) -> None: pass # no error
x = 1
def p(): pass # fail
[out]
main:3: error: An overloaded function outside a stub file must have an implementation
main:8: error: Name "p" already defined on line 3
[case testNestedFunctionInMethod]
import typing
class A:
def f(self) -> None:
def g() -> None:
x
y
[out]
main:5: error: Name "x" is not defined
main:6: error: Name "y" is not defined
[case testImportScope]
import typing
def f() -> None:
import x
x.y # E: Name "x" is not defined
[file x.py]
y = 1
[out]
[case testImportScope2]
import typing
def f() -> None:
from x import y
y
y # E: Name "y" is not defined
[file x.py]
y = 1
[out]
[case testImportScope3]
import typing
def f() -> None:
from x import *
y
y # E: Name "y" is not defined
[file x.py]
y = 1
[out]
[case testImportScope4]
import typing
class A:
from x import *
y
y # E: Name "y" is not defined
[file x.py]
y = 1
[out]
[case testScopeOfNestedClass]
import typing
def f():
class A: pass
A
A # E: Name "A" is not defined
[out]
[case testScopeOfNestedClass2]
import typing
class A:
class B: pass
B # E: Name "B" is not defined
[out]
[case testScopeOfNestedClass3]
import typing
class A:
def f(self):
class B: pass
B # E: Name "B" is not defined
B # E: Name "B" is not defined
[out]
[case testInvalidNestedClassReferenceInDecl]
import typing
class A: pass
foo = 0 # type: A.x # E: Name "A.x" is not defined
[out]
[case testTvarScopingWithNestedClass]
from typing import TypeVar, Generic
t = TypeVar('t')
s = TypeVar('s')
class A(Generic[t]):
class B(Generic[s]):
x = 0 # type: A[s]
y = 0 # type: A[t] # E: Type variable "__main__.t" is unbound \
# N: (Hint: Use "Generic[t]" or "Protocol[t]" base class to bind "t" inside a class) \
# N: (Hint: Use "t" in function signature to bind "t" inside a function)
z = 0 # type: A[s] # E: Type variable "__main__.s" is unbound \
# N: (Hint: Use "Generic[s]" or "Protocol[s]" base class to bind "s" inside a class) \
# N: (Hint: Use "s" in function signature to bind "s" inside a function)
a = 0 # type: A[t]
[out]
[case testTestExtendPrimitives]
# Extending bool is not checked here as it should be typed
# as final meaning the type checker will detect it.
class C(bool): pass # ok
class A(int): pass # ok
class B(float): pass # ok
class D(str): pass # ok
[builtins fixtures/primitives.pyi]
[out]
[case testCyclicInheritance1]
class A(A): pass # E: Cannot resolve name "A" (possible cyclic definition)
[out]
[case testCyclicInheritance2]
class A(B): pass # E: Cannot resolve name "B" (possible cyclic definition)
class B(A): pass
[out]
[case testAssignToTypeDef]
import typing
class A: pass
A = None # E: Cannot assign to a type
[out]
[case testInvalidCastTargetSyntax]
from typing import cast, TypeVar, Generic
t = TypeVar('t')
class C(Generic[t]): pass
cast(str + str, None) # E: Cast target is not a type
cast(C[str][str], None) # E: Cast target is not a type
cast(C[str + str], None) # E: Cast target is not a type
cast([int], None) # E: Bracketed expression "[...]" is not valid as a type \
# N: Did you mean "List[...]"?
[out]
[case testInvalidCastTargetType]
from typing import cast
x = 0
cast(x, None) # E: Variable "__main__.x" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
cast(t, None) # E: Name "t" is not defined
cast(__builtins__.x, None) # E: Name "__builtins__.x" is not defined
[out]
[case testInvalidCastTargetType2]
from typing import cast
x = 0
cast(str[str], None) # E: "str" expects no type arguments, but 1 given
[out]
[case testInvalidNumberOfArgsToCast]
from typing import cast
cast(str) # E: "cast" expects 2 arguments
cast(str, None, None) # E: "cast" expects 2 arguments
[out]
[case testInvalidKindsOfArgsToCast]
from typing import cast
cast(str, *None) # E: "cast" must be called with 2 positional arguments
cast(str, target=None) # E: "cast" must be called with 2 positional arguments
[out]
[case testInvalidAssertType]
from typing import assert_type
assert_type(1, type=int) # E: "assert_type" must be called with 2 positional arguments
assert_type(1, *int) # E: "assert_type" must be called with 2 positional arguments
assert_type() # E: "assert_type" expects 2 arguments
assert_type(1, int, "hello") # E: "assert_type" expects 2 arguments
assert_type(int, 1) # E: Invalid type: try using Literal[1] instead?
assert_type(1, int[int]) # E: "int" expects no type arguments, but 1 given
[case testInvalidAnyCall]
from typing import Any
Any(str, None) # E: Any(...) is no longer supported. Use cast(Any, ...) instead
Any(arg=str) # E: Any(...) is no longer supported. Use cast(Any, ...) instead
[out]
[case testTypeListAsType]
def f(x: [int]) -> None: # E: Bracketed expression "[...]" is not valid as a type \
# N: Did you mean "List[...]"?
pass
[out]
[case testInvalidFunctionType]
from typing import Callable
x = None # type: Callable[int, str]
y = None # type: Callable[int]
z = None # type: Callable[int, int, int]
[out]
main:2: error: The first argument to Callable must be a list of types, parameter specification, or "..."
main:2: note: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas
main:3: error: Please use "Callable[[<parameters>], <return type>]" or "Callable"
main:4: error: Please use "Callable[[<parameters>], <return type>]" or "Callable"
[case testAbstractGlobalFunction]
import typing
from abc import abstractmethod
@abstractmethod
def foo(): pass
[out]
main:3: error: "abstractmethod" used with a non-method
[case testAbstractNestedFunction]
import typing
from abc import abstractmethod
def g() -> None:
@abstractmethod
def foo(): pass
[out]
main:4: error: "abstractmethod" used with a non-method
[case testInvalidTypeDeclaration]
import typing
def f(): pass
f() = 1 # type: int
[out]
main:3: error: cannot assign to function call
[out version>=3.10]
main:3: error: cannot assign to function call here. Maybe you meant '==' instead of '='?
[case testIndexedAssignmentWithTypeDeclaration]
import typing
None[1] = 1 # type: int
[out]
main:2: error: Unexpected type declaration
[case testNonSelfMemberAssignmentWithTypeDeclaration]
import typing
None.x = 1 # type: int
[out]
main:2: error: Type cannot be declared in assignment to non-self attribute
[case testNonSelfMemberAssignmentWithTypeDeclarationInMethod]
import typing
class A:
def f(self, x) -> None:
x.y = 1 # type: int
[out]
main:4: error: Type cannot be declared in assignment to non-self attribute
[case testInvalidTypeInTypeApplication]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(Generic[t]): pass
A[TypeVar] # E: Variable "typing.TypeVar" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[out]
[case testInvalidTypeInTypeApplication2]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(Generic[t]): pass
A[1] # E: Invalid type: try using Literal[1] instead?
[out]
[case testVariableDeclWithInvalidNumberOfTypes]
x, y = 1, 2 # type: int, str, int # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested]
x, (y, z) = 1, (2, 3) # type: int, (str, int, int) # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested2]
x, (y, z) = 1, (2, 3) # type: int, (str, ) # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested3]
x, (y, z) = 1, (2, 3) # type: int, str # E: Tuple type expected for multiple variables
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested4]
x, (y, z) = 1, (2, 3) # type: int, str, int # E: Incompatible number of tuple items
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidNumberOfTypesNested5]
x, (y, ) = 1, (2, ) # type: int, str # E: Tuple type expected for multiple variables
[builtins fixtures/tuple.pyi]
[out]
[case testVariableDeclWithInvalidType]
x, y = 1, 2 # type: int # E: Tuple type expected for multiple variables
[out]
[case testInvalidLvalueWithExplicitType]
a = 1
a() = None # type: int
[out]
main:2: error: cannot assign to function call
[out version>=3.10]
main:2: error: cannot assign to function call here. Maybe you meant '==' instead of '='?
[case testInvalidLvalueWithExplicitType2]
a = 1
a[1] = None # type: int # E: Unexpected type declaration
a.x = None # type: int \
# E: Type cannot be declared in assignment to non-self attribute
[out]
[case testInvalidLvalueWithExplicitType3]
a = 1
a.y, a.x = None, None # type: int, int \
# E: Type cannot be declared in assignment to non-self attribute
a[1], a[2] = None, None # type: int, int \
# E: Unexpected type declaration
[builtins fixtures/tuple.pyi]
[out]
[case testMissingGenericImport]
from typing import TypeVar
T = TypeVar('T')
class A(Generic[T]): pass
[out]
main:3: error: Name "Generic" is not defined
[case testInvalidTypeWithinGeneric]
from typing import Generic
class A(Generic[int]): pass # E: Free type variable expected in Generic[...]
[out]
[case testInvalidTypeWithinNestedGenericClass]
from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
class B(Generic[T]): pass \
# E: Free type variable expected in Generic[...]
[out]
[case testIncludingGenericTwiceInBaseClassList]
from typing import Generic, TypeVar
T = TypeVar('T')
S = TypeVar('S')
class A(Generic[T], Generic[S]): pass \
# E: Only single Generic[...] or Protocol[...] can be in bases
[out]
[case testInvalidMetaclass]
class A(metaclass=x): pass # E: Name "x" is not defined
[out]
[case testInvalidQualifiedMetaclass]
import abc
class A(metaclass=abc.Foo): pass # E: Name "abc.Foo" is not defined
[out]
[case testNonClassMetaclass]
def f(): pass
class A(metaclass=f): pass # E: Invalid metaclass "f"
[out]
[case testInvalidTypevarArguments]
from typing import TypeVar
a = TypeVar() # E: Too few arguments for TypeVar()
b = TypeVar(x='b') # E: TypeVar() expects a string literal as first argument
c = TypeVar(1) # E: TypeVar() expects a string literal as first argument
T = TypeVar(b'T') # E: TypeVar() expects a string literal as first argument
d = TypeVar('D') # E: String argument 1 "D" to TypeVar(...) does not match variable name "d"
e = TypeVar('e', int, str, x=1) # E: Unexpected argument to "TypeVar()": "x"
f = TypeVar('f', (int, str), int) # E: Type expected
g = TypeVar('g', int) # E: TypeVar cannot have only a single constraint
h = TypeVar('h', x=(int, str)) # E: Unexpected argument to "TypeVar()": "x"
i = TypeVar('i', bound=1) # E: TypeVar "bound" must be a type
j = TypeVar('j', covariant=None) # E: TypeVar "covariant" may only be a literal bool
k = TypeVar('k', contravariant=1) # E: TypeVar "contravariant" may only be a literal bool
[out]
[case testMoreInvalidTypevarArguments]
from typing import TypeVar
T = TypeVar('T', int, str, bound=bool) # E: TypeVar cannot have both values and an upper bound
S = TypeVar('S', covariant=True, contravariant=True) \
# E: TypeVar cannot be both covariant and contravariant
[builtins fixtures/bool.pyi]
[case testInvalidTypevarValues]
from typing import TypeVar
b = TypeVar('b', *[int]) # E: Unexpected argument to "TypeVar()"
c = TypeVar('c', int, 2) # E: Invalid type: try using Literal[2] instead?
[out]
[case testObsoleteTypevarValuesSyntax]
from typing import TypeVar
a = TypeVar('a', values=(int, str))
[out]
main:2: error: TypeVar "values" argument not supported
main:2: error: Use TypeVar('T', t, ...) instead of TypeVar('T', values=(t, ...))
[case testLocalTypevarScope]
from typing import TypeVar
def f() -> None:
T = TypeVar('T')
def g(x: T) -> None: pass # E: Name "T" is not defined
[out]
[case testClassTypevarScope]
from typing import TypeVar
class A:
T = TypeVar('T')
def g(x: T) -> None: pass # E: Name "T" is not defined
[out]
[case testRedefineVariableAsTypevar]
from typing import TypeVar
x = 0
x = TypeVar('x') # E: Cannot redefine "x" as a type variable
[out]
[case testTypevarWithType]
from typing import TypeVar
x = TypeVar('x') # type: int # E: Cannot declare the type of a TypeVar or similar construct
[out]
[case testRedefineTypevar]
from typing import TypeVar
t = TypeVar('t')
t = 1 # E: Invalid assignment target
[out]
[case testRedefineTypevar2]
from typing import TypeVar
t = TypeVar('t')
def t(): pass # E: Name "t" already defined on line 2
[out]
[case testRedefineTypevar3]
from typing import TypeVar
t = TypeVar('t')
class t: pass # E: Name "t" already defined on line 2
[out]
[case testRedefineTypevar4]
from typing import TypeVar
t = TypeVar('t')
from typing import Generic as t # E: Name "t" already defined on line 2
[out]
[case testInvalidStrLiteralType]
def f(x: 'foo'): pass # E: Name "foo" is not defined
[out]
[case testInvalidStrLiteralStrayBrace]
def f(x: 'int['): pass # E: Invalid type comment or annotation
[out]
[case testInvalidStrLiteralSpaces]
def f(x: 'A B'): pass # E: Invalid type comment or annotation
[out]
[case testInvalidMultilineLiteralType]
def f() -> "A\nB": pass # E: Invalid type comment or annotation
[out]
[case testInconsistentOverload]
from typing import overload
def dec(x): pass
@dec # E: The implementation for an overloaded function must come last
def f(): pass
@overload
def f(): pass
[out]
[case testInconsistentOverload2]
from typing import overload
def dec(x): pass
@dec # E: The implementation for an overloaded function must come last
def f(): pass
@overload
def f(): pass
[out]
[case testMissingOverloadDecorator]
from typing import overload
def dec(x): pass
@dec
def f(): pass
@dec # E: Name "f" already defined on line 3
def f(): pass
[out]
[case testIncompatibleSignatureInComment]
import typing
def f(): # type: (int) -> int
pass
def g(x): # type: () -> int
pass
[out]
main:2: error: Type signature has too many arguments
main:4: error: Type signature has too few arguments
[case testStaticmethodAndNonMethod]
import typing
@staticmethod
def f(): pass
class A:
def g(self) -> None:
@staticmethod
def h(): pass
[builtins fixtures/staticmethod.pyi]
[out]
main:2: error: "staticmethod" used with a non-method
main:6: error: "staticmethod" used with a non-method
[case testClassmethodAndNonMethod]
import typing
@classmethod
def f(): pass
class A:
def g(self) -> None:
@classmethod
def h(): pass
[builtins fixtures/classmethod.pyi]
[out]
main:2: error: "classmethod" used with a non-method
main:6: error: "classmethod" used with a non-method
[case testNonMethodProperty]
import typing
@property # E: "property" used with a non-method
def f() -> int: pass
[builtins fixtures/property.pyi]
[out]
[case testOverloadedProperty]
from typing import overload
class A:
@overload # E: Decorators on top of @property are not supported
@property
def f(self) -> int: pass
@property # E: Only supported top decorator is @f.setter
@overload
def f(self) -> int: pass
[builtins fixtures/property.pyi]
[out]
[case testOverloadedProperty2]
from typing import overload
class A:
@overload # E: An overloaded function outside a stub file must have an implementation
def f(self) -> int: pass
@property # E: An overload can not be a property
@overload
def f(self) -> int: pass
[builtins fixtures/property.pyi]
[out]
[case testDecoratedProperty]
import typing
def dec(f): pass
class A:
@dec # E: Decorators on top of @property are not supported
@property
def f(self) -> int: pass
@property # OK
@dec
def g(self) -> int: pass
[builtins fixtures/property.pyi]
[out]
[case testImportTwoModulesWithSameNameInFunction]
import typing
def f() -> None:
import x
import y as x # E: Name "x" already defined (by an import)
x.y
[file x.py]
y = 1
[file y.py]
[out]
[case testImportTwoModulesWithSameNameInGlobalContext]
import typing
import x
import y as x # E: Name "x" already defined (by an import)
x.y
[file x.py]
y = 1
[file y.py]
[out]
[case testListTypeAliasWithoutImport]
import typing
def f() -> List[int]: pass
[builtins fixtures/list.pyi]
[out]
main:2: error: Name "List" is not defined
main:2: note: Did you forget to import it from "typing"? (Suggestion: "from typing import List")
[case testInvalidWithTarget]
def f(): pass
with f() as 1: pass
[out]
main:2: error: cannot assign to literal
[case testInvalidTypeAnnotation]
import typing
def f() -> None:
1[2] = 1 # type: int
[out]
main:3: error: Unexpected type declaration
[case testInvalidTypeAnnotation2]
import typing
def f() -> None:
f() = 1 # type: int
[out]
main:3: error: cannot assign to function call
[out version>=3.10]
main:3: error: cannot assign to function call here. Maybe you meant '==' instead of '='?
[case testInvalidReferenceToAttributeOfOuterClass]
class A:
class X: pass
class B:
y = X # E: Name "X" is not defined
[out]
[case testStubPackage]
from m import x
from m import y # E: Module "m" has no attribute "y"
[file m/__init__.pyi]
x = 1
[out]
[case testStubPackageSubModule]
from m import x
from m import y # E: Module "m" has no attribute "y"
from m.m2 import y
from m.m2 import z # E: Module "m.m2" has no attribute "z"
[file m/__init__.pyi]
x = 1
[file m/m2.pyi]
y = 1
[out]
[case testListComprehensionSpecialScoping]
class A:
x = 1
y = 1
z = 1
[x for i in z if y]
[out]
main:5: error: Name "x" is not defined
main:5: error: Name "y" is not defined
[case testTypeRedeclarationNoSpuriousWarnings]
from typing import Tuple
a = 1 # type: int
a = 's' # type: str
a = ('spam', 'spam', 'eggs', 'spam') # type: Tuple[str]
[builtins fixtures/tuple.pyi]
[out]
main:3: error: Name "a" already defined on line 2
main:4: error: Name "a" already defined on line 2
[case testDuplicateDefFromImport]
from m import A
class A: # E: Name "A" already defined (possibly by an import)
pass
[file m.py]
class A:
pass
[out]
[case testDuplicateDefDec]
from typing import Any
def dec(x: Any) -> Any:
return x
@dec
def f() -> None:
pass
@dec # E: Name "f" already defined on line 4
def f() -> None:
pass
[out]
[case testDuplicateDefOverload]
from typing import overload, Any
if 1:
@overload
def f(x: int) -> None:
pass
@overload
def f(x: str) -> None:
pass
def f(x: Any) -> None:
pass
else:
def f(x: str) -> None: # E: Name "f" already defined on line 3
pass
[out]
[case testDuplicateDefNT]
from typing import NamedTuple
N = NamedTuple('N', [('a', int),
('b', str)])
class N: # E: Name "N" already defined on line 2
pass
[builtins fixtures/tuple.pyi]
[out]
[case testDuplicateDefTypedDict]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
class Point: # E: Name "Point" already defined on line 2
pass
[builtins fixtures/dict.pyi]
[out]
[case testTypeVarClassDup]
from typing import TypeVar
T = TypeVar('T')
class T: ... # E: Name "T" already defined on line 2
[out]
[case testAliasDup]
from typing import List
A = List[int]
class A: ... # E: Name "A" already defined on line 2
[builtins fixtures/list.pyi]
[out]
[case testNoInvalidTypeInDynamicFunctions]
from typing import Dict, TypeVar
T = TypeVar('T')
def f(): # Note no annotation
x: Dict[str, T] = {}
y: T
z: x
def nested(): pass
t: nested
def g() -> None:
x: Dict[str, T] = {} # E: Type variable "__main__.T" is unbound \
# N: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) \
# N: (Hint: Use "T" in function signature to bind "T" inside a function)
[builtins fixtures/dict.pyi]
[out]
[case testParamSpec]
from typing_extensions import ParamSpec
TParams = ParamSpec('TParams')
TP = ParamSpec('?') # E: String argument 1 "?" to ParamSpec(...) does not match variable name "TP"
TP2: int = ParamSpec('TP2') # E: Cannot declare the type of a TypeVar or similar construct
[out]
[case testBaseClassAnnotatedWithoutArgs]
# https://github.com/python/mypy/issues/11808
from typing_extensions import Annotated
# Next line should not crash:
class A(Annotated): pass # E: Annotated[...] must have exactly one type argument and at least one annotation
[case testInvalidUnpackTypes]
from typing_extensions import Unpack
from typing import Tuple
heterogenous_tuple: Tuple[Unpack[Tuple[int, str]]]
homogenous_tuple: Tuple[Unpack[Tuple[int, ...]]]
bad: Tuple[Unpack[int]] # E: "int" cannot be unpacked (must be tuple or TypeVarTuple)
[builtins fixtures/tuple.pyi]
[case testTypeVarTupleErrors]
from typing import Generic
from typing_extensions import TypeVarTuple, Unpack
TVariadic = TypeVarTuple('TVariadic')
TVariadic2 = TypeVarTuple('TVariadic2')
TP = TypeVarTuple('?') # E: String argument 1 "?" to TypeVarTuple(...) does not match variable name "TP"
TP2: int = TypeVarTuple('TP2') # E: Cannot declare the type of a TypeVar or similar construct
TP3 = TypeVarTuple() # E: Too few arguments for TypeVarTuple()
TP4 = TypeVarTuple('TP4', 'TP4') # E: Too many positional arguments for "TypeVarTuple"
TP5 = TypeVarTuple(t='TP5') # E: TypeVarTuple() expects a string literal as first argument
TP6 = TypeVarTuple('TP6', bound=int) # E: Unexpected keyword argument "bound" for "TypeVarTuple"
x: TVariadic # E: TypeVarTuple "TVariadic" is unbound
y: Unpack[TVariadic] # E: Unpack is only valid in a variadic position
class Variadic(Generic[Unpack[TVariadic], Unpack[TVariadic2]]): # E: Can only use one type var tuple in a class def
pass
def bad_args(*args: TVariadic): # E: TypeVarTuple "TVariadic" is only valid with an unpack
pass
def bad_kwargs(**kwargs: Unpack[TVariadic]): # E: Unpack item in ** argument must be a TypedDict
pass
[builtins fixtures/dict.pyi]