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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
[case testEmptyFile]
[out]
[case testAssignmentAndVarDef]
a: A
b: 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]
class A:
def __init__(self): pass
class B:
def __init__(self): pass
x: A
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testInheritInitFromObject]
class A(object): pass
class B(object): pass
x: A
if int():
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testImplicitInheritInitFromObject]
class A: pass
class B: pass
x: A
o: 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
[case testTooManyConstructorArgs]
import typing
object(object())
[out]
main:2: error: Too many arguments for "object"
[case testVarDefWithInit]
import typing
class A: pass
a = A() # type: A
b = object() # type: A # E: Incompatible types in assignment (expression has type "object", variable has type "A")
[case testInheritanceBasedSubtyping]
import typing
class A: pass
class B(A): pass
x = B() # type: A
y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
[case testDeclaredVariableInParentheses]
(x) = 2 # 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
class A: pass
class B: pass
def f(x: 'A') -> None: pass
f(A())
f(B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
[case testNotCallable]
import typing
class A: pass
A()() # E: "A" not callable
[case testSubtypeArgument]
import typing
class A: pass
class B(A): pass
def f(x: 'A', y: 'B') -> None: pass
f(B(), A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
f(B(), 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: A
y: 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
class A: pass
class B(A): pass
x = B() # type: A
y = A() # type: B # E: 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
class A: pass
class B: pass
def f() -> 'A':
return B() # E: Incompatible return value type (got "B", expected "A")
a = B() # type: A # E: 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 testModuleImplicitAttributes]
import typing
class A: pass
reveal_type(__name__) # N: Revealed type is "builtins.str"
reveal_type(__doc__) # N: Revealed type is "builtins.str"
reveal_type(__file__) # N: Revealed type is "builtins.str"
reveal_type(__package__) # N: Revealed type is "builtins.str"
reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]"
# This will actually reveal Union[importlib.machinery.ModuleSpec, None]
reveal_type(__spec__) # N: Revealed type is "Union[builtins.object, None]"
import module
reveal_type(module.__name__) # N: Revealed type is "builtins.str"
# This will actually reveal importlib.machinery.ModuleSpec
reveal_type(module.__spec__) # N: Revealed type is "builtins.object"
[file module.py]
[builtins fixtures/primitives.pyi]
-- Scoping and shadowing
-- ---------------------
[case testLocalVariableShadowing]
class A: pass
class B: pass
a: A
if int():
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = A()
def f() -> None:
a: 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()
[case testGlobalDefinedInBlockWithType]
class A: pass
while 1:
a: 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]
x = 1
x in 1, # E: Unsupported right operand type for in ("int")
[builtins fixtures/tuple.pyi]
[case testTrailingCommaInIfParsing]
if x in 1, : pass
[out]
main:1: error: Invalid syntax
[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 \
# N: Perhaps you meant to use a protocol matching the module structure?
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 \
# N: Perhaps you meant to use a protocol matching the module structure?
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 "Literal[False]"
[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 https://mypy.readthedocs.io/en/stable/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 https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Mapping" instead, which is covariant in the value type
[builtins fixtures/dict.pyi]
[case testDistinctTypes]
import b
[file a.py]
from typing import NamedTuple, 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 Final, List, Literal, Optional, Union, Sequence, NamedTuple, Tuple, Type, 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]
[typing fixtures/typing-full.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: ...
[case testInlineAssertions]
import a, b
s1: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[file a.py]
s2: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[file b.py]
s3: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[file c.py]
s3: str = 'foo'
[case testMultilineQuotedAnnotation]
x: """
int |
str
"""
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
y: """(
int |
str
)
"""
reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]"
|