1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
# 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]
# flags: --no-local-partial-types
(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")
s: str
if int():
del t[s] # E:11: Expected TypedDict key to be string literal
del t["x"] # E:11: Key "x" of TypedDict "D" cannot be deleted
del t["y"] # E:11: TypedDict "D" has no key "y"
t.pop(s) # E:7: Expected TypedDict key to be string literal
t.pop("y") # E:7: TypedDict "D" has no key "y"
t.setdefault(s, 123) # E:14: Expected TypedDict key to be string literal
t.setdefault("x", "a") # E:19: Argument 2 to "setdefault" of "TypedDict" has incompatible type "str"; expected "int"
t.setdefault("y", 123) # E:14: 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: Missing type parameters for generic type "list"
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:12: 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 testColumnInvalidLocationForParamSpec]
from typing import List
from typing_extensions import ParamSpec
P = ParamSpec('P')
def foo(x: List[P]): pass # E:17: Invalid location for ParamSpec "P" \
# N:17: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
[builtins fixtures/list.pyi]
[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
[case testEndColumnsWithTooManyTypeVars]
# flags: --pretty
import typing
x1: typing.List[typing.List[int, int]]
x2: list[list[int, int]]
[out]
main:4:17: error: "list" expects 1 type argument, but 2 given
x1: typing.List[typing.List[int, int]]
^~~~~~~~~~~~~~~~~~~~~
main:5:10: error: "list" expects 1 type argument, but 2 given
x2: list[list[int, int]]
^~~~~~~~~~~~~~
|