| 1 |
# Test column numbers in messages. --show-column-numbers is enabled implicitly by test runner. |
| 2 |
|
| 3 |
[case testColumnsSyntaxError_no_native_parse] |
| 4 |
f() |
| 5 |
1 + |
| 6 |
[out] |
| 7 |
main:2:5: error: Invalid syntax |
| 8 |
|
| 9 |
[case testColumnsNestedFunctions] |
| 10 |
import typing |
| 11 |
def f() -> 'A': |
| 12 |
def g() -> 'B': |
| 13 |
return A() # E:16: Incompatible return value type (got "A", expected "B") |
| 14 |
return B() # E:12: Incompatible return value type (got "B", expected "A") |
| 15 |
class A: pass |
| 16 |
class B: pass |
| 17 |
|
| 18 |
[case testColumnsMethodDefaultArgumentsAndSignatureAsComment] |
| 19 |
import typing |
| 20 |
class A: |
| 21 |
def f(self, x = 1, y = 'hello'): # type: (int, str) -> str |
| 22 |
pass |
| 23 |
A().f() |
| 24 |
A().f(1) |
| 25 |
A().f('') # E:7: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" |
| 26 |
A().f(1, 1) # E:10: Argument 2 to "f" of "A" has incompatible type "int"; expected "str" |
| 27 |
(A().f(1, 'hello', 'hi')) # E:2: Too many arguments for "f" of "A" |
| 28 |
|
| 29 |
[case testColumnsInvalidArgumentType] |
| 30 |
def f(x: int, y: str) -> None: ... |
| 31 |
def g(*x: int) -> None: pass |
| 32 |
def h(**x: int) -> None: pass |
| 33 |
def ff(x: int) -> None: pass |
| 34 |
|
| 35 |
class A: |
| 36 |
x: str |
| 37 |
def __neg__(self) -> str: pass |
| 38 |
def __add__(self, other: int) -> str: pass |
| 39 |
def __lt__(self, other: int) -> str: pass |
| 40 |
|
| 41 |
f( |
| 42 |
y=0, x=0) # E:4: Argument "y" to "f" has incompatible type "int"; expected "str" |
| 43 |
f(x=0, |
| 44 |
y=None) # E:6: Argument "y" to "f" has incompatible type "None"; expected "str" |
| 45 |
g(1, '', 2) # E:6: Argument 2 to "g" has incompatible type "str"; expected "int" |
| 46 |
aaa: str |
| 47 |
h(x=1, y=aaa, z=2) # E:10: Argument "y" to "h" has incompatible type "str"; expected "int" |
| 48 |
a: A |
| 49 |
ff(a.x) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int" |
| 50 |
ff([1]) # E:4: Argument 1 to "ff" has incompatible type "list[int]"; expected "int" |
| 51 |
# TODO: Different column in Python 3.8+ |
| 52 |
#ff([1 for x in [1]]) # Argument 1 to "ff" has incompatible type "list[int]"; expected "int" |
| 53 |
ff({1: 2}) # E:4: Argument 1 to "ff" has incompatible type "dict[int, int]"; expected "int" |
| 54 |
ff(1.1) # E:4: Argument 1 to "ff" has incompatible type "float"; expected "int" |
| 55 |
# TODO: Different column in Python 3.8+ |
| 56 |
#ff( ( 1, 1)) # Argument 1 to "ff" has incompatible type "tuple[int, int]"; expected "int" |
| 57 |
ff(-a) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int" |
| 58 |
ff(a + 1) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int" |
| 59 |
ff(a < 1) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int" |
| 60 |
ff([''][0]) # E:4: Argument 1 to "ff" has incompatible type "str"; expected "int" |
| 61 |
|
| 62 |
class B(A): |
| 63 |
def f(self) -> None: |
| 64 |
ff(super().__neg__()) # E:12: Argument 1 to "ff" has incompatible type "str"; expected "int" |
| 65 |
[builtins fixtures/dict.pyi] |
| 66 |
|
| 67 |
[case testColumnsInvalidArgumentTypeVarArgs] |
| 68 |
def f(*x: int) -> None: pass |
| 69 |
def g(**x: int) -> None: pass |
| 70 |
|
| 71 |
a = [''] |
| 72 |
f(*a) # E:4: Argument 1 to "f" has incompatible type "*list[str]"; expected "int" |
| 73 |
b = {'x': 'y'} |
| 74 |
g(**b) # E:5: Argument 1 to "g" has incompatible type "**dict[str, str]"; expected "int" |
| 75 |
[builtins fixtures/dict.pyi] |
| 76 |
|
| 77 |
[case testColumnsMultipleStatementsPerLine] |
| 78 |
x = 15 |
| 79 |
y = 'hello' |
| 80 |
if int(): |
| 81 |
x = 2; y = x; y += 1 |
| 82 |
[builtins fixtures/primitives.pyi] |
| 83 |
[out] |
| 84 |
main:4:16: error: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 85 |
main:4:24: error: Unsupported operand types for + ("str" and "int") |
| 86 |
|
| 87 |
[case testColumnsAssignment] |
| 88 |
class A: |
| 89 |
x = 0 |
| 90 |
|
| 91 |
A().x = '' # E:9: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 92 |
a = [0] |
| 93 |
a[0] = '' # E:8: Incompatible types in assignment (expression has type "str", target has type "int") |
| 94 |
b = 0 |
| 95 |
c = 0 |
| 96 |
b, c = 0, '' # E:11: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 97 |
b, c = '', 0 # E:8: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 98 |
|
| 99 |
t = 0, '' |
| 100 |
b, c = t # E:8: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 101 |
|
| 102 |
class B(A): |
| 103 |
x = '' # E:9: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int") |
| 104 |
[builtins fixtures/list.pyi] |
| 105 |
|
| 106 |
[case testColumnsAttributeIncompatibleWithBaseClassUsingAnnotation] |
| 107 |
class A: |
| 108 |
x: str |
| 109 |
|
| 110 |
class B(A): |
| 111 |
x: int # E:5: Incompatible types in assignment (expression has type "int", base class "A" defined the type as "str") |
| 112 |
|
| 113 |
[case testColumnsSimpleIsinstance] |
| 114 |
import typing |
| 115 |
def f(x: object, n: int, s: str) -> None: |
| 116 |
if int(): |
| 117 |
n = x # E:13: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 118 |
if isinstance(x, int): |
| 119 |
n = x |
| 120 |
s = x # E:17: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 121 |
n = x # E:13: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 122 |
[builtins fixtures/isinstance.pyi] |
| 123 |
|
| 124 |
[case testColumnHasNoAttribute] |
| 125 |
import m |
| 126 |
if int(): |
| 127 |
from m import foobaz # E:5: Module "m" has no attribute "foobaz"; maybe "foobar"? |
| 128 |
1 .x # E:1: "int" has no attribute "x" |
| 129 |
(m.foobaz()) # E:2: Module has no attribute "foobaz"; maybe "foobar"? |
| 130 |
|
| 131 |
[file m.py] |
| 132 |
def foobar(): pass |
| 133 |
|
| 134 |
[builtins fixtures/module.pyi] |
| 135 |
|
| 136 |
[case testColumnUnexpectedOrMissingKeywordArg] |
| 137 |
def f(): pass |
| 138 |
# TODO: Point to "x" instead |
| 139 |
(f(x=1)) # E:2: Unexpected keyword argument "x" for "f" |
| 140 |
def g(*, x: int) -> None: pass |
| 141 |
(g()) # E:2: Missing named argument "x" for "g" |
| 142 |
|
| 143 |
[case testColumnDefinedHere] |
| 144 |
class A: pass |
| 145 |
if int(): |
| 146 |
def f(a: 'A') -> None: pass |
| 147 |
(f(b=object())) # E:6: Unexpected keyword argument "b" for "f" |
| 148 |
|
| 149 |
[case testColumnInvalidType_no_native_parse] |
| 150 |
|
| 151 |
from typing import Iterable |
| 152 |
|
| 153 |
bad = 0 |
| 154 |
|
| 155 |
def f(x: bad): # E:10: Variable "__main__.bad" is not valid as a type \ |
| 156 |
# N:10: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 157 |
y: bad # E:8: Variable "__main__.bad" is not valid as a type \ |
| 158 |
# N:8: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 159 |
|
| 160 |
if int(): |
| 161 |
def g(x): # E:5: Variable "__main__.bad" is not valid as a type \ |
| 162 |
# N:5: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 163 |
# type: (bad) -> None |
| 164 |
y = 0 # type: bad # E:9: Variable "__main__.bad" is not valid as a type \ |
| 165 |
# N:9: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 166 |
|
| 167 |
z: Iterable[bad] # E:13: Variable "__main__.bad" is not valid as a type \ |
| 168 |
# N:13: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 169 |
h: bad[int] # E:4: Variable "__main__.bad" is not valid as a type \ |
| 170 |
# N:4: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 171 |
|
| 172 |
[case testColumnFunctionMissingTypeAnnotation] |
| 173 |
# flags: --disallow-untyped-defs |
| 174 |
if int(): |
| 175 |
def f(x: int): # E:5: Function is missing a return type annotation |
| 176 |
pass |
| 177 |
|
| 178 |
def g(x): # E:5: Function is missing a type annotation |
| 179 |
pass |
| 180 |
|
| 181 |
[case testColumnNameIsNotDefined] |
| 182 |
((x)) # E:3: Name "x" is not defined |
| 183 |
|
| 184 |
[case testColumnNeedTypeAnnotation] |
| 185 |
if 1: |
| 186 |
x = [] # E:5: Need type annotation for "x" (hint: "x: list[<type>] = ...") |
| 187 |
[builtins fixtures/list.pyi] |
| 188 |
|
| 189 |
[case testColumnCallToUntypedFunction] |
| 190 |
# flags: --disallow-untyped-calls |
| 191 |
def f() -> None: |
| 192 |
(g(1)) # E:6: Call to untyped function "g" in typed context |
| 193 |
|
| 194 |
def g(x): |
| 195 |
pass |
| 196 |
|
| 197 |
[case testColumnInvalidArguments] |
| 198 |
def f(x, y): pass |
| 199 |
(f()) # E:2: Missing positional arguments "x", "y" in call to "f" |
| 200 |
(f(y=1)) # E:2: Missing positional argument "x" in call to "f" |
| 201 |
|
| 202 |
[case testColumnListOrDictItemHasIncompatibleType] |
| 203 |
from typing import List, Dict |
| 204 |
x: List[int] = [ |
| 205 |
'x', # E:5: List item 0 has incompatible type "str"; expected "int" |
| 206 |
1.1] # E:7: List item 1 has incompatible type "float"; expected "int" |
| 207 |
y: Dict[int, int] = { |
| 208 |
'x': 1 # E:5: Dict entry 0 has incompatible type "str": "int"; expected "int": "int" |
| 209 |
} |
| 210 |
[builtins fixtures/dict.pyi] |
| 211 |
|
| 212 |
[case testColumnCannotDetermineType] |
| 213 |
# flags: --no-local-partial-types |
| 214 |
(x) # E:2: Cannot determine type of "x" # E:2: Name "x" is used before definition |
| 215 |
x = None |
| 216 |
|
| 217 |
[case testColumnInvalidIndexing] |
| 218 |
from typing import List |
| 219 |
([1]['']) # E:6: Invalid index type "str" for "list[int]"; expected type "int" |
| 220 |
(1[1]) # E:2: Value of type "int" is not indexable |
| 221 |
def f() -> None: |
| 222 |
1[1] = 1 # E:5: Unsupported target for indexed assignment ("int") |
| 223 |
[builtins fixtures/list.pyi] |
| 224 |
|
| 225 |
[case testColumnTypedDict] |
| 226 |
from typing import TypedDict |
| 227 |
class D(TypedDict): |
| 228 |
x: int |
| 229 |
t: D = {'x': |
| 230 |
'y'} # E:5: Incompatible types (expression has type "str", TypedDict item "x" has type "int") |
| 231 |
s: str |
| 232 |
|
| 233 |
if int(): |
| 234 |
del t[s] # E:11: Expected TypedDict key to be string literal |
| 235 |
del t["x"] # E:11: Key "x" of TypedDict "D" cannot be deleted |
| 236 |
del t["y"] # E:11: TypedDict "D" has no key "y" |
| 237 |
|
| 238 |
t.pop(s) # E:7: Expected TypedDict key to be string literal |
| 239 |
t.pop("y") # E:7: TypedDict "D" has no key "y" |
| 240 |
|
| 241 |
t.setdefault(s, 123) # E:14: Expected TypedDict key to be string literal |
| 242 |
t.setdefault("x", "a") # E:19: Argument 2 to "setdefault" of "TypedDict" has incompatible type "str"; expected "int" |
| 243 |
t.setdefault("y", 123) # E:14: TypedDict "D" has no key "y" |
| 244 |
[builtins fixtures/dict.pyi] |
| 245 |
[typing fixtures/typing-typeddict.pyi] |
| 246 |
|
| 247 |
[case testColumnSignatureIncompatibleWithSuperType] |
| 248 |
class A: |
| 249 |
def f(self, x: int) -> None: pass |
| 250 |
class B(A): |
| 251 |
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" \ |
| 252 |
# N:17: This violates the Liskov substitution principle \ |
| 253 |
# N:17: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 254 |
class C(A): |
| 255 |
def f(self, x: int) -> int: pass # E:5: Return type "int" of "f" incompatible with return type "None" in supertype "A" |
| 256 |
class D(A): |
| 257 |
def f(self) -> None: pass # E:5: Signature of "f" incompatible with supertype "A" \ |
| 258 |
# N:5: Superclass: \ |
| 259 |
# N:5: def f(self, x: int) -> None \ |
| 260 |
# N:5: Subclass: \ |
| 261 |
# N:5: def f(self) -> None |
| 262 |
|
| 263 |
[case testColumnMissingTypeParameters] |
| 264 |
# flags: --disallow-any-generics |
| 265 |
from typing import List, Callable |
| 266 |
def f(x: List) -> None: pass # E:10: Missing type arguments for generic type "List" |
| 267 |
def g(x: list) -> None: pass # E:10: Missing type arguments for generic type "list" |
| 268 |
if int(): |
| 269 |
c: Callable # E:8: Missing type arguments for generic type "Callable" |
| 270 |
[builtins fixtures/list.pyi] |
| 271 |
|
| 272 |
[case testColumnIncompatibleDefault] |
| 273 |
if int(): |
| 274 |
def f(x: int = '') -> None: # E:20: Incompatible default for parameter "x" (default has type "str", parameter has type "int") |
| 275 |
pass |
| 276 |
|
| 277 |
[case testColumnMissingProtocolMember] |
| 278 |
from typing import Protocol |
| 279 |
|
| 280 |
class P(Protocol): |
| 281 |
x: int |
| 282 |
y: int |
| 283 |
|
| 284 |
class C: |
| 285 |
x: int |
| 286 |
|
| 287 |
p: P |
| 288 |
if int(): |
| 289 |
p = C() # E:9: Incompatible types in assignment (expression has type "C", variable has type "P") \ |
| 290 |
# N:9: "C" is missing following "P" protocol member: \ |
| 291 |
# N:9: y |
| 292 |
|
| 293 |
[case testColumnRedundantCast] |
| 294 |
# flags: --warn-redundant-casts |
| 295 |
from typing import cast |
| 296 |
y = 1 |
| 297 |
x = cast(int, y) # E:5: Redundant cast to "int" |
| 298 |
|
| 299 |
[case testColumnTypeSignatureHasTooFewArguments] |
| 300 |
if int(): |
| 301 |
def f(x, y): # E:5: Type signature has too few parameters |
| 302 |
# type: (int) -> None |
| 303 |
pass |
| 304 |
|
| 305 |
[case testColumnRevealedType] |
| 306 |
if int(): |
| 307 |
reveal_type(1) # N:17: Revealed type is "Literal[1]?" |
| 308 |
|
| 309 |
[case testColumnNonOverlappingEqualityCheck] |
| 310 |
# flags: --strict-equality |
| 311 |
if 1 == '': # E:4: Non-overlapping equality check (left operand type: "Literal[1]", right operand type: "Literal['']") |
| 312 |
pass |
| 313 |
[builtins fixtures/bool.pyi] |
| 314 |
|
| 315 |
[case testColumnValueOfTypeVariableCannotBe] |
| 316 |
|
| 317 |
from typing import TypeVar, Generic |
| 318 |
|
| 319 |
T = TypeVar('T', int, str) |
| 320 |
|
| 321 |
class C(Generic[T]): |
| 322 |
pass |
| 323 |
|
| 324 |
def f(c: C[object]) -> None: pass # E:12: Value of type variable "T" of "C" cannot be "object" |
| 325 |
(C[object]()) # E:2: Value of type variable "T" of "C" cannot be "object" |
| 326 |
|
| 327 |
[case testColumnInvalidLocationForParamSpec] |
| 328 |
from typing import List |
| 329 |
from typing_extensions import ParamSpec |
| 330 |
|
| 331 |
P = ParamSpec('P') |
| 332 |
def foo(x: List[P]): pass # E:17: Invalid location for ParamSpec "P" \ |
| 333 |
# N:17: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]" |
| 334 |
[builtins fixtures/list.pyi] |
| 335 |
|
| 336 |
[case testColumnSyntaxErrorInTypeAnnotation] |
| 337 |
if int(): |
| 338 |
def f(x # type: int, |
| 339 |
): |
| 340 |
pass |
| 341 |
[out] |
| 342 |
main:2:11: error: Syntax error in type annotation |
| 343 |
main:2:11: note: Suggestion: Is there a spurious trailing comma? |
| 344 |
|
| 345 |
[case testColumnSyntaxErrorInTypeAnnotation2_no_native_parse] |
| 346 |
if int(): |
| 347 |
# TODO: It would be better to point to the type comment |
| 348 |
xyz = 0 # type: blurbnard blarb |
| 349 |
[out] |
| 350 |
main:3:5: error: Syntax error in type comment "blurbnard blarb" |
| 351 |
|
| 352 |
[case testColumnProperty] |
| 353 |
class A: |
| 354 |
@property |
| 355 |
def x(self) -> int: pass |
| 356 |
|
| 357 |
@x.setter |
| 358 |
def x(self, x: int) -> None: pass |
| 359 |
|
| 360 |
class B(A): |
| 361 |
@property # E:6: Read-only property cannot override read-write property |
| 362 |
def x(self) -> int: pass |
| 363 |
[builtins fixtures/property.pyi] |
| 364 |
|
| 365 |
[case testColumnOverloaded] |
| 366 |
from typing import overload, Any |
| 367 |
class A: |
| 368 |
@overload # E:6: An overloaded function outside a stub file must have an implementation |
| 369 |
def f(self, x: int) -> int: pass |
| 370 |
@overload |
| 371 |
def f(self, x: str) -> str: pass |
| 372 |
|
| 373 |
[case testColumnFunctionWithTypeVarValues] |
| 374 |
from typing import TypeVar, List |
| 375 |
|
| 376 |
T = TypeVar('T', int, str) |
| 377 |
|
| 378 |
def g(x): pass |
| 379 |
|
| 380 |
def f(x: T) -> T: |
| 381 |
(x.bad) # E:6: "int" has no attribute "bad" \ |
| 382 |
# E:6: "str" has no attribute "bad" |
| 383 |
g(y=x) # E:5: Unexpected keyword argument "y" for "g" |
| 384 |
y: List[int, str] # E:8: "list" expects 1 type argument, but 2 given |
| 385 |
del 1[0] # E:5: "int" has no attribute "__delitem__" |
| 386 |
bb: List[int] = [''] # E:22: List item 0 has incompatible type "str"; expected "int" |
| 387 |
aa: List[int] = ['' for x in [1]] # E:22: List comprehension has incompatible type List[str]; expected List[int] |
| 388 |
cc = 1 .bad # E:10: "int" has no attribute "bad" |
| 389 |
n: int = '' # E:14: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 390 |
return x |
| 391 |
[builtins fixtures/list.pyi] |
| 392 |
|
| 393 |
[case testColumnReturnValueExpected] |
| 394 |
def f() -> int: |
| 395 |
return # E:5: Return value expected |
| 396 |
|
| 397 |
[case testCheckEndColumnPositions] |
| 398 |
# flags: --show-error-end |
| 399 |
x: int = "no way" |
| 400 |
|
| 401 |
def g() -> int: ... |
| 402 |
def f(x: str) -> None: ... |
| 403 |
f(g( |
| 404 |
)) |
| 405 |
x[0] |
| 406 |
[out] |
| 407 |
main:2:10:2:17: error: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 408 |
main:6:3:7:1: error: Argument 1 to "f" has incompatible type "int"; expected "str" |
| 409 |
main:8:1:8:4: error: Value of type "int" is not indexable |
| 410 |
|
| 411 |
[case testColumnsMissingPositionalArgShiftDetected] |
| 412 |
def f(x: int, y: str, z: bytes, aa: int) -> None: ... |
| 413 |
f(1, b'x', 1) # E:6: Missing positional argument "y" in call to "f" |
| 414 |
def g(x: int, y: str, z: bytes) -> None: ... |
| 415 |
g("hello", b'x') # E:3: Missing positional argument "x" in call to "g" |
| 416 |
g(1, "hello") # E:1: Missing positional argument "z" in call to "g" |
| 417 |
[builtins fixtures/primitives.pyi] |
| 418 |
|
| 419 |
[case testEndColumnsWithTooManyTypeVars] |
| 420 |
# flags: --pretty |
| 421 |
import typing |
| 422 |
|
| 423 |
x1: typing.List[typing.List[int, int]] |
| 424 |
x2: list[list[int, int]] |
| 425 |
[out] |
| 426 |
main:4:17: error: "list" expects 1 type argument, but 2 given |
| 427 |
x1: typing.List[typing.List[int, int]] |
| 428 |
^~~~~~~~~~~~~~~~~~~~~ |
| 429 |
main:5:10: error: "list" expects 1 type argument, but 2 given |
| 430 |
x2: list[list[int, int]] |
| 431 |
^~~~~~~~~~~~~~ |