blob: 44524b9df9435c942e9555b56ebd2986ea534eae [file] [log] [blame] [edit]
# Test column numbers in messages. --show-column-numbers is enabled implicitly by test runner.
[case testColumnsSyntaxError]
f()
1 +
[out]
main:2:5: error: invalid syntax
[case testColumnsNestedFunctions]
import typing
def f() -> 'A':
def g() -> 'B':
return A() # E:16: Incompatible return value type (got "A", expected "B")
return B() # E:12: Incompatible return value type (got "B", expected "A")
class A: pass
class B: pass
[case testColumnsMethodDefaultArgumentsAndSignatureAsComment]
import typing
class A:
def f(self, x = 1, y = 'hello'): # type: (int, str) -> str
pass
A().f()
A().f(1)
A().f('') # E:7: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
A().f(1, 1) # E:10: Argument 2 to "f" of "A" has incompatible type "int"; expected "str"
(A().f(1, 'hello', 'hi')) # E:2: Too many arguments for "f" of "A"
[case testColumnsInvalidArgumentType]
def f(x: int, y: str) -> None: ...
def g(*x: int) -> None: pass
def h(**x: int) -> None: pass
def ff(x: int) -> None: pass
class A:
x: str
def __neg__(self) -> str: pass
def __add__(self, other: int) -> str: pass
def __lt__(self, other: int) -> str: pass
f(
y=0, x=0) # E:4: Argument "y" to "f" has incompatible type "int"; expected "str"
f(x=0,
y=None) # E:6: Argument "y" to "f" has incompatible type "None"; expected "str"
g(1, '', 2) # E:6: Argument 2 to "g" has incompatible type "str"; expected "int"
aaa: str
h(x=1, y=aaa, z=2) # E:10: Argument "y" to "h" has incompatible type "str"; expected "int"
a: A
ff(a.x) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int"
ff([1]) # E:4: Argument 1 to "ff" has incompatible type "List[int]"; expected "int"
# TODO: Different column in Python 3.8+
#ff([1 for x in [1]]) # Argument 1 to "ff" has incompatible type "List[int]"; expected "int"
ff({1: 2}) # E:4: Argument 1 to "ff" has incompatible type "Dict[int, int]"; expected "int"
ff(1.1) # E:4: Argument 1 to "ff" has incompatible type "float"; expected "int"
# TODO: Different column in Python 3.8+
#ff( ( 1, 1)) # Argument 1 to "ff" has incompatible type "Tuple[int, int]"; expected "int"
ff(-a) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int"
ff(a + 1) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int"
ff(a < 1) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int"
ff([''][0]) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int"
class B(A):
def f(self) -> None:
ff(super().__neg__()) # E:12: Argument 1 to "ff" has incompatible type "str"; expected "int"
[builtins fixtures/dict.pyi]
[case testColumnsInvalidArgumentTypeVarArgs]
def f(*x: int) -> None: pass
def g(**x: int) -> None: pass
a = ['']
f(*a) # E:4: Argument 1 to "f" has incompatible type "*List[str]"; expected "int"
b = {'x': 'y'}
g(**b) # E:5: Argument 1 to "g" has incompatible type "**Dict[str, str]"; expected "int"
[builtins fixtures/dict.pyi]
[case testColumnsMultipleStatementsPerLine]
x = 15
y = 'hello'
if int():
x = 2; y = x; y += 1
[builtins fixtures/primitives.pyi]
[out]
main:4:16: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:4:24: error: Unsupported operand types for + ("str" and "int")
[case testColumnsAssignment]
class A:
x = 0
A().x = '' # E:9: Incompatible types in assignment (expression has type "str", variable has type "int")
a = [0]
a[0] = '' # E:8: Incompatible types in assignment (expression has type "str", target has type "int")
b = 0
c = 0
b, c = 0, '' # E:11: Incompatible types in assignment (expression has type "str", variable has type "int")
b, c = '', 0 # E:8: Incompatible types in assignment (expression has type "str", variable has type "int")
t = 0, ''
b, c = t # E:8: Incompatible types in assignment (expression has type "str", variable has type "int")
class B(A):
x = '' # E:9: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int")
[builtins fixtures/list.pyi]
[case testColumnsAttributeIncompatibleWithBaseClassUsingAnnotation]
class A:
x: str
class B(A):
x: int # E:5: Incompatible types in assignment (expression has type "int", base class "A" defined the type as "str")
[case testColumnsSimpleIsinstance]
import typing
def f(x: object, n: int, s: str) -> None:
if int():
n = x # E:13: Incompatible types in assignment (expression has type "object", variable has type "int")
if isinstance(x, int):
n = x
s = x # E:17: Incompatible types in assignment (expression has type "int", variable has type "str")
n = x # E:13: Incompatible types in assignment (expression has type "object", variable has type "int")
[builtins fixtures/isinstance.pyi]
[case testColumnHasNoAttribute]
import m
if int():
from m import foobaz # E:5: Module "m" has no attribute "foobaz"; maybe "foobar"?
1 .x # E:1: "int" has no attribute "x"
(m.foobaz()) # E:2: Module has no attribute "foobaz"; maybe "foobar"?
[file m.py]
def foobar(): pass
[builtins fixtures/module.pyi]
[case testColumnUnexpectedOrMissingKeywordArg]
def f(): pass # N:1: "f" defined here
# TODO: Point to "x" instead
(f(x=1)) # E:2: Unexpected keyword argument "x" for "f"
def g(*, x: int) -> None: pass
(g()) # E:2: Missing named argument "x" for "g"
[case testColumnDefinedHere]
class A: pass
if int():
def f(a: 'A') -> None: pass # N:5: "f" defined here
(f(b=object())) # E:6: Unexpected keyword argument "b" for "f"
[case testColumnInvalidType]
from typing import Iterable
bad = 0
def f(x: bad): # E:10: Variable "__main__.bad" is not valid as a type \
# N:10: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
y: bad # E:8: Variable "__main__.bad" is not valid as a type \
# N:8: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
if int():
def g(x): # E:5: Variable "__main__.bad" is not valid as a type \
# N:5: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
# type: (bad) -> None
y = 0 # type: bad # E:9: Variable "__main__.bad" is not valid as a type \
# N:9: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
z: Iterable[bad] # E:13: Variable "__main__.bad" is not valid as a type \
# N:13: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
h: bad[int] # E:4: Variable "__main__.bad" is not valid as a type \
# N:4: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[case testColumnFunctionMissingTypeAnnotation]
# flags: --disallow-untyped-defs
if int():
def f(x: int): # E:5: Function is missing a return type annotation
pass
def g(x): # E:5: Function is missing a type annotation
pass
[case testColumnNameIsNotDefined]
((x)) # E:3: Name "x" is not defined
[case testColumnNeedTypeAnnotation]
if 1:
x = [] # E:5: Need type annotation for "x" (hint: "x: List[<type>] = ...")
[builtins fixtures/list.pyi]
[case testColumnCallToUntypedFunction]
# flags: --disallow-untyped-calls
def f() -> None:
(g(1)) # E:6: Call to untyped function "g" in typed context
def g(x):
pass
[case testColumnInvalidArguments]
def f(x, y): pass
(f()) # E:2: Missing positional arguments "x", "y" in call to "f"
(f(y=1)) # E:2: Missing positional argument "x" in call to "f"
[case testColumnListOrDictItemHasIncompatibleType]
from typing import List, Dict
x: List[int] = [
'x', # E:5: List item 0 has incompatible type "str"; expected "int"
1.1] # E:7: List item 1 has incompatible type "float"; expected "int"
y: Dict[int, int] = {
'x': 1 # E:5: Dict entry 0 has incompatible type "str": "int"; expected "int": "int"
}
[builtins fixtures/dict.pyi]
[case testColumnCannotDetermineType]
(x) # E:2: Cannot determine type of "x" # E:2: Name "x" is used before definition
x = None
[case testColumnInvalidIndexing]
from typing import List
([1]['']) # E:6: Invalid index type "str" for "List[int]"; expected type "int"
(1[1]) # E:2: Value of type "int" is not indexable
def f() -> None:
1[1] = 1 # E:5: Unsupported target for indexed assignment ("int")
[builtins fixtures/list.pyi]
[case testColumnTypedDict]
from typing import TypedDict
class D(TypedDict):
x: int
t: D = {'x':
'y'} # E:5: Incompatible types (expression has type "str", TypedDict item "x" has type "int")
if int():
del t['y'] # E:5: TypedDict "D" has no key "y"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[case testColumnSignatureIncompatibleWithSuperType]
class A:
def f(self, x: int) -> None: pass
class B(A):
def f(self, x: str) -> None: pass # E:17: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \
# N:17: This violates the Liskov substitution principle \
# N:17: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
class C(A):
def f(self, x: int) -> int: pass # E:5: Return type "int" of "f" incompatible with return type "None" in supertype "A"
class D(A):
def f(self) -> None: pass # E:5: Signature of "f" incompatible with supertype "A" \
# N:5: Superclass: \
# N:5: def f(self, x: int) -> None \
# N:5: Subclass: \
# N:5: def f(self) -> None
[case testColumnMissingTypeParameters]
# flags: --disallow-any-generics
from typing import List, Callable
def f(x: List) -> None: pass # E:10: Missing type parameters for generic type "List"
def g(x: list) -> None: pass # E:10: Implicit generic "Any". Use "typing.List" and specify generic parameters
if int():
c: Callable # E:8: Missing type parameters for generic type "Callable"
[builtins fixtures/list.pyi]
[case testColumnIncompatibleDefault]
if int():
def f(x: int = '') -> None: # E:20: Incompatible default for argument "x" (default has type "str", argument has type "int")
pass
[case testColumnMissingProtocolMember]
from typing import Protocol
class P(Protocol):
x: int
y: int
class C:
x: int
p: P
if int():
p = C() # E:9: Incompatible types in assignment (expression has type "C", variable has type "P") \
# N:9: "C" is missing following "P" protocol member: \
# N:9: y
[case testColumnRedundantCast]
# flags: --warn-redundant-casts
from typing import cast
y = 1
x = cast(int, y) # E:5: Redundant cast to "int"
[case testColumnTypeSignatureHasTooFewArguments]
if int():
def f(x, y): # E:5: Type signature has too few arguments
# type: (int) -> None
pass
[case testColumnRevealedType]
if int():
reveal_type(1) # N:17: Revealed type is "Literal[1]?"
[case testColumnNonOverlappingEqualityCheck]
# flags: --strict-equality
if 1 == '': # E:4: Non-overlapping equality check (left operand type: "Literal[1]", right operand type: "Literal['']")
pass
[builtins fixtures/bool.pyi]
[case testColumnValueOfTypeVariableCannotBe]
from typing import TypeVar, Generic
T = TypeVar('T', int, str)
class C(Generic[T]):
pass
def f(c: C[object]) -> None: pass # E:10: Value of type variable "T" of "C" cannot be "object"
(C[object]()) # E:2: Value of type variable "T" of "C" cannot be "object"
[case testColumnSyntaxErrorInTypeAnnotation]
if int():
def f(x # type: int,
):
pass
[out]
main:2:11: error: Syntax error in type annotation
main:2:11: note: Suggestion: Is there a spurious trailing comma?
[case testColumnSyntaxErrorInTypeAnnotation2]
if int():
# TODO: It would be better to point to the type comment
xyz = 0 # type: blurbnard blarb
[out]
main:3:5: error: Syntax error in type comment "blurbnard blarb"
[case testColumnProperty]
class A:
@property
def x(self) -> int: pass
@x.setter
def x(self, x: int) -> None: pass
class B(A):
@property # E:6: Read-only property cannot override read-write property
def x(self) -> int: pass
[builtins fixtures/property.pyi]
[case testColumnOverloaded]
from typing import overload, Any
class A:
@overload # E:6: An overloaded function outside a stub file must have an implementation
def f(self, x: int) -> int: pass
@overload
def f(self, x: str) -> str: pass
[case testColumnFunctionWithTypeVarValues]
from typing import TypeVar, List
T = TypeVar('T', int, str)
def g(x): pass # N:1: "g" defined here
def f(x: T) -> T:
(x.bad) # E:6: "int" has no attribute "bad" \
# E:6: "str" has no attribute "bad"
g(y=x) # E:5: Unexpected keyword argument "y" for "g"
y: List[int, str] # E:8: "list" expects 1 type argument, but 2 given
del 1[0] # E:5: "int" has no attribute "__delitem__"
bb: List[int] = [''] # E:22: List item 0 has incompatible type "str"; expected "int"
# XXX: Disabled because the column differs in 3.8
# aa: List[int] = ['' for x in [1]] # :22: List comprehension has incompatible type List[str]; expected List[int]
cc = 1 .bad # E:10: "int" has no attribute "bad"
n: int = '' # E:14: Incompatible types in assignment (expression has type "str", variable has type "int")
return x
[builtins fixtures/list.pyi]
[case testColumnReturnValueExpected]
def f() -> int:
return # E:5: Return value expected
[case testCheckEndColumnPositions]
# flags: --show-error-end
x: int = "no way"
def g() -> int: ...
def f(x: str) -> None: ...
f(g(
))
x[0]
[out]
main:2:10:2:17: error: Incompatible types in assignment (expression has type "str", variable has type "int")
main:6:3:7:1: error: Argument 1 to "f" has incompatible type "int"; expected "str"
main:8:1:8:4: error: Value of type "int" is not indexable