| 1 |
-- Test cases for the type checker related to functions, function types and |
| 2 |
-- calls. |
| 3 |
|
| 4 |
-- See also check-varargs.test. |
| 5 |
|
| 6 |
|
| 7 |
-- Callable type basics |
| 8 |
-- -------------------- |
| 9 |
|
| 10 |
|
| 11 |
[case testCallingVariableWithFunctionType] |
| 12 |
from typing import Callable |
| 13 |
f: Callable[[A], B] |
| 14 |
a: A |
| 15 |
b: B |
| 16 |
if int(): |
| 17 |
a = f(a) # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 18 |
if int(): |
| 19 |
b = f(b) # E: Argument 1 has incompatible type "B"; expected "A" |
| 20 |
if int(): |
| 21 |
b = f() # E: Too few arguments |
| 22 |
if int(): |
| 23 |
b = f(a, a) # E: Too many arguments |
| 24 |
if int(): |
| 25 |
b = f(a) |
| 26 |
|
| 27 |
class A: pass |
| 28 |
class B: pass |
| 29 |
[builtins fixtures/tuple.pyi] |
| 30 |
|
| 31 |
[case testKeywordOnlyArgumentOrderInsensitivity] |
| 32 |
import typing |
| 33 |
|
| 34 |
class A(object): |
| 35 |
def f(self, *, a: int, b: str) -> None: pass |
| 36 |
|
| 37 |
class B(A): |
| 38 |
def f(self, *, b: str, a: int) -> None: pass |
| 39 |
|
| 40 |
class C(A): |
| 41 |
def f(self, *, b: int, a: str) -> None: pass # Fail |
| 42 |
[out] |
| 43 |
main:10: error: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "str" |
| 44 |
main:10: note: This violates the Liskov substitution principle |
| 45 |
main:10: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 46 |
main:10: error: Argument 2 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" |
| 47 |
|
| 48 |
[case testPositionalOverridingArgumentNameInsensitivity] |
| 49 |
import typing |
| 50 |
|
| 51 |
class A(object): |
| 52 |
def f(self, a: int, b: str) -> None: pass |
| 53 |
|
| 54 |
class B(A): |
| 55 |
def f(self, b: str, a: int) -> None: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \ |
| 56 |
# N: This violates the Liskov substitution principle \ |
| 57 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides \ |
| 58 |
# E: Argument 2 of "f" is incompatible with supertype "A"; supertype defines the argument type as "str" |
| 59 |
|
| 60 |
class C(A): |
| 61 |
def f(self, foo: int, bar: str) -> None: pass |
| 62 |
|
| 63 |
|
| 64 |
[case testPositionalOverridingArgumentNamesCheckedWhenMismatchingPos] |
| 65 |
import typing |
| 66 |
|
| 67 |
class A(object): |
| 68 |
def f(self, a: int, b: str) -> None: pass |
| 69 |
|
| 70 |
class B(A): |
| 71 |
def f(self, b: int, a: str) -> None: pass # Fail |
| 72 |
[out] |
| 73 |
main:7: error: Signature of "f" incompatible with supertype "A" |
| 74 |
main:7: note: Superclass: |
| 75 |
main:7: note: def f(self, a: int, b: str) -> None |
| 76 |
main:7: note: Subclass: |
| 77 |
main:7: note: def f(self, b: int, a: str) -> None |
| 78 |
|
| 79 |
[case testSubtypingFunctionTypes] |
| 80 |
from typing import Callable |
| 81 |
|
| 82 |
class A: pass |
| 83 |
class B(A): pass |
| 84 |
|
| 85 |
f: Callable[[B], A] |
| 86 |
g: Callable[[A], A] # subtype of f |
| 87 |
h: Callable[[B], B] # subtype of f |
| 88 |
if int(): |
| 89 |
g = h # E: Incompatible types in assignment (expression has type "Callable[[B], B]", variable has type "Callable[[A], A]") |
| 90 |
if int(): |
| 91 |
h = f # E: Incompatible types in assignment (expression has type "Callable[[B], A]", variable has type "Callable[[B], B]") |
| 92 |
if int(): |
| 93 |
h = g # E: Incompatible types in assignment (expression has type "Callable[[A], A]", variable has type "Callable[[B], B]") |
| 94 |
if int(): |
| 95 |
g = f # E: Incompatible types in assignment (expression has type "Callable[[B], A]", variable has type "Callable[[A], A]") |
| 96 |
if int(): |
| 97 |
f = g |
| 98 |
if int(): |
| 99 |
f = h |
| 100 |
if int(): |
| 101 |
f = f |
| 102 |
if int(): |
| 103 |
g = g |
| 104 |
if int(): |
| 105 |
h = h |
| 106 |
|
| 107 |
[case testSubtypingFunctionsDoubleCorrespondence] |
| 108 |
def l(x) -> None: ... |
| 109 |
def r(__x, *, x) -> None: ... |
| 110 |
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(Any, /, *, x: Any) -> None") |
| 111 |
|
| 112 |
[case testSubtypingFunctionsDoubleCorrespondenceNamedOptional] |
| 113 |
def l(x) -> None: ... |
| 114 |
def r(__x, *, x = 1) -> None: ... |
| 115 |
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(Any, /, *, x: Any = ...) -> None") |
| 116 |
|
| 117 |
[case testSubtypingFunctionsDoubleCorrespondenceBothNamedOptional] |
| 118 |
def l(x = 1) -> None: ... |
| 119 |
def r(__x, *, x = 1) -> None: ... |
| 120 |
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(Any, /, *, x: Any = ...) -> None") |
| 121 |
|
| 122 |
[case testSubtypingFunctionsTrivialSuffixRequired] |
| 123 |
def l(__x) -> None: ... |
| 124 |
def r(x, *args, **kwargs) -> None: ... |
| 125 |
|
| 126 |
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "def r(x: Any, *args: Any, **kwargs: Any) -> None") |
| 127 |
[builtins fixtures/dict.pyi] |
| 128 |
|
| 129 |
[case testSubtypingFunctionsTrivialSuffixOptional] |
| 130 |
def l(__x = 1) -> None: ... |
| 131 |
def r(x = 1, *args, **kwargs) -> None: ... |
| 132 |
|
| 133 |
r = l # E: Incompatible types in assignment (expression has type "def l(Any = ..., /) -> None", variable has type "def r(x: Any = ..., *args: Any, **kwargs: Any) -> None") |
| 134 |
[builtins fixtures/dict.pyi] |
| 135 |
|
| 136 |
[case testSubtypingFunctionsRequiredLeftArgNotPresent] |
| 137 |
def l(x, y) -> None: ... |
| 138 |
def r(x) -> None: ... |
| 139 |
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any, Any], None]", variable has type "Callable[[Any], None]") |
| 140 |
|
| 141 |
[case testSubtypingFunctionsImplicitNames] |
| 142 |
from typing import Any |
| 143 |
|
| 144 |
def f(a, b): pass |
| 145 |
def g(c: Any, d: Any) -> Any: pass |
| 146 |
|
| 147 |
ff = f |
| 148 |
gg = g |
| 149 |
|
| 150 |
gg = f |
| 151 |
ff = g |
| 152 |
|
| 153 |
[case testSubtypingFunctionsDefaultsNames] |
| 154 |
from typing import Callable |
| 155 |
|
| 156 |
def f(a: int, b: str) -> None: pass |
| 157 |
f_nonames: Callable[[int, str], None] |
| 158 |
def g(a: int, b: str = "") -> None: pass |
| 159 |
def h(aa: int, b: str = "") -> None: pass |
| 160 |
|
| 161 |
ff_nonames = f_nonames |
| 162 |
ff = f |
| 163 |
gg = g |
| 164 |
hh = h |
| 165 |
|
| 166 |
if int(): |
| 167 |
ff = gg |
| 168 |
if int(): |
| 169 |
ff_nonames = ff |
| 170 |
if int(): |
| 171 |
ff_nonames = f_nonames # reset |
| 172 |
if int(): |
| 173 |
ff = ff_nonames # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "def f(a: int, b: str) -> None") |
| 174 |
if int(): |
| 175 |
ff = f # reset |
| 176 |
if int(): |
| 177 |
gg = ff # E: Incompatible types in assignment (expression has type "def f(a: int, b: str) -> None", variable has type "def g(a: int, b: str = ...) -> None") |
| 178 |
if int(): |
| 179 |
gg = hh # E: Incompatible types in assignment (expression has type "def h(aa: int, b: str = ...) -> None", variable has type "def g(a: int, b: str = ...) -> None") |
| 180 |
|
| 181 |
[case testSubtypingFunctionsArgsKwargs] |
| 182 |
from typing import Any, Callable |
| 183 |
|
| 184 |
def everything(*args: Any, **kwargs: Any) -> None: pass |
| 185 |
everywhere: Callable[..., None] |
| 186 |
|
| 187 |
def specific_1(a: int, b: str) -> None: pass |
| 188 |
def specific_2(a: int, *, b: str) -> None: pass |
| 189 |
|
| 190 |
ss_1 = specific_1 |
| 191 |
ss_2 = specific_2 |
| 192 |
ee_def = everything |
| 193 |
ee_var = everywhere |
| 194 |
|
| 195 |
if int(): |
| 196 |
ss_1 = ee_def |
| 197 |
if int(): |
| 198 |
ss_1 = specific_1 |
| 199 |
if int(): |
| 200 |
ss_2 = ee_def |
| 201 |
if int(): |
| 202 |
ss_2 = specific_2 |
| 203 |
if int(): |
| 204 |
ee_def = everywhere |
| 205 |
if int(): |
| 206 |
ee_def = everything |
| 207 |
if int(): |
| 208 |
ee_var = everything |
| 209 |
if int(): |
| 210 |
ee_var = everywhere |
| 211 |
|
| 212 |
if int(): |
| 213 |
ee_var = specific_1 |
| 214 |
if int(): |
| 215 |
ee_def = specific_1 |
| 216 |
|
| 217 |
[builtins fixtures/dict.pyi] |
| 218 |
|
| 219 |
[case testSubtypingFunctionsDecorated] |
| 220 |
from typing import Any |
| 221 |
|
| 222 |
# untyped decorator |
| 223 |
def deco(f): pass |
| 224 |
|
| 225 |
class A: |
| 226 |
@deco |
| 227 |
def f(self) -> Any: |
| 228 |
pass |
| 229 |
|
| 230 |
class B(A): |
| 231 |
@deco |
| 232 |
def f(self) -> Any: |
| 233 |
pass |
| 234 |
|
| 235 |
[builtins fixtures/list.pyi] |
| 236 |
|
| 237 |
|
| 238 |
[case testLackOfNames] |
| 239 |
def f(__a: int, __b: str) -> None: pass |
| 240 |
def g(a: int, b: str) -> None: pass |
| 241 |
|
| 242 |
ff = f |
| 243 |
gg = g |
| 244 |
|
| 245 |
if int(): |
| 246 |
ff = g |
| 247 |
if int(): |
| 248 |
gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "def g(a: int, b: str) -> None") |
| 249 |
|
| 250 |
[case testLackOfNamesFastparse] |
| 251 |
def f(__a: int, __b: str) -> None: pass |
| 252 |
def g(a: int, b: str) -> None: pass |
| 253 |
|
| 254 |
ff = f |
| 255 |
gg = g |
| 256 |
|
| 257 |
if int(): |
| 258 |
ff = g |
| 259 |
if int(): |
| 260 |
gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "def g(a: int, b: str) -> None") |
| 261 |
|
| 262 |
[case testFunctionTypeCompatibilityWithOtherTypes] |
| 263 |
# flags: --no-strict-optional |
| 264 |
from typing import Callable |
| 265 |
f = None # type: Callable[[], None] |
| 266 |
a, o = None, None # type: (A, object) |
| 267 |
if int(): |
| 268 |
a = f # E: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "A") |
| 269 |
if int(): |
| 270 |
f = a # E: Incompatible types in assignment (expression has type "A", variable has type "Callable[[], None]") |
| 271 |
if int(): |
| 272 |
f = o # E: Incompatible types in assignment (expression has type "object", variable has type "Callable[[], None]") |
| 273 |
if int(): |
| 274 |
f = f() # E: Function does not return a value (it only ever returns None) |
| 275 |
|
| 276 |
if int(): |
| 277 |
f = f |
| 278 |
if int(): |
| 279 |
f = None |
| 280 |
if int(): |
| 281 |
o = f |
| 282 |
|
| 283 |
class A: pass |
| 284 |
[builtins fixtures/tuple.pyi] |
| 285 |
|
| 286 |
[case testReturnEmptyTuple] |
| 287 |
from typing import Tuple |
| 288 |
def f(x): # type: (int) -> () # E: Syntax error in type annotation \ |
| 289 |
# N: Suggestion: Use Tuple[()] instead of () for an empty tuple, or None for a function without a return value |
| 290 |
pass |
| 291 |
|
| 292 |
def g(x: int) -> Tuple[()]: |
| 293 |
pass |
| 294 |
[builtins fixtures/tuple.pyi] |
| 295 |
|
| 296 |
[case testFunctionSubtypingWithVoid] |
| 297 |
from typing import Callable |
| 298 |
f: Callable[[], None] |
| 299 |
g: Callable[[], object] |
| 300 |
if int(): |
| 301 |
f = g # E: Incompatible types in assignment (expression has type "Callable[[], object]", variable has type "Callable[[], None]") |
| 302 |
if int(): |
| 303 |
g = f # OK |
| 304 |
|
| 305 |
if int(): |
| 306 |
f = f |
| 307 |
if int(): |
| 308 |
g = g |
| 309 |
|
| 310 |
[case testFunctionSubtypingWithMultipleArgs] |
| 311 |
from typing import Callable |
| 312 |
f: Callable[[A, A], None] |
| 313 |
g: Callable[[A, B], None] |
| 314 |
h: Callable[[B, B], None] |
| 315 |
if int(): |
| 316 |
f = g # E: Incompatible types in assignment (expression has type "Callable[[A, B], None]", variable has type "Callable[[A, A], None]") |
| 317 |
if int(): |
| 318 |
f = h # E: Incompatible types in assignment (expression has type "Callable[[B, B], None]", variable has type "Callable[[A, A], None]") |
| 319 |
if int(): |
| 320 |
g = h # E: Incompatible types in assignment (expression has type "Callable[[B, B], None]", variable has type "Callable[[A, B], None]") |
| 321 |
if int(): |
| 322 |
g = f |
| 323 |
if int(): |
| 324 |
h = f |
| 325 |
if int(): |
| 326 |
h = g |
| 327 |
if int(): |
| 328 |
f = f |
| 329 |
if int(): |
| 330 |
g = g |
| 331 |
if int(): |
| 332 |
h = h |
| 333 |
|
| 334 |
class A: pass |
| 335 |
class B(A): pass |
| 336 |
|
| 337 |
[case testFunctionTypesWithDifferentArgumentCounts] |
| 338 |
from typing import Callable |
| 339 |
f: Callable[[], None] |
| 340 |
g: Callable[[A], None] |
| 341 |
h: Callable[[A, A], None] |
| 342 |
|
| 343 |
if int(): |
| 344 |
f = g # E: Incompatible types in assignment (expression has type "Callable[[A], None]", variable has type "Callable[[], None]") |
| 345 |
if int(): |
| 346 |
f = h # E: Incompatible types in assignment (expression has type "Callable[[A, A], None]", variable has type "Callable[[], None]") |
| 347 |
if int(): |
| 348 |
h = f # E: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "Callable[[A, A], None]") |
| 349 |
if int(): |
| 350 |
h = g # E: Incompatible types in assignment (expression has type "Callable[[A], None]", variable has type "Callable[[A, A], None]") |
| 351 |
|
| 352 |
if int(): |
| 353 |
f = f |
| 354 |
if int(): |
| 355 |
g = g |
| 356 |
if int(): |
| 357 |
h = h |
| 358 |
|
| 359 |
class A: pass |
| 360 |
[out] |
| 361 |
|
| 362 |
[case testCompatibilityOfSimpleTypeObjectWithStdType] |
| 363 |
class A: |
| 364 |
def __init__(self, a: 'A') -> None: pass |
| 365 |
|
| 366 |
def f() -> None: pass |
| 367 |
|
| 368 |
t: type |
| 369 |
a: A |
| 370 |
|
| 371 |
if int(): |
| 372 |
a = A # E: Incompatible types in assignment (expression has type "type[A]", variable has type "A") |
| 373 |
if int(): |
| 374 |
t = f # E: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "type") |
| 375 |
if int(): |
| 376 |
t = A |
| 377 |
|
| 378 |
[case testFunctionTypesWithOverloads] |
| 379 |
from foo import * |
| 380 |
[file foo.pyi] |
| 381 |
from typing import Callable, overload |
| 382 |
f: Callable[[AA], A] |
| 383 |
g: Callable[[B], B] |
| 384 |
h: Callable[[A], AA] |
| 385 |
|
| 386 |
if int(): |
| 387 |
h = i # E: Incompatible types in assignment (expression has type overloaded function, variable has type "Callable[[A], AA]") |
| 388 |
if int(): |
| 389 |
f = j |
| 390 |
|
| 391 |
if int(): |
| 392 |
f = i |
| 393 |
if int(): |
| 394 |
g = i |
| 395 |
if int(): |
| 396 |
g = j |
| 397 |
|
| 398 |
class A: pass |
| 399 |
class AA(A): pass |
| 400 |
|
| 401 |
class B: pass |
| 402 |
|
| 403 |
@overload |
| 404 |
def i(x: AA) -> A: |
| 405 |
pass |
| 406 |
@overload |
| 407 |
def i(x: B) -> B: |
| 408 |
pass |
| 409 |
|
| 410 |
@overload |
| 411 |
def j(x: B) -> B: |
| 412 |
pass |
| 413 |
@overload |
| 414 |
def j(x: A) -> AA: |
| 415 |
pass |
| 416 |
|
| 417 |
[case testOverloadWithThreeItems] |
| 418 |
from foo import * |
| 419 |
[file foo.pyi] |
| 420 |
from typing import Callable, overload |
| 421 |
g1: Callable[[A], A] |
| 422 |
g2: Callable[[B], B] |
| 423 |
g3: Callable[[C], C] |
| 424 |
g4: Callable[[A], B] |
| 425 |
a: A |
| 426 |
b: B |
| 427 |
c: C |
| 428 |
|
| 429 |
if int(): |
| 430 |
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 431 |
if int(): |
| 432 |
a = f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 433 |
if int(): |
| 434 |
b = f(c) # E: Incompatible types in assignment (expression has type "C", variable has type "B") |
| 435 |
if int(): |
| 436 |
g4 = f # E: Incompatible types in assignment (expression has type overloaded function, variable has type "Callable[[A], B]") |
| 437 |
|
| 438 |
if int(): |
| 439 |
g1 = f |
| 440 |
if int(): |
| 441 |
g2 = f |
| 442 |
if int(): |
| 443 |
g3 = f |
| 444 |
if int(): |
| 445 |
a = f(a) |
| 446 |
if int(): |
| 447 |
b = f(b) |
| 448 |
if int(): |
| 449 |
c = f(c) |
| 450 |
|
| 451 |
class A: pass |
| 452 |
class B: pass |
| 453 |
class C: pass |
| 454 |
|
| 455 |
@overload |
| 456 |
def f(x: A) -> A: pass |
| 457 |
@overload |
| 458 |
def f(x: B) -> B: pass |
| 459 |
@overload |
| 460 |
def f(x: C) -> C: pass |
| 461 |
[builtins fixtures/tuple.pyi] |
| 462 |
|
| 463 |
[case testInferConstraintsUnequalLengths] |
| 464 |
from typing import Any, Callable, List |
| 465 |
def f(fields: List[Callable[[Any], Any]]): pass |
| 466 |
class C: pass |
| 467 |
f([C]) # E: List item 0 has incompatible type "type[C]"; expected "Callable[[Any], Any]" |
| 468 |
class D: |
| 469 |
def __init__(self, a, b): pass |
| 470 |
f([D]) # E: List item 0 has incompatible type "type[D]"; expected "Callable[[Any], Any]" |
| 471 |
[builtins fixtures/list.pyi] |
| 472 |
|
| 473 |
[case testSubtypingTypeTypeAsCallable] |
| 474 |
from typing import Callable, Type |
| 475 |
class A: pass |
| 476 |
x: Callable[..., A] |
| 477 |
y: Type[A] |
| 478 |
x = y |
| 479 |
|
| 480 |
[case testSubtypingCallableAsTypeType] |
| 481 |
from typing import Callable, Type |
| 482 |
class A: pass |
| 483 |
x: Callable[..., A] |
| 484 |
y: Type[A] |
| 485 |
if int(): |
| 486 |
y = x # E: Incompatible types in assignment (expression has type "Callable[..., A]", variable has type "type[A]") |
| 487 |
|
| 488 |
-- Default argument values |
| 489 |
-- ----------------------- |
| 490 |
|
| 491 |
|
| 492 |
[case testCallingFunctionsWithDefaultArgumentValues] |
| 493 |
# flags: --implicit-optional --no-strict-optional |
| 494 |
class A: pass |
| 495 |
class AA(A): pass |
| 496 |
class B: pass |
| 497 |
|
| 498 |
def f(x: 'A' = None) -> 'B': pass |
| 499 |
|
| 500 |
a, b = None, None # type: (A, B) |
| 501 |
if int(): |
| 502 |
a = f() # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 503 |
if int(): |
| 504 |
b = f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "A | None" |
| 505 |
if int(): |
| 506 |
b = f(a, a) # E: Too many arguments for "f" |
| 507 |
|
| 508 |
if int(): |
| 509 |
b = f() |
| 510 |
if int(): |
| 511 |
b = f(a) |
| 512 |
if int(): |
| 513 |
b = f(AA()) |
| 514 |
|
| 515 |
[builtins fixtures/tuple.pyi] |
| 516 |
|
| 517 |
[case testDefaultParameterExpressions] |
| 518 |
import typing |
| 519 |
class B: pass |
| 520 |
class A: pass |
| 521 |
|
| 522 |
def f(x: 'A' = A()) -> None: |
| 523 |
b = x # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 524 |
a = x # type: A |
| 525 |
[out] |
| 526 |
|
| 527 |
[case testDefaultParameterExpressions2] |
| 528 |
import typing |
| 529 |
class B: pass |
| 530 |
class A: pass |
| 531 |
|
| 532 |
def f(x: 'A' = B()) -> None: # E: Incompatible default for parameter "x" (default has type "B", parameter has type "A") |
| 533 |
b = x # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 534 |
a = x # type: A |
| 535 |
[case testDefaultParameterExpressionsGeneric] |
| 536 |
from typing import TypeVar |
| 537 |
T = TypeVar('T', bound='A') |
| 538 |
|
| 539 |
class B: pass |
| 540 |
class A: pass |
| 541 |
|
| 542 |
def f(x: T = B()) -> None: # E: Incompatible default for parameter "x" (default has type "B", parameter has type "T") |
| 543 |
b = x # type: B # E: Incompatible types in assignment (expression has type "T", variable has type "B") |
| 544 |
a = x # type: A |
| 545 |
[case testDefaultParametersWithSubtypes] |
| 546 |
import typing |
| 547 |
class A: pass |
| 548 |
class B(A): pass |
| 549 |
|
| 550 |
def f(x: 'B' = A()) -> None: # E: Incompatible default for parameter "x" (default has type "A", parameter has type "B") |
| 551 |
pass |
| 552 |
def g(x: 'A' = B()) -> None: |
| 553 |
pass |
| 554 |
[out] |
| 555 |
|
| 556 |
[case testMultipleDefaultParameterExpressions] |
| 557 |
import typing |
| 558 |
class A: pass |
| 559 |
class B: pass |
| 560 |
|
| 561 |
def f(x: 'A' = B(), y: 'B' = B()) -> None: # E: Incompatible default for parameter "x" (default has type "B", parameter has type "A") |
| 562 |
pass |
| 563 |
def h(x: 'A' = A(), y: 'B' = B()) -> None: |
| 564 |
pass |
| 565 |
[out] |
| 566 |
|
| 567 |
[case testMultipleDefaultParameterExpressions2] |
| 568 |
import typing |
| 569 |
class A: pass |
| 570 |
class B: pass |
| 571 |
|
| 572 |
def g(x: 'A' = A(), y: 'B' = A()) -> None: # E: Incompatible default for parameter "y" (default has type "A", parameter has type "B") |
| 573 |
pass |
| 574 |
[out] |
| 575 |
|
| 576 |
[case testDefaultArgumentsAndSignatureAsComment] |
| 577 |
import typing |
| 578 |
def f(x = 1): # type: (int) -> str |
| 579 |
pass |
| 580 |
f() |
| 581 |
f(1) |
| 582 |
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 583 |
|
| 584 |
[case testMethodDefaultArgumentsAndSignatureAsComment] |
| 585 |
import typing |
| 586 |
class A: |
| 587 |
def f(self, x = 1): # type: (int) -> str |
| 588 |
pass |
| 589 |
A().f() |
| 590 |
A().f(1) |
| 591 |
A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" |
| 592 |
|
| 593 |
|
| 594 |
-- Access to method defined as a data attribute |
| 595 |
-- -------------------------------------------- |
| 596 |
|
| 597 |
|
| 598 |
[case testMethodAsDataAttribute] |
| 599 |
from typing import Any, Callable, ClassVar |
| 600 |
class B: pass |
| 601 |
x: Any |
| 602 |
class A: |
| 603 |
f = x # type: ClassVar[Callable[[A], None]] |
| 604 |
g = x # type: ClassVar[Callable[[A, B], None]] |
| 605 |
a: A |
| 606 |
a.f() |
| 607 |
a.g(B()) |
| 608 |
a.f(a) # E: Too many arguments |
| 609 |
a.g() # E: Too few arguments |
| 610 |
|
| 611 |
[case testMethodWithInvalidMethodAsDataAttribute] |
| 612 |
from typing import Any, Callable, ClassVar |
| 613 |
class B: pass |
| 614 |
x: Any |
| 615 |
class A: |
| 616 |
f = x # type: ClassVar[Callable[[], None]] |
| 617 |
g = x # type: ClassVar[Callable[[B], None]] |
| 618 |
a: A |
| 619 |
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument |
| 620 |
a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]" |
| 621 |
|
| 622 |
[case testMethodWithDynamicallyTypedMethodAsDataAttribute] |
| 623 |
from typing import Any, Callable, ClassVar |
| 624 |
class B: pass |
| 625 |
x: Any |
| 626 |
class A: |
| 627 |
f = x # type: ClassVar[Callable[[Any], Any]] |
| 628 |
a: A |
| 629 |
a.f() |
| 630 |
a.f(a) # E: Too many arguments |
| 631 |
|
| 632 |
[case testMethodWithInferredMethodAsDataAttribute] |
| 633 |
from typing import Any |
| 634 |
def m(self: "A") -> int: ... |
| 635 |
|
| 636 |
class A: |
| 637 |
n = m |
| 638 |
|
| 639 |
a = A() |
| 640 |
reveal_type(a.n()) # N: Revealed type is "builtins.int" |
| 641 |
reveal_type(A.n(a)) # N: Revealed type is "builtins.int" |
| 642 |
A.n() # E: Too few arguments |
| 643 |
|
| 644 |
[case testOverloadedMethodAsDataAttribute] |
| 645 |
from foo import * |
| 646 |
[file foo.pyi] |
| 647 |
from typing import overload |
| 648 |
class B: pass |
| 649 |
class A: |
| 650 |
@overload |
| 651 |
def f(self) -> None: pass |
| 652 |
@overload |
| 653 |
def f(self, b: B) -> None: pass |
| 654 |
g = f |
| 655 |
a: A |
| 656 |
a.g() |
| 657 |
a.g(B()) |
| 658 |
a.g(a) # E: No overload variant matches argument type "A" \ |
| 659 |
# N: Possible overload variants: \ |
| 660 |
# N: def f(self) -> None \ |
| 661 |
# N: def f(self, b: B) -> None |
| 662 |
|
| 663 |
[case testMethodAsDataAttributeInferredFromDynamicallyTypedMethod] |
| 664 |
|
| 665 |
class A: |
| 666 |
def f(self, x): pass |
| 667 |
g = f |
| 668 |
a: A |
| 669 |
a.g(object()) |
| 670 |
a.g(a, a) # E: Too many arguments |
| 671 |
a.g() # E: Too few arguments |
| 672 |
|
| 673 |
[case testMethodAsDataAttributeInGenericClass] |
| 674 |
from typing import TypeVar, Generic |
| 675 |
t = TypeVar('t') |
| 676 |
class B: pass |
| 677 |
class A(Generic[t]): |
| 678 |
def f(self, x: t) -> None: pass |
| 679 |
g = f |
| 680 |
a: A[B] |
| 681 |
a.g(B()) |
| 682 |
a.g(a) # E: Argument 1 has incompatible type "A[B]"; expected "B" |
| 683 |
|
| 684 |
[case testInvalidMethodAsDataAttributeInGenericClass] |
| 685 |
from typing import Any, TypeVar, Generic, Callable, ClassVar |
| 686 |
t = TypeVar('t') |
| 687 |
class B: pass |
| 688 |
class C: pass |
| 689 |
x: Any |
| 690 |
class A(Generic[t]): |
| 691 |
f = x # type: ClassVar[Callable[[A[B]], None]] |
| 692 |
ab: A[B] |
| 693 |
ac: A[C] |
| 694 |
ab.f() |
| 695 |
ac.f() # E: Invalid self argument "A[C]" to attribute function "f" with type "Callable[[A[B]], None]" |
| 696 |
|
| 697 |
[case testPartiallyTypedSelfInMethodDataAttribute] |
| 698 |
from typing import Any, TypeVar, Generic, Callable, ClassVar |
| 699 |
t = TypeVar('t') |
| 700 |
class B: pass |
| 701 |
class C: pass |
| 702 |
x: Any |
| 703 |
class A(Generic[t]): |
| 704 |
f = x # type: ClassVar[Callable[[A], None]] |
| 705 |
ab: A[B] |
| 706 |
ac: A[C] |
| 707 |
ab.f() |
| 708 |
ac.f() |
| 709 |
|
| 710 |
[case testCallableDataAttribute] |
| 711 |
from typing import Callable, ClassVar |
| 712 |
class A: |
| 713 |
g: ClassVar[Callable[[A], None]] |
| 714 |
def __init__(self, f: Callable[[], None]) -> None: |
| 715 |
self.f = f |
| 716 |
a = A(lambda: None) |
| 717 |
a.f() |
| 718 |
a.g() |
| 719 |
a.f(a) # E: Too many arguments |
| 720 |
a.g(a) # E: Too many arguments |
| 721 |
|
| 722 |
|
| 723 |
-- Nested functions |
| 724 |
-- ---------------- |
| 725 |
|
| 726 |
|
| 727 |
[case testSimpleNestedFunction] |
| 728 |
import typing |
| 729 |
def f(a: 'A') -> None: |
| 730 |
def g(b: 'B') -> None: |
| 731 |
if int(): |
| 732 |
b = a \ |
| 733 |
# E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 734 |
aa = a # type: A # ok |
| 735 |
b = B() |
| 736 |
g(a) # E: Argument 1 to "g" has incompatible type "A"; expected "B" |
| 737 |
g(B()) |
| 738 |
class A: pass |
| 739 |
class B: pass |
| 740 |
|
| 741 |
[case testReturnAndNestedFunction] |
| 742 |
import typing |
| 743 |
def f() -> 'A': |
| 744 |
def g() -> 'B': |
| 745 |
return A() # fail |
| 746 |
return B() |
| 747 |
return B() # fail |
| 748 |
return A() |
| 749 |
class A: pass |
| 750 |
class B: pass |
| 751 |
[out] |
| 752 |
main:4: error: Incompatible return value type (got "A", expected "B") |
| 753 |
main:6: error: Incompatible return value type (got "B", expected "A") |
| 754 |
|
| 755 |
[case testDynamicallyTypedNestedFunction] |
| 756 |
import typing |
| 757 |
def f(x: object) -> None: |
| 758 |
def g(y): |
| 759 |
pass |
| 760 |
g() # E: Missing positional argument "y" in call to "g" |
| 761 |
g(x) |
| 762 |
[out] |
| 763 |
|
| 764 |
[case testNestedFunctionInMethod] |
| 765 |
import typing |
| 766 |
class A: |
| 767 |
def f(self) -> None: |
| 768 |
def g(x: int) -> None: |
| 769 |
y = x # type: int |
| 770 |
a = x # type: A # fail |
| 771 |
g(2) |
| 772 |
g(A()) # fail |
| 773 |
[out] |
| 774 |
main:6: error: Incompatible types in assignment (expression has type "int", variable has type "A") |
| 775 |
main:8: error: Argument 1 to "g" has incompatible type "A"; expected "int" |
| 776 |
|
| 777 |
[case testNestedFunctionInMethodWithTooFewArgumentsInTypeComment_no_native_parse] |
| 778 |
class A: |
| 779 |
def f(self): |
| 780 |
# type: () -> None |
| 781 |
def g(x): # E: Type signature has too few parameters |
| 782 |
# type: () -> None |
| 783 |
pass |
| 784 |
|
| 785 |
[case testNestedFunctionInMethodCannotAccessEnclosingClassAttributes] |
| 786 |
class A: |
| 787 |
var: int |
| 788 |
import mod |
| 789 |
class B: ... |
| 790 |
|
| 791 |
def method(self) -> None: |
| 792 |
def f1(arg: object) -> None: |
| 793 |
arg.var # E: "object" has no attribute "var" |
| 794 |
arg.mod # E: "object" has no attribute "mod" |
| 795 |
arg.B # E: "object" has no attribute "B" |
| 796 |
[file mod.py] |
| 797 |
|
| 798 |
[case testDeepNestedFunctionWithTooFewArgumentsInTypeComment_no_native_parse] |
| 799 |
class A: |
| 800 |
def f(self): |
| 801 |
# type: () -> None |
| 802 |
class B: |
| 803 |
def g(self): |
| 804 |
# type: () -> None |
| 805 |
def h(x): # E: Type signature has too few parameters |
| 806 |
# type: () -> None |
| 807 |
pass |
| 808 |
|
| 809 |
[case testDeepNestedMethodInTypeComment] |
| 810 |
class A: |
| 811 |
def f(self): |
| 812 |
# type: () -> None |
| 813 |
class B: |
| 814 |
class C: |
| 815 |
def g(self): |
| 816 |
# type: () -> None |
| 817 |
pass |
| 818 |
|
| 819 |
[case testMutuallyRecursiveNestedFunctions] |
| 820 |
def f() -> None: |
| 821 |
def g() -> None: |
| 822 |
h(1) |
| 823 |
h('') # E |
| 824 |
def h(x: int) -> None: |
| 825 |
g() |
| 826 |
g(1) # E |
| 827 |
[out] |
| 828 |
main:4: error: Argument 1 to "h" has incompatible type "str"; expected "int" |
| 829 |
main:7: error: Too many arguments for "g" |
| 830 |
|
| 831 |
[case testMutuallyRecursiveDecoratedFunctions] |
| 832 |
from typing import Callable, Any |
| 833 |
def dec(f) -> Callable[..., Any]: pass |
| 834 |
def f() -> None: |
| 835 |
@dec |
| 836 |
def g() -> None: |
| 837 |
h() |
| 838 |
h.x # E |
| 839 |
@dec |
| 840 |
def h(x: int) -> None: |
| 841 |
g(1) |
| 842 |
g.x # E |
| 843 |
[out] |
| 844 |
main:7: error: "Callable[..., Any]" has no attribute "x" |
| 845 |
main:11: error: "Callable[..., Any]" has no attribute "x" |
| 846 |
|
| 847 |
[case testNestedGenericFunctions] |
| 848 |
from typing import TypeVar |
| 849 |
T = TypeVar('T') |
| 850 |
U = TypeVar('U') |
| 851 |
|
| 852 |
def outer(x: T) -> T: |
| 853 |
def inner(y: U) -> T: ... |
| 854 |
return inner(1) |
| 855 |
|
| 856 |
|
| 857 |
-- Casts |
| 858 |
-- ----- |
| 859 |
|
| 860 |
|
| 861 |
[case testCastsToAndFromFunctionTypes] |
| 862 |
from typing import TypeVar, Callable, Any, cast |
| 863 |
t = TypeVar('t') |
| 864 |
def f(x: t, |
| 865 |
f1: Callable[[], None], |
| 866 |
f2: Callable[[Any], None], o: object) -> None: |
| 867 |
x = cast(t, f1) |
| 868 |
f1 = cast(Callable[[], None], x) |
| 869 |
f1 = cast(Callable[[], None], f2) |
| 870 |
f1 = cast(Callable[[], None], o) |
| 871 |
|
| 872 |
|
| 873 |
-- Function decorators |
| 874 |
-- ------------------- |
| 875 |
|
| 876 |
|
| 877 |
[case testTrivialStaticallyTypedFunctionDecorator] |
| 878 |
from typing import TypeVar |
| 879 |
t = TypeVar('t') |
| 880 |
def dec(f: t) -> t: |
| 881 |
return f |
| 882 |
@dec |
| 883 |
def f(x: int) -> None: pass |
| 884 |
f(1) |
| 885 |
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 886 |
|
| 887 |
[case testTrivialStaticallyTypedMethodDecorator] |
| 888 |
from typing import TypeVar |
| 889 |
t = TypeVar('t') |
| 890 |
def dec(f: t) -> t: |
| 891 |
return f |
| 892 |
class A: |
| 893 |
@dec |
| 894 |
def f(self, x: int) -> None: pass |
| 895 |
A().f(1) |
| 896 |
A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" |
| 897 |
class B: pass |
| 898 |
|
| 899 |
[case testTrivialDecoratedNestedFunction] |
| 900 |
from typing import TypeVar |
| 901 |
t = TypeVar('t') |
| 902 |
def dec(f: t) -> t: |
| 903 |
return f |
| 904 |
def g() -> None: |
| 905 |
@dec |
| 906 |
def f(x: int) -> None: pass |
| 907 |
f(1) |
| 908 |
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 909 |
[out] |
| 910 |
|
| 911 |
[case testCheckingDecoratedFunction] |
| 912 |
import typing |
| 913 |
def dec(f): pass |
| 914 |
@dec |
| 915 |
def f(x: 'A') -> None: |
| 916 |
a = x # type: A |
| 917 |
if int(): |
| 918 |
x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A") |
| 919 |
class A: pass |
| 920 |
[out] |
| 921 |
|
| 922 |
[case testDecoratorThatSwitchesType] |
| 923 |
from typing import Callable |
| 924 |
def dec(x) -> Callable[[], None]: pass |
| 925 |
@dec |
| 926 |
def f(y): pass |
| 927 |
f() |
| 928 |
f(None) # E: Too many arguments for "f" |
| 929 |
|
| 930 |
[case testDecoratorThatSwitchesTypeWithMethod] |
| 931 |
from typing import Any, Callable |
| 932 |
def dec(x) -> Callable[[Any], None]: pass |
| 933 |
class A: |
| 934 |
@dec |
| 935 |
def f(self, a, b, c): pass |
| 936 |
a: A |
| 937 |
a.f() |
| 938 |
a.f(None) # E: Too many arguments for "f" of "A" |
| 939 |
|
| 940 |
[case testNestedDecorators] |
| 941 |
from typing import Any, Callable |
| 942 |
def dec1(f: Callable[[Any], None]) -> Callable[[], None]: pass |
| 943 |
def dec2(f: Callable[[Any, Any], None]) -> Callable[[Any], None]: pass |
| 944 |
@dec1 |
| 945 |
@dec2 |
| 946 |
def f(x, y): pass |
| 947 |
f() |
| 948 |
f(None) # E: Too many arguments for "f" |
| 949 |
|
| 950 |
[case testInvalidDecorator1] |
| 951 |
from typing import Any, Callable |
| 952 |
def dec1(f: Callable[[Any], None]) -> Callable[[], None]: pass |
| 953 |
def dec2(f: Callable[[Any, Any], None]) -> Callable[[Any], None]: pass |
| 954 |
@dec1 |
| 955 |
@dec2 # E: Argument 1 to "dec2" has incompatible type "Callable[[Any], Any]"; expected "Callable[[Any, Any], None]" |
| 956 |
def f(x): pass |
| 957 |
|
| 958 |
def faulty(c: Callable[[int], None]) -> Callable[[tuple[int, int]], None]: |
| 959 |
return lambda x: None |
| 960 |
|
| 961 |
@faulty # E: Argument 1 to "faulty" has incompatible type "Callable[[tuple[int, int]], None]"; expected "Callable[[int], None]" |
| 962 |
@faulty # E: Argument 1 to "faulty" has incompatible type "Callable[[str], None]"; expected "Callable[[int], None]" |
| 963 |
def g(x: str) -> None: |
| 964 |
return None |
| 965 |
[builtins fixtures/tuple.pyi] |
| 966 |
|
| 967 |
[case testInvalidDecorator2] |
| 968 |
from typing import Any, Callable |
| 969 |
def dec1(f: Callable[[Any, Any], None]) -> Callable[[], None]: pass |
| 970 |
def dec2(f: Callable[[Any, Any], None]) -> Callable[[Any], None]: pass |
| 971 |
@dec1 # E: Argument 1 to "dec1" has incompatible type "Callable[[Any], None]"; expected "Callable[[Any, Any], None]" |
| 972 |
@dec2 |
| 973 |
def f(x, y): pass |
| 974 |
|
| 975 |
[case testNoTypeCheckDecoratorOnMethod1] |
| 976 |
from typing import no_type_check |
| 977 |
|
| 978 |
@no_type_check |
| 979 |
def foo(x: 'bar', y: {'x': 4}) -> 42: |
| 980 |
1 + 'x' |
| 981 |
[typing fixtures/typing-medium.pyi] |
| 982 |
|
| 983 |
[case testNoTypeCheckDecoratorOnMethod2] |
| 984 |
import typing |
| 985 |
|
| 986 |
@typing.no_type_check |
| 987 |
def foo(x: 's', y: {'x': 4}) -> 42: |
| 988 |
1 + 'x' |
| 989 |
|
| 990 |
@typing.no_type_check |
| 991 |
def bar() -> None: |
| 992 |
1 + 'x' |
| 993 |
[typing fixtures/typing-medium.pyi] |
| 994 |
|
| 995 |
[case testCallingNoTypeCheckFunction] |
| 996 |
import typing |
| 997 |
|
| 998 |
@typing.no_type_check |
| 999 |
def foo(x: {1:2}) -> [1]: |
| 1000 |
1 + 'x' |
| 1001 |
|
| 1002 |
foo() |
| 1003 |
foo(1, 'b') |
| 1004 |
[typing fixtures/typing-medium.pyi] |
| 1005 |
|
| 1006 |
[case testCallingNoTypeCheckFunction2] |
| 1007 |
import typing |
| 1008 |
|
| 1009 |
def f() -> None: |
| 1010 |
foo() |
| 1011 |
|
| 1012 |
@typing.no_type_check |
| 1013 |
def foo(x: {1:2}) -> [1]: |
| 1014 |
1 + 'x' |
| 1015 |
[typing fixtures/typing-medium.pyi] |
| 1016 |
|
| 1017 |
[case testNoTypeCheckDecoratorSemanticError] |
| 1018 |
import typing |
| 1019 |
|
| 1020 |
@typing.no_type_check |
| 1021 |
def foo(x: {1:2}) -> [1]: |
| 1022 |
x = y |
| 1023 |
[typing fixtures/typing-medium.pyi] |
| 1024 |
|
| 1025 |
[case testNoTypeCheckDecoratorNoUntypedDefs] |
| 1026 |
# flags: --disallow-untyped-defs |
| 1027 |
import typing |
| 1028 |
|
| 1029 |
@typing.no_type_check |
| 1030 |
def foo(x: "foo") -> "bar": |
| 1031 |
x: "whatever" |
| 1032 |
[typing fixtures/typing-medium.pyi] |
| 1033 |
|
| 1034 |
-- Forward references to decorated functions |
| 1035 |
-- ----------------------------------------- |
| 1036 |
|
| 1037 |
|
| 1038 |
[case testForwardReferenceToDynamicallyTypedDecorator] |
| 1039 |
def f(self) -> None: |
| 1040 |
g() |
| 1041 |
g(1) |
| 1042 |
|
| 1043 |
def dec(f): |
| 1044 |
return f |
| 1045 |
|
| 1046 |
@dec |
| 1047 |
def g(): |
| 1048 |
pass |
| 1049 |
|
| 1050 |
[case testForwardReferenceToDecoratorWithAnyReturn] |
| 1051 |
from typing import Any |
| 1052 |
|
| 1053 |
def f(self) -> None: |
| 1054 |
g() |
| 1055 |
g(1) |
| 1056 |
|
| 1057 |
def dec(f) -> Any: |
| 1058 |
return f |
| 1059 |
|
| 1060 |
@dec |
| 1061 |
def g(): |
| 1062 |
pass |
| 1063 |
|
| 1064 |
[case testForwardReferenceToDecoratorWithIdentityMapping] |
| 1065 |
from typing import TypeVar |
| 1066 |
|
| 1067 |
def f(self) -> None: |
| 1068 |
g() |
| 1069 |
g(1) # E: Too many arguments for "g" |
| 1070 |
h(1).x # E: "str" has no attribute "x" |
| 1071 |
h('') # E: Argument 1 to "h" has incompatible type "str"; expected "int" |
| 1072 |
|
| 1073 |
T = TypeVar('T') |
| 1074 |
def dec(f: T) -> T: |
| 1075 |
return f |
| 1076 |
|
| 1077 |
@dec |
| 1078 |
def g(): pass |
| 1079 |
@dec |
| 1080 |
def h(x: int) -> str: pass |
| 1081 |
[out] |
| 1082 |
|
| 1083 |
[case testForwardReferenceToDynamicallyTypedDecoratedMethod] |
| 1084 |
def f(self) -> None: |
| 1085 |
A().f(1).y |
| 1086 |
A().f() |
| 1087 |
|
| 1088 |
class A: |
| 1089 |
@dec |
| 1090 |
def f(self, x): pass |
| 1091 |
|
| 1092 |
def dec(f): return f |
| 1093 |
[builtins fixtures/staticmethod.pyi] |
| 1094 |
|
| 1095 |
[case testForwardReferenceToStaticallyTypedDecoratedMethod] |
| 1096 |
from typing import TypeVar |
| 1097 |
|
| 1098 |
def f(self) -> None: |
| 1099 |
A().f(1).y # E: "str" has no attribute "y" |
| 1100 |
A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" |
| 1101 |
|
| 1102 |
class A: |
| 1103 |
@dec |
| 1104 |
def f(self, a: int) -> str: return '' |
| 1105 |
|
| 1106 |
T = TypeVar('T') |
| 1107 |
def dec(f: T) -> T: return f |
| 1108 |
[builtins fixtures/staticmethod.pyi] |
| 1109 |
[out] |
| 1110 |
|
| 1111 |
[case testForwardReferenceToDynamicallyTypedProperty] |
| 1112 |
def f(self) -> None: |
| 1113 |
A().x.y |
| 1114 |
|
| 1115 |
class A: |
| 1116 |
@property |
| 1117 |
def x(self): pass |
| 1118 |
[builtins fixtures/property.pyi] |
| 1119 |
|
| 1120 |
[case testForwardReferenceToStaticallyTypedProperty] |
| 1121 |
def f(self) -> None: |
| 1122 |
A().x.y # E: "int" has no attribute "y" |
| 1123 |
|
| 1124 |
class A: |
| 1125 |
@property |
| 1126 |
def x(self) -> int: return 1 |
| 1127 |
[builtins fixtures/property.pyi] |
| 1128 |
[out] |
| 1129 |
|
| 1130 |
[case testForwardReferenceToDynamicallyTypedStaticMethod] |
| 1131 |
def f(self) -> None: |
| 1132 |
A.x(1).y |
| 1133 |
A.x() # E: Missing positional argument "x" in call to "x" of "A" |
| 1134 |
|
| 1135 |
class A: |
| 1136 |
@staticmethod |
| 1137 |
def x(x): pass |
| 1138 |
[builtins fixtures/staticmethod.pyi] |
| 1139 |
[out] |
| 1140 |
|
| 1141 |
[case testForwardReferenceToStaticallyTypedStaticMethod] |
| 1142 |
def f(self) -> None: |
| 1143 |
A.x(1).y # E: "str" has no attribute "y" |
| 1144 |
A.x('') # E: Argument 1 to "x" of "A" has incompatible type "str"; expected "int" |
| 1145 |
|
| 1146 |
class A: |
| 1147 |
@staticmethod |
| 1148 |
def x(a: int) -> str: return '' |
| 1149 |
[builtins fixtures/staticmethod.pyi] |
| 1150 |
[out] |
| 1151 |
|
| 1152 |
[case testForwardReferenceToDynamicallyTypedClassMethod] |
| 1153 |
def f(self) -> None: |
| 1154 |
A.x(1).y |
| 1155 |
A.x() # E: Missing positional argument "a" in call to "x" of "A" |
| 1156 |
|
| 1157 |
class A: |
| 1158 |
@classmethod |
| 1159 |
def x(cls, a): pass |
| 1160 |
[builtins fixtures/classmethod.pyi] |
| 1161 |
[out] |
| 1162 |
|
| 1163 |
[case testForwardReferenceToStaticallyTypedClassMethod] |
| 1164 |
def f(self) -> None: |
| 1165 |
A.x(1).y # E: "str" has no attribute "y" |
| 1166 |
A.x('') # E: Argument 1 to "x" of "A" has incompatible type "str"; expected "int" |
| 1167 |
|
| 1168 |
class A: |
| 1169 |
@classmethod |
| 1170 |
def x(cls, x: int) -> str: return '' |
| 1171 |
[builtins fixtures/classmethod.pyi] |
| 1172 |
[out] |
| 1173 |
|
| 1174 |
[case testForwardReferenceToDecoratedFunctionUsingMemberExpr] |
| 1175 |
import m |
| 1176 |
|
| 1177 |
def f(self) -> None: |
| 1178 |
g(1).x # E: "str" has no attribute "x" |
| 1179 |
|
| 1180 |
@m.dec |
| 1181 |
def g(x: int) -> str: pass |
| 1182 |
[file m.py] |
| 1183 |
from typing import TypeVar |
| 1184 |
T = TypeVar('T') |
| 1185 |
def dec(f: T) -> T: |
| 1186 |
return f |
| 1187 |
[out] |
| 1188 |
|
| 1189 |
[case testForwardReferenceToFunctionWithMultipleDecorators] |
| 1190 |
# flags: --disable-error-code=used-before-def |
| 1191 |
def f(self) -> None: |
| 1192 |
g() |
| 1193 |
g(1) |
| 1194 |
|
| 1195 |
def dec(f): |
| 1196 |
return f |
| 1197 |
|
| 1198 |
@dec |
| 1199 |
@dec2 |
| 1200 |
def g(): |
| 1201 |
pass |
| 1202 |
|
| 1203 |
def dec2(f): |
| 1204 |
return f |
| 1205 |
|
| 1206 |
[case testForwardReferenceToDynamicallyTypedDecoratedStaticMethod] |
| 1207 |
def f(self) -> None: |
| 1208 |
A().f(1).y |
| 1209 |
A().f() |
| 1210 |
A().g(1).y |
| 1211 |
A().g() |
| 1212 |
|
| 1213 |
class A: |
| 1214 |
@dec |
| 1215 |
@staticmethod |
| 1216 |
def f(self, x): pass |
| 1217 |
@staticmethod |
| 1218 |
@dec |
| 1219 |
def g(self, x): pass |
| 1220 |
|
| 1221 |
def dec(f): return f |
| 1222 |
[builtins fixtures/staticmethod.pyi] |
| 1223 |
|
| 1224 |
[case testForwardRefereceToDecoratedFunctionWithCallExpressionDecorator] |
| 1225 |
# flags: --disable-error-code=used-before-def |
| 1226 |
def f(self) -> None: |
| 1227 |
g() |
| 1228 |
g(1) |
| 1229 |
|
| 1230 |
@dec(1) |
| 1231 |
def g(): pass |
| 1232 |
|
| 1233 |
def dec(f): pass |
| 1234 |
|
| 1235 |
|
| 1236 |
-- Decorator functions in import cycles |
| 1237 |
-- ------------------------------------ |
| 1238 |
|
| 1239 |
|
| 1240 |
[case testDecoratorWithIdentityTypeInImportCycle] |
| 1241 |
import a |
| 1242 |
|
| 1243 |
[file a.py] |
| 1244 |
import b |
| 1245 |
from d import dec |
| 1246 |
@dec |
| 1247 |
def f(x: int) -> None: pass |
| 1248 |
b.g(1) # E: Argument 1 to "g" has incompatible type "int"; expected "str" |
| 1249 |
|
| 1250 |
[file b.py] |
| 1251 |
import a |
| 1252 |
from d import dec |
| 1253 |
@dec |
| 1254 |
def g(x: str) -> None: pass |
| 1255 |
a.f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1256 |
|
| 1257 |
[file d.py] |
| 1258 |
from typing import TypeVar |
| 1259 |
T = TypeVar('T') |
| 1260 |
def dec(f: T) -> T: return f |
| 1261 |
|
| 1262 |
[case testDecoratorWithNoAnnotationInImportCycle] |
| 1263 |
import a |
| 1264 |
|
| 1265 |
[file a.py] |
| 1266 |
import b |
| 1267 |
from d import dec |
| 1268 |
@dec |
| 1269 |
def f(x: int) -> None: pass |
| 1270 |
b.g(1, z=4) |
| 1271 |
|
| 1272 |
[file b.py] |
| 1273 |
import a |
| 1274 |
from d import dec |
| 1275 |
@dec |
| 1276 |
def g(x: str) -> None: pass |
| 1277 |
a.f('', y=2) |
| 1278 |
|
| 1279 |
[file d.py] |
| 1280 |
def dec(f): return f |
| 1281 |
|
| 1282 |
[case testDecoratorWithFixedReturnTypeInImportCycle] |
| 1283 |
import a |
| 1284 |
|
| 1285 |
[file a.py] |
| 1286 |
import b |
| 1287 |
from d import dec |
| 1288 |
@dec |
| 1289 |
def f(x: int) -> str: pass |
| 1290 |
b.g(1)() # E: "str" not callable |
| 1291 |
|
| 1292 |
[file b.py] |
| 1293 |
import a |
| 1294 |
from d import dec |
| 1295 |
@dec |
| 1296 |
def g(x: int) -> str: pass |
| 1297 |
a.f(1)() # E: "str" not callable |
| 1298 |
|
| 1299 |
[file d.py] |
| 1300 |
from typing import Callable |
| 1301 |
def dec(f: Callable[[int], str]) -> Callable[[int], str]: return f |
| 1302 |
|
| 1303 |
[case testDecoratorWithCallAndFixedReturnTypeInImportCycle] |
| 1304 |
import a |
| 1305 |
|
| 1306 |
[file a.py] |
| 1307 |
import b |
| 1308 |
from d import dec |
| 1309 |
@dec() |
| 1310 |
def f(x: int) -> str: pass |
| 1311 |
b.g(1)() # E: "str" not callable |
| 1312 |
|
| 1313 |
[file b.py] |
| 1314 |
import a |
| 1315 |
from d import dec |
| 1316 |
@dec() |
| 1317 |
def g(x: int) -> str: pass |
| 1318 |
a.f(1)() # E: "str" not callable |
| 1319 |
|
| 1320 |
[file d.py] |
| 1321 |
from typing import Callable |
| 1322 |
def dec() -> Callable[[Callable[[int], str]], Callable[[int], str]]: pass |
| 1323 |
|
| 1324 |
[case testDecoratorWithCallAndFixedReturnTypeInImportCycleAndDecoratorArgs] |
| 1325 |
import a |
| 1326 |
|
| 1327 |
[file a.py] |
| 1328 |
import b |
| 1329 |
from d import dec |
| 1330 |
@dec(1) # E: Argument 1 to "dec" has incompatible type "int"; expected "str" |
| 1331 |
def f(x: int) -> str: pass |
| 1332 |
b.g(1)() # E: "str" not callable |
| 1333 |
|
| 1334 |
[file b.py] |
| 1335 |
import a |
| 1336 |
from d import dec |
| 1337 |
@dec(1) # E: Argument 1 to "dec" has incompatible type "int"; expected "str" |
| 1338 |
def g(x: int) -> str: pass |
| 1339 |
a.f(1)() # E: "str" not callable |
| 1340 |
|
| 1341 |
[file d.py] |
| 1342 |
from typing import Callable |
| 1343 |
def dec(x: str) -> Callable[[Callable[[int], str]], Callable[[int], str]]: pass |
| 1344 |
|
| 1345 |
[case testUndefinedDecoratorInImportCycle] |
| 1346 |
# cmd: mypy -m foo.base |
| 1347 |
[file foo/__init__.py] |
| 1348 |
import foo.base |
| 1349 |
class Derived(foo.base.Base): |
| 1350 |
def method(self) -> None: pass |
| 1351 |
[file foo/base.py] |
| 1352 |
import foo |
| 1353 |
class Base: |
| 1354 |
@decorator |
| 1355 |
def method(self) -> None: pass |
| 1356 |
[out] |
| 1357 |
tmp/foo/base.py:3: error: Name "decorator" is not defined |
| 1358 |
|
| 1359 |
|
| 1360 |
-- Conditional function definition |
| 1361 |
-- ------------------------------- |
| 1362 |
|
| 1363 |
|
| 1364 |
[case testTypeCheckBodyOfConditionalFunction] |
| 1365 |
from typing import Any |
| 1366 |
x = None # type: Any |
| 1367 |
if x: |
| 1368 |
def f(x: int) -> None: |
| 1369 |
if int(): |
| 1370 |
x = 1 |
| 1371 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 1372 |
[out] |
| 1373 |
|
| 1374 |
[case testCallConditionalFunction] |
| 1375 |
from typing import Any |
| 1376 |
x = None # type: Any |
| 1377 |
if x: |
| 1378 |
def f(x: int) -> None: pass |
| 1379 |
f(1) |
| 1380 |
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1381 |
f(1) |
| 1382 |
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1383 |
|
| 1384 |
[case testConditionalFunctionDefinitionWithIfElse] |
| 1385 |
from typing import Any |
| 1386 |
x = None # type: Any |
| 1387 |
if x: |
| 1388 |
def f(x: int) -> None: |
| 1389 |
'x' + x # fail |
| 1390 |
if int(): |
| 1391 |
x = 1 |
| 1392 |
else: |
| 1393 |
def f(x: int) -> None: |
| 1394 |
x + 'x' # fail |
| 1395 |
if int(): |
| 1396 |
x = 1 |
| 1397 |
f(1) |
| 1398 |
f('x') # fail |
| 1399 |
[builtins fixtures/primitives.pyi] |
| 1400 |
[out] |
| 1401 |
main:5: error: Unsupported operand types for + ("str" and "int") |
| 1402 |
main:10: error: Unsupported operand types for + ("int" and "str") |
| 1403 |
main:14: error: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1404 |
|
| 1405 |
[case testNestedConditionalFunctionDefinitionWithIfElse] |
| 1406 |
from typing import Any |
| 1407 |
x = None # type: Any |
| 1408 |
def top() -> None: |
| 1409 |
if x: |
| 1410 |
def f(x: int) -> None: |
| 1411 |
if int(): |
| 1412 |
x = 'x' # E: Incompatible types in assignment \ |
| 1413 |
(expression has type "str", variable has type "int") |
| 1414 |
x = 1 |
| 1415 |
else: |
| 1416 |
def f(x: int) -> None: |
| 1417 |
x + 'x' # E: Unsupported operand types for + ("int" and "str") |
| 1418 |
x = 1 |
| 1419 |
f(1) |
| 1420 |
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1421 |
|
| 1422 |
[case testUnconditionalRedefinitionOfConditionalFunction] |
| 1423 |
from typing import Any |
| 1424 |
x = None # type: Any |
| 1425 |
if x: |
| 1426 |
def f(): pass |
| 1427 |
def f(): pass # E: Name "f" already defined on line 4 |
| 1428 |
|
| 1429 |
[case testIncompatibleConditionalFunctionDefinition] |
| 1430 |
from typing import Any |
| 1431 |
x = None # type: Any |
| 1432 |
if x: |
| 1433 |
def f(x: int) -> None: pass |
| 1434 |
else: |
| 1435 |
def f(x): pass # E: All conditional function variants must have identical signatures \ |
| 1436 |
# N: Original: \ |
| 1437 |
# N: def f(x: int) -> None \ |
| 1438 |
# N: Redefinition: \ |
| 1439 |
# N: def f(x: Any) -> Any |
| 1440 |
|
| 1441 |
[case testIncompatibleConditionalFunctionDefinition2] |
| 1442 |
from typing import Any |
| 1443 |
x = None # type: Any |
| 1444 |
if x: |
| 1445 |
def f(x: int) -> None: pass |
| 1446 |
else: |
| 1447 |
def f(y: int) -> None: pass # E: All conditional function variants must have identical signatures \ |
| 1448 |
# N: Original: \ |
| 1449 |
# N: def f(x: int) -> None \ |
| 1450 |
# N: Redefinition: \ |
| 1451 |
# N: def f(y: int) -> None |
| 1452 |
|
| 1453 |
[case testIncompatibleConditionalFunctionDefinition3] |
| 1454 |
from typing import Any |
| 1455 |
x = None # type: Any |
| 1456 |
if x: |
| 1457 |
def f(x: int) -> None: pass |
| 1458 |
else: |
| 1459 |
def f(x: int = 0) -> None: pass # E: All conditional function variants must have identical signatures \ |
| 1460 |
# N: Original: \ |
| 1461 |
# N: def f(x: int) -> None \ |
| 1462 |
# N: Redefinition: \ |
| 1463 |
# N: def f(x: int = ...) -> None |
| 1464 |
|
| 1465 |
[case testIncompatibleConditionalFunctionDefinition4] |
| 1466 |
from typing import Any, Union, TypeVar |
| 1467 |
T1 = TypeVar('T1') |
| 1468 |
T2 = TypeVar('T2', bound=Union[int, str]) |
| 1469 |
x = None # type: Any |
| 1470 |
if x: |
| 1471 |
def f(x: T1) -> T1: pass |
| 1472 |
else: |
| 1473 |
def f(x: T2) -> T2: pass # E: All conditional function variants must have identical signatures \ |
| 1474 |
# N: Original: \ |
| 1475 |
# N: def [T1] f(x: T1) -> T1 \ |
| 1476 |
# N: Redefinition: \ |
| 1477 |
# N: def [T2: int | str] f(x: T2) -> T2 |
| 1478 |
|
| 1479 |
[case testConditionalFunctionDefinitionUsingDecorator1] |
| 1480 |
from typing import Callable |
| 1481 |
|
| 1482 |
def dec(f) -> Callable[[int], None]: pass |
| 1483 |
|
| 1484 |
x = int() |
| 1485 |
if x: |
| 1486 |
@dec |
| 1487 |
def f(): pass |
| 1488 |
else: |
| 1489 |
def f(x: int) -> None: pass |
| 1490 |
|
| 1491 |
[case testConditionalFunctionDefinitionUsingDecorator2] |
| 1492 |
from typing import Callable |
| 1493 |
|
| 1494 |
def dec(f) -> Callable[[int], None]: pass |
| 1495 |
|
| 1496 |
x = int() |
| 1497 |
if x: |
| 1498 |
@dec |
| 1499 |
def f(): pass |
| 1500 |
else: |
| 1501 |
def f(x: str) -> None: pass # E: Incompatible redefinition (redefinition with type "Callable[[str], None]", original type "Callable[[int], None]") |
| 1502 |
|
| 1503 |
[case testConditionalFunctionDefinitionUsingDecorator3] |
| 1504 |
|
| 1505 |
from typing import Callable |
| 1506 |
|
| 1507 |
def dec(f) -> Callable[[int], None]: pass |
| 1508 |
|
| 1509 |
x = int() |
| 1510 |
if x: |
| 1511 |
def f(x: int, /) -> None: pass |
| 1512 |
else: |
| 1513 |
@dec |
| 1514 |
def f(): pass |
| 1515 |
|
| 1516 |
[case testConditionalFunctionDefinitionUsingDecorator4] |
| 1517 |
|
| 1518 |
from typing import Callable |
| 1519 |
|
| 1520 |
def dec(f) -> Callable[[int], None]: pass |
| 1521 |
|
| 1522 |
x = int() |
| 1523 |
if x: |
| 1524 |
def f(x: str) -> None: pass |
| 1525 |
else: |
| 1526 |
@dec |
| 1527 |
def f(): pass # E: All conditional function variants must have identical signatures \ |
| 1528 |
# N: Original: \ |
| 1529 |
# N: def f(x: str) -> None \ |
| 1530 |
# N: Redefinition: \ |
| 1531 |
# N: def f(int, /) -> None |
| 1532 |
|
| 1533 |
[case testConditionalFunctionDefinitionUnreachable] |
| 1534 |
# flags: --warn-unreachable |
| 1535 |
def bar() -> None: |
| 1536 |
if False: |
| 1537 |
foo = 1 # E: Statement is unreachable |
| 1538 |
else: |
| 1539 |
def foo(obj): ... |
| 1540 |
|
| 1541 |
def baz() -> None: |
| 1542 |
if False: |
| 1543 |
foo: int = 1 # E: Statement is unreachable |
| 1544 |
else: |
| 1545 |
def foo(obj): ... # E: Incompatible redefinition (redefinition with type "Callable[[Any], Any]", original type "int") |
| 1546 |
[builtins fixtures/tuple.pyi] |
| 1547 |
|
| 1548 |
[case testConditionalRedefinitionOfAnUnconditionalFunctionDefinition1] |
| 1549 |
from typing import Any |
| 1550 |
def f(x: str) -> None: pass |
| 1551 |
x = None # type: Any |
| 1552 |
if x: |
| 1553 |
def f(x: int) -> None: pass # E: All conditional function variants must have identical signatures \ |
| 1554 |
# N: Original: \ |
| 1555 |
# N: def f(x: str) -> None \ |
| 1556 |
# N: Redefinition: \ |
| 1557 |
# N: def f(x: int) -> None |
| 1558 |
|
| 1559 |
[case testConditionalRedefinitionOfAnUnconditionalFunctionDefinition2] |
| 1560 |
from typing import Any |
| 1561 |
def f(x: int) -> None: pass |
| 1562 |
x = None # type: Any |
| 1563 |
if x: |
| 1564 |
def f(y: int) -> None: pass # E: All conditional function variants must have identical signatures \ |
| 1565 |
# N: Original: \ |
| 1566 |
# N: def f(x: int) -> None \ |
| 1567 |
# N: Redefinition: \ |
| 1568 |
# N: def f(y: int) -> None |
| 1569 |
f(x=1) # The first definition takes precedence. |
| 1570 |
f(y=1) # E: Unexpected keyword argument "y" for "f" |
| 1571 |
|
| 1572 |
[case testRedefineFunctionDefinedAsVariable] |
| 1573 |
def g(): pass |
| 1574 |
f = g |
| 1575 |
if g(): |
| 1576 |
def f(): pass |
| 1577 |
f() |
| 1578 |
f(1) # E: Too many arguments |
| 1579 |
|
| 1580 |
[case testRedefineFunctionDefinedAsVariableInitializedToNone] |
| 1581 |
def g(): pass |
| 1582 |
f = None |
| 1583 |
if g(): |
| 1584 |
def f(): pass |
| 1585 |
f() |
| 1586 |
f(1) # E: Too many arguments for "f" |
| 1587 |
|
| 1588 |
[case testRedefineNestedFunctionDefinedAsVariableInitializedToNone] |
| 1589 |
def g() -> None: |
| 1590 |
f = None |
| 1591 |
if object(): |
| 1592 |
def f(x: int) -> None: pass |
| 1593 |
f() # E: Missing positional argument "x" in call to "f" |
| 1594 |
f(1) |
| 1595 |
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1596 |
[out] |
| 1597 |
|
| 1598 |
[case testRedefineFunctionDefinedAsVariableWithInvalidSignature] |
| 1599 |
def g(): pass |
| 1600 |
f = g |
| 1601 |
if g(): |
| 1602 |
def f(x): pass # E: Incompatible redefinition (redefinition with type "Callable[[Any], Any]", original type "Callable[[], Any]") |
| 1603 |
|
| 1604 |
[case testRedefineFunctionDefinedAsVariableWithVariance1] |
| 1605 |
class B: pass |
| 1606 |
class C(B): pass |
| 1607 |
def g(x: C) -> B: pass |
| 1608 |
f = g |
| 1609 |
if g(C()): |
| 1610 |
def f(x: C) -> C: pass |
| 1611 |
|
| 1612 |
[case testRedefineFunctionDefinedAsVariableWithVariance2] |
| 1613 |
class B: pass |
| 1614 |
class C(B): pass |
| 1615 |
def g(x: C) -> B: pass |
| 1616 |
f = g |
| 1617 |
if g(C()): |
| 1618 |
def f(x: B) -> B: pass |
| 1619 |
|
| 1620 |
[case testRedefineFunctionDefinedAsVariableInitializedToEmptyList] |
| 1621 |
f = [] # E: Need type annotation for "f" (hint: "f: list[<type>] = ...") |
| 1622 |
if object(): |
| 1623 |
def f(): pass # E: Incompatible redefinition |
| 1624 |
f() # E: "list[Any]" not callable |
| 1625 |
f(1) # E: "list[Any]" not callable |
| 1626 |
[builtins fixtures/list.pyi] |
| 1627 |
|
| 1628 |
[case testDefineConditionallyAsImportedAndDecorated] |
| 1629 |
from typing import Callable |
| 1630 |
|
| 1631 |
def dec(f: Callable[[], None]) -> Callable[[], None]: ... |
| 1632 |
|
| 1633 |
if int(): |
| 1634 |
from m import f |
| 1635 |
else: |
| 1636 |
@dec |
| 1637 |
def f(): |
| 1638 |
yield |
| 1639 |
[file m.py] |
| 1640 |
def f() -> None: pass |
| 1641 |
|
| 1642 |
[case testDefineConditionallyAsImportedAndDecoratedWithInference] |
| 1643 |
if int(): |
| 1644 |
from m import f |
| 1645 |
else: |
| 1646 |
from contextlib import contextmanager |
| 1647 |
|
| 1648 |
@contextmanager |
| 1649 |
def f(): |
| 1650 |
yield |
| 1651 |
[file m.py] |
| 1652 |
from contextlib import contextmanager |
| 1653 |
|
| 1654 |
@contextmanager |
| 1655 |
def f(): |
| 1656 |
yield |
| 1657 |
[typing fixtures/typing-medium.pyi] |
| 1658 |
[builtins fixtures/tuple.pyi] |
| 1659 |
|
| 1660 |
|
| 1661 |
-- Conditional method definition |
| 1662 |
-- ----------------------------- |
| 1663 |
|
| 1664 |
|
| 1665 |
[case testTypeCheckBodyOfConditionalMethod] |
| 1666 |
from typing import Any |
| 1667 |
x = None # type: Any |
| 1668 |
class A: |
| 1669 |
if x: |
| 1670 |
def f(self, x: int) -> None: |
| 1671 |
if int(): |
| 1672 |
x = 1 |
| 1673 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 1674 |
[out] |
| 1675 |
|
| 1676 |
[case testCallConditionalMethodInClassBody] |
| 1677 |
from typing import Any |
| 1678 |
x = None # type: Any |
| 1679 |
class A: |
| 1680 |
if x: |
| 1681 |
def f(self, x: int) -> None: pass |
| 1682 |
f(x, 1) |
| 1683 |
f(x, 'x') # E: Argument 2 to "f" of "A" has incompatible type "str"; expected "int" |
| 1684 |
f(x, 1) |
| 1685 |
f(x, 'x') # E: Argument 2 to "f" of "A" has incompatible type "str"; expected "int" |
| 1686 |
[out] |
| 1687 |
|
| 1688 |
[case testCallConditionalMethodViaInstance] |
| 1689 |
from typing import Any |
| 1690 |
x = None # type: Any |
| 1691 |
class A: |
| 1692 |
if x: |
| 1693 |
def f(self, x: int) -> None: pass |
| 1694 |
A().f(1) |
| 1695 |
A().f('x') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" |
| 1696 |
|
| 1697 |
[case testConditionalMethodDefinitionWithIfElse] |
| 1698 |
from typing import Any |
| 1699 |
x = None # type: Any |
| 1700 |
class A: |
| 1701 |
if x: |
| 1702 |
def f(self, x: int) -> None: |
| 1703 |
'x' + x # fail |
| 1704 |
if int(): |
| 1705 |
x = 1 |
| 1706 |
else: |
| 1707 |
def f(self, x: int) -> None: |
| 1708 |
x + 'x' # fail |
| 1709 |
if int(): |
| 1710 |
x = 1 |
| 1711 |
A().f(1) |
| 1712 |
A().f('x') # fail |
| 1713 |
[builtins fixtures/primitives.pyi] |
| 1714 |
[out] |
| 1715 |
main:6: error: Unsupported operand types for + ("str" and "int") |
| 1716 |
main:11: error: Unsupported operand types for + ("int" and "str") |
| 1717 |
main:15: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" |
| 1718 |
|
| 1719 |
[case testUnconditionalRedefinitionOfConditionalMethod] |
| 1720 |
from typing import Any |
| 1721 |
x = None # type: Any |
| 1722 |
class A: |
| 1723 |
if x: |
| 1724 |
def f(self): pass |
| 1725 |
def f(self): pass # E: Name "f" already defined on line 5 |
| 1726 |
|
| 1727 |
[case testIncompatibleConditionalMethodDefinition] |
| 1728 |
from typing import Any |
| 1729 |
x = None # type: Any |
| 1730 |
class A: |
| 1731 |
if x: |
| 1732 |
def f(self, x: int) -> None: pass |
| 1733 |
else: |
| 1734 |
def f(self, x): pass # E: All conditional function variants must have identical signatures \ |
| 1735 |
# N: Original: \ |
| 1736 |
# N: def f(self: A, x: int) -> None \ |
| 1737 |
# N: Redefinition: \ |
| 1738 |
# N: def f(self: A, x: Any) -> Any |
| 1739 |
[out] |
| 1740 |
|
| 1741 |
[case testConditionalFunctionDefinitionInTry] |
| 1742 |
import typing |
| 1743 |
try: |
| 1744 |
def f(x: int) -> None: pass |
| 1745 |
except: |
| 1746 |
def g(x: str) -> None: pass |
| 1747 |
f(1) |
| 1748 |
f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1749 |
g('x') |
| 1750 |
g(1) # E: Argument 1 to "g" has incompatible type "int"; expected "str" |
| 1751 |
|
| 1752 |
[case testConditionalMethodDefinitionUsingDecorator] |
| 1753 |
from typing import Callable |
| 1754 |
|
| 1755 |
def dec(f) -> Callable[['A', int], None]: pass |
| 1756 |
|
| 1757 |
class A: |
| 1758 |
x = int() |
| 1759 |
if x: |
| 1760 |
@dec |
| 1761 |
def f(self): pass |
| 1762 |
else: |
| 1763 |
def f(self, x: int) -> None: pass |
| 1764 |
|
| 1765 |
|
| 1766 |
-- Callable with specific arg list |
| 1767 |
-- ------------------------------- |
| 1768 |
|
| 1769 |
|
| 1770 |
[case testCallableWithNamedArg] |
| 1771 |
from typing import Callable |
| 1772 |
from mypy_extensions import Arg |
| 1773 |
|
| 1774 |
def a(f: Callable[[Arg(int, 'x')], int]): |
| 1775 |
f(x=4) |
| 1776 |
f(5) |
| 1777 |
f(y=3) # E: Unexpected keyword argument "y" |
| 1778 |
|
| 1779 |
[builtins fixtures/dict.pyi] |
| 1780 |
|
| 1781 |
[case testCallableWithOptionalArg] |
| 1782 |
from typing import Callable |
| 1783 |
from mypy_extensions import DefaultArg |
| 1784 |
|
| 1785 |
def a(f: Callable[[DefaultArg(int, 'x')], int]): |
| 1786 |
f(x=4) |
| 1787 |
f(2) |
| 1788 |
f() |
| 1789 |
f(y=3) # E: Unexpected keyword argument "y" |
| 1790 |
f("foo") # E: Argument 1 has incompatible type "str"; expected "int" |
| 1791 |
[builtins fixtures/dict.pyi] |
| 1792 |
|
| 1793 |
[case testCallableWithNamedArgFromExpr] |
| 1794 |
from typing import Callable |
| 1795 |
from mypy_extensions import Arg |
| 1796 |
|
| 1797 |
F = Callable[[Arg(int, 'x')], int] |
| 1798 |
|
| 1799 |
def a(f: F): |
| 1800 |
f(x=4) |
| 1801 |
f(5) |
| 1802 |
f(y=3) # E: Unexpected keyword argument "y" |
| 1803 |
|
| 1804 |
[builtins fixtures/dict.pyi] |
| 1805 |
|
| 1806 |
[case testCallableWithOptionalArgFromExpr] |
| 1807 |
from typing import Callable |
| 1808 |
from mypy_extensions import DefaultArg |
| 1809 |
|
| 1810 |
F = Callable[[DefaultArg(int, 'x')], int] |
| 1811 |
def a(f: F): |
| 1812 |
f(x=4) |
| 1813 |
f(2) |
| 1814 |
f() |
| 1815 |
f(y=3) # E: Unexpected keyword argument "y" |
| 1816 |
f("foo") # E: Argument 1 has incompatible type "str"; expected "int" |
| 1817 |
[builtins fixtures/dict.pyi] |
| 1818 |
|
| 1819 |
[case testCallableParsingInInheritance] |
| 1820 |
|
| 1821 |
from collections import namedtuple |
| 1822 |
class C(namedtuple('t', 'x')): |
| 1823 |
pass |
| 1824 |
[builtins fixtures/tuple.pyi] |
| 1825 |
|
| 1826 |
[case testCallableParsingSameName] |
| 1827 |
from typing import Callable |
| 1828 |
|
| 1829 |
def Arg(x, y): pass |
| 1830 |
|
| 1831 |
F = Callable[[Arg(int, 'x')], int] # E: Invalid argument constructor "__main__.Arg" |
| 1832 |
|
| 1833 |
[case testCallableParsingFromExpr] |
| 1834 |
from typing import Callable, List |
| 1835 |
from mypy_extensions import Arg, VarArg, KwArg |
| 1836 |
import mypy_extensions |
| 1837 |
import types # Needed for type checking |
| 1838 |
|
| 1839 |
def WrongArg(x, y): return y |
| 1840 |
# Note that for this test, the 'Value of type "int" is not indexable' errors are silly, |
| 1841 |
# and a consequence of Callable being set to an int in the test stub. We can't set it to |
| 1842 |
# something else sensible, because other tests require the stub not have anything |
| 1843 |
# that looks like a function call. |
| 1844 |
F = Callable[[WrongArg(int, 'x')], int] # E: Invalid argument constructor "__main__.WrongArg" |
| 1845 |
G = Callable[[Arg(1, 'x')], int] # E: Invalid type: try using Literal[1] instead? |
| 1846 |
H = Callable[[VarArg(int, 'x')], int] # E: VarArg arguments should not have names |
| 1847 |
I = Callable[[VarArg(int)], int] # ok |
| 1848 |
J = Callable[[VarArg(), KwArg()], int] # ok |
| 1849 |
K = Callable[[VarArg(), int], int] # E: Required positional args may not appear after default, named or var args |
| 1850 |
L = Callable[[Arg(name='x', type=int)], int] # ok |
| 1851 |
# I have commented out the following test because I don't know how to expect the "defined here" note part of the error. |
| 1852 |
# M = Callable[[Arg(gnome='x', type=int)], int] E: Invalid type alias: expression is not a valid type E: Unexpected keyword argument "gnome" for "Arg" |
| 1853 |
N = Callable[[Arg(name=None, type=int)], int] # ok |
| 1854 |
O = Callable[[List[Arg(int)]], int] # E: Invalid type alias: expression is not a valid type \ |
| 1855 |
# E: Value of type "int" is not indexable \ |
| 1856 |
# E: Type expected within [...] |
| 1857 |
P = Callable[[mypy_extensions.VarArg(int)], int] # ok |
| 1858 |
Q = Callable[[Arg(int, type=int)], int] # E: Invalid type alias: expression is not a valid type \ |
| 1859 |
# E: Value of type "int" is not indexable \ |
| 1860 |
# E: "Arg" gets multiple values for keyword argument "type" |
| 1861 |
R = Callable[[Arg(int, 'x', name='y')], int] # E: Invalid type alias: expression is not a valid type \ |
| 1862 |
# E: Value of type "int" is not indexable \ |
| 1863 |
# E: "Arg" gets multiple values for keyword argument "name" |
| 1864 |
[builtins fixtures/dict.pyi] |
| 1865 |
|
| 1866 |
[case testCallableParsing] |
| 1867 |
from typing import Callable |
| 1868 |
from mypy_extensions import Arg, VarArg, KwArg |
| 1869 |
|
| 1870 |
def WrongArg(x, y): return y |
| 1871 |
|
| 1872 |
def b(f: Callable[[Arg(1, 'x')], int]): pass # Invalid type. Try using Literal[1] instead? |
| 1873 |
def d(f: Callable[[VarArg(int)], int]): pass # ok |
| 1874 |
def e(f: Callable[[VarArg(), KwArg()], int]): pass # ok |
| 1875 |
def g(f: Callable[[Arg(name='x', type=int)], int]): pass # ok |
| 1876 |
def h(f: Callable[[Arg(gnome='x', type=int)], int]): pass # E: Unexpected argument "gnome" for argument constructor |
| 1877 |
def i(f: Callable[[Arg(name=None, type=int)], int]): pass # ok |
| 1878 |
def j(f: Callable[[Arg(int, 'x', name='y')], int]): pass # E: "Arg" gets multiple values for keyword argument "name" |
| 1879 |
def k(f: Callable[[Arg(int, type=int)], int]): pass # E: "Arg" gets multiple values for keyword argument "type" |
| 1880 |
|
| 1881 |
[builtins fixtures/dict.pyi] |
| 1882 |
|
| 1883 |
[case testCallableTypeAnalysis] |
| 1884 |
from typing import Callable |
| 1885 |
from mypy_extensions import Arg, VarArg as VARG, KwArg |
| 1886 |
import mypy_extensions as ext |
| 1887 |
|
| 1888 |
def WrongArg(x, y): return y |
| 1889 |
def a(f: Callable[[WrongArg(int, 'x')], int]): pass # E: Invalid argument constructor "__main__.WrongArg" |
| 1890 |
def b(f: Callable[[BadArg(int, 'x')], int]): pass # E: Name "BadArg" is not defined |
| 1891 |
def d(f: Callable[[ext.VarArg(int)], int]): pass # ok |
| 1892 |
def e(f: Callable[[VARG(), ext.KwArg()], int]): pass # ok |
| 1893 |
def g(f: Callable[[ext.Arg(name='x', type=int)], int]): pass # ok |
| 1894 |
def i(f: Callable[[Arg(name=None, type=int)], int]): pass # ok |
| 1895 |
|
| 1896 |
def f1(*args) -> int: pass |
| 1897 |
def f2(*args, **kwargs) -> int: pass |
| 1898 |
|
| 1899 |
d(f1) |
| 1900 |
e(f2) |
| 1901 |
d(f2) |
| 1902 |
e(f1) |
| 1903 |
|
| 1904 |
[builtins fixtures/dict.pyi] |
| 1905 |
|
| 1906 |
[case testCallableWrongTypeType] |
| 1907 |
from typing import Callable |
| 1908 |
from mypy_extensions import Arg |
| 1909 |
def b(f: Callable[[Arg(1, 'x')], int]): pass # E: Invalid type: try using Literal[1] instead? |
| 1910 |
[builtins fixtures/dict.pyi] |
| 1911 |
|
| 1912 |
[case testCallableTooManyVarArg] |
| 1913 |
from typing import Callable |
| 1914 |
from mypy_extensions import VarArg |
| 1915 |
def c(f: Callable[[VarArg(int, 'x')], int]): pass # E: VarArg arguments should not have names |
| 1916 |
[builtins fixtures/dict.pyi] |
| 1917 |
|
| 1918 |
[case testCallableFastParseGood] |
| 1919 |
from typing import Callable |
| 1920 |
from mypy_extensions import VarArg, Arg, KwArg |
| 1921 |
def d(f: Callable[[VarArg(int)], int]): pass # ok |
| 1922 |
def e(f: Callable[[VarArg(), KwArg()], int]): pass # ok |
| 1923 |
def g(f: Callable[[Arg(name='x', type=int)], int]): pass # ok |
| 1924 |
def i(f: Callable[[Arg(name=None, type=int)], int]): pass # ok |
| 1925 |
[builtins fixtures/dict.pyi] |
| 1926 |
|
| 1927 |
[case testCallableFastParseBadArgArgName] |
| 1928 |
from typing import Callable |
| 1929 |
from mypy_extensions import Arg |
| 1930 |
def h(f: Callable[[Arg(gnome='x', type=int)], int]): pass # E: Unexpected argument "gnome" for argument constructor |
| 1931 |
[builtins fixtures/dict.pyi] |
| 1932 |
|
| 1933 |
[case testCallableKindsOrdering] |
| 1934 |
from typing import Callable, Any |
| 1935 |
from mypy_extensions import Arg, VarArg, KwArg, DefaultArg, NamedArg |
| 1936 |
|
| 1937 |
def f(f: Callable[[VarArg(), int], int]): pass # E: Required positional args may not appear after default, named or var args |
| 1938 |
def g(f: Callable[[VarArg(), VarArg()], int]): pass # E: Var args may not appear after named or var args |
| 1939 |
def h(f: Callable[[KwArg(), KwArg()], int]): pass # E: You may only have one **kwargs argument |
| 1940 |
def i(f: Callable[[DefaultArg(), int], int]): pass # E: Required positional args may not appear after default, named or var args |
| 1941 |
def j(f: Callable[[NamedArg(Any, 'x'), DefaultArg(int, 'y')], int]): pass # E: Positional default args may not appear after named or var args |
| 1942 |
def k(f: Callable[[KwArg(), NamedArg(Any, 'x')], int]): pass # E: A **kwargs argument must be the last argument |
| 1943 |
[builtins fixtures/dict.pyi] |
| 1944 |
|
| 1945 |
[case testCallableDuplicateNames] |
| 1946 |
from typing import Callable |
| 1947 |
from mypy_extensions import Arg, VarArg, KwArg, DefaultArg |
| 1948 |
|
| 1949 |
def f(f: Callable[[Arg(int, 'x'), int, Arg(int, 'x')], int]): pass # E: Duplicate parameter "x" in Callable |
| 1950 |
|
| 1951 |
[builtins fixtures/dict.pyi] |
| 1952 |
|
| 1953 |
|
| 1954 |
[case testCallableWithKeywordOnlyArg] |
| 1955 |
from typing import Callable |
| 1956 |
from mypy_extensions import NamedArg |
| 1957 |
|
| 1958 |
def a(f: Callable[[NamedArg(int, 'x')], int]): |
| 1959 |
f(x=4) |
| 1960 |
f(2) # E: Too many positional arguments |
| 1961 |
f() # E: Missing named argument "x" |
| 1962 |
f(y=3) # E: Unexpected keyword argument "y" |
| 1963 |
f(x="foo") # E: Argument "x" has incompatible type "str"; expected "int" |
| 1964 |
[builtins fixtures/dict.pyi] |
| 1965 |
|
| 1966 |
[case testCallableWithKeywordOnlyOptionalArg] |
| 1967 |
from typing import Callable |
| 1968 |
from mypy_extensions import DefaultNamedArg |
| 1969 |
|
| 1970 |
def a(f: Callable[[DefaultNamedArg(int, 'x')], int]): |
| 1971 |
f(x=4) |
| 1972 |
f(2) # E: Too many positional arguments |
| 1973 |
f() |
| 1974 |
f(y=3) # E: Unexpected keyword argument "y" |
| 1975 |
f(x="foo") # E: Argument "x" has incompatible type "str"; expected "int" |
| 1976 |
[builtins fixtures/dict.pyi] |
| 1977 |
|
| 1978 |
[case testCallableWithKwargs] |
| 1979 |
from typing import Callable |
| 1980 |
from mypy_extensions import KwArg |
| 1981 |
|
| 1982 |
def a(f: Callable[[KwArg(int)], int]): |
| 1983 |
f(x=4) |
| 1984 |
f(2) # E: Too many arguments |
| 1985 |
f() |
| 1986 |
f(y=3) |
| 1987 |
f(x=4, y=3, z=10) |
| 1988 |
f(x="foo") # E: Argument "x" has incompatible type "str"; expected "int" |
| 1989 |
[builtins fixtures/dict.pyi] |
| 1990 |
|
| 1991 |
|
| 1992 |
[case testCallableWithVarArg] |
| 1993 |
from typing import Callable |
| 1994 |
from mypy_extensions import VarArg |
| 1995 |
|
| 1996 |
def a(f: Callable[[VarArg(int)], int]): |
| 1997 |
f(x=4) # E: Unexpected keyword argument "x" |
| 1998 |
f(2) |
| 1999 |
f() |
| 2000 |
f(3, 4, 5) |
| 2001 |
f("a") # E: Argument 1 has incompatible type "str"; expected "int" |
| 2002 |
[builtins fixtures/dict.pyi] |
| 2003 |
|
| 2004 |
[case testCallableArgKindSubtyping] |
| 2005 |
from typing import Callable |
| 2006 |
from mypy_extensions import Arg, DefaultArg |
| 2007 |
|
| 2008 |
int_str_fun: Callable[[int, str], str] |
| 2009 |
int_opt_str_fun: Callable[[int, DefaultArg(str, None)], str] |
| 2010 |
int_named_str_fun: Callable[[int, Arg(str, 's')], str] |
| 2011 |
|
| 2012 |
def isf(ii: int, ss: str) -> str: |
| 2013 |
return ss |
| 2014 |
|
| 2015 |
def iosf(i: int, s: str = "bar") -> str: |
| 2016 |
return s |
| 2017 |
|
| 2018 |
def isf_unnamed(__i: int, __s: str) -> str: |
| 2019 |
return __s |
| 2020 |
|
| 2021 |
int_str_fun = isf |
| 2022 |
int_str_fun = isf_unnamed |
| 2023 |
int_named_str_fun = isf_unnamed # E: Incompatible types in assignment (expression has type "Callable[[int, str], str]", variable has type "def (int, /, s: str) -> str") |
| 2024 |
int_opt_str_fun = iosf |
| 2025 |
int_str_fun = iosf |
| 2026 |
int_opt_str_fun = isf # E: Incompatible types in assignment (expression has type "def isf(ii: int, ss: str) -> str", variable has type "def (int, str = ..., /) -> str") |
| 2027 |
|
| 2028 |
int_named_str_fun = isf # E: Incompatible types in assignment (expression has type "def isf(ii: int, ss: str) -> str", variable has type "def (int, /, s: str) -> str") |
| 2029 |
int_named_str_fun = iosf |
| 2030 |
|
| 2031 |
[builtins fixtures/dict.pyi] |
| 2032 |
|
| 2033 |
-- Callable[..., T] |
| 2034 |
-- ---------------- |
| 2035 |
|
| 2036 |
|
| 2037 |
[case testCallableWithArbitraryArgs] |
| 2038 |
from typing import Callable |
| 2039 |
def f(x: Callable[..., int]) -> None: |
| 2040 |
x() |
| 2041 |
x(1) |
| 2042 |
x(z=1) |
| 2043 |
x() + '' # E: Unsupported operand types for + ("int" and "str") |
| 2044 |
[out] |
| 2045 |
|
| 2046 |
[case testCallableWithArbitraryArgs2] |
| 2047 |
from typing import Callable |
| 2048 |
def f(x: Callable[..., int]) -> None: |
| 2049 |
x(*[1], **{'x': 2}) |
| 2050 |
[builtins fixtures/dict.pyi] |
| 2051 |
|
| 2052 |
[case testCastWithCallableAndArbitraryArgs] |
| 2053 |
from typing import Callable, cast |
| 2054 |
f = cast(Callable[..., int], None) |
| 2055 |
f(x=4) + '' # E: Unsupported operand types for + ("int" and "str") |
| 2056 |
|
| 2057 |
[case testCallableWithArbitraryArgsInErrorMessage] |
| 2058 |
from typing import Callable |
| 2059 |
def f(x: Callable[..., int]) -> None: |
| 2060 |
if int(): |
| 2061 |
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[..., int]") |
| 2062 |
[out] |
| 2063 |
|
| 2064 |
[case testCallableWithArbitraryArgsInGenericFunction] |
| 2065 |
from typing import Callable, TypeVar |
| 2066 |
T = TypeVar('T') |
| 2067 |
def f(x: Callable[..., T]) -> T: pass |
| 2068 |
def g(*x: int) -> str: pass |
| 2069 |
x = f(g) |
| 2070 |
x + 1 # E: Unsupported left operand type for + ("str") |
| 2071 |
[builtins fixtures/list.pyi] |
| 2072 |
|
| 2073 |
[case testCallableWithArbitraryArgsSubtyping] |
| 2074 |
from typing import Callable |
| 2075 |
def f(x: Callable[..., int]) -> None: pass |
| 2076 |
def g1(): pass |
| 2077 |
def g2(x, y) -> int: pass |
| 2078 |
def g3(*, y: str) -> int: pass |
| 2079 |
def g4(*, y: int) -> str: pass |
| 2080 |
f(g1) |
| 2081 |
f(g2) |
| 2082 |
f(g3) |
| 2083 |
f(g4) # E: Argument 1 to "f" has incompatible type "def g4(*, y: int) -> str"; expected "Callable[..., int]" |
| 2084 |
|
| 2085 |
[case testCallableWithArbitraryArgsSubtypingWithGenericFunc] |
| 2086 |
from typing import Callable, TypeVar |
| 2087 |
T = TypeVar('T') |
| 2088 |
def f(x: Callable[..., int]) -> None: pass |
| 2089 |
def g1(x: T) -> int: pass |
| 2090 |
def g2(*x: T) -> int: pass |
| 2091 |
def g3(*x: T) -> T: pass |
| 2092 |
f(g1) |
| 2093 |
f(g2) |
| 2094 |
f(g3) |
| 2095 |
[builtins fixtures/tuple.pyi] |
| 2096 |
|
| 2097 |
-- (...) -> T |
| 2098 |
-- ---------------- |
| 2099 |
|
| 2100 |
[case testEllipsisWithArbitraryArgsOnBareFunction] |
| 2101 |
def f(x, y, z): # type: (...) -> None |
| 2102 |
pass |
| 2103 |
f(1, "hello", []) |
| 2104 |
f(x=1, y="hello", z=[]) |
| 2105 |
[builtins fixtures/dict.pyi] |
| 2106 |
|
| 2107 |
[case testEllipsisWithArbitraryArgsOnBareFunctionWithDefaults] |
| 2108 |
def f(x, y=1, z="hey"): # type: (...) -> None |
| 2109 |
pass |
| 2110 |
f(1, "hello", []) |
| 2111 |
f(x=1, y="hello", z=[]) |
| 2112 |
[builtins fixtures/dict.pyi] |
| 2113 |
|
| 2114 |
[case testEllipsisWithArbitraryArgsOnBareFunctionWithKwargs] |
| 2115 |
from typing import Dict |
| 2116 |
def f(x, **kwargs): # type: (...) -> None |
| 2117 |
success_dict_type = kwargs # type: Dict[str, str] |
| 2118 |
failure_dict_type = kwargs # type: Dict[int, str] # E: Incompatible types in assignment (expression has type "dict[str, Any]", variable has type "dict[int, str]") |
| 2119 |
f(1, thing_in_kwargs=["hey"]) |
| 2120 |
[builtins fixtures/dict.pyi] |
| 2121 |
[out] |
| 2122 |
|
| 2123 |
[case testEllipsisWithArbitraryArgsOnBareFunctionWithVarargs] |
| 2124 |
from typing import Tuple, Any |
| 2125 |
def f(x, *args): # type: (...) -> None |
| 2126 |
success_tuple_type = args # type: Tuple[Any, ...] |
| 2127 |
fail_tuple_type = args # type: None # E: Incompatible types in assignment (expression has type "tuple[Any, ...]", variable has type "None") |
| 2128 |
f(1, "hello") |
| 2129 |
[builtins fixtures/tuple.pyi] |
| 2130 |
[out] |
| 2131 |
|
| 2132 |
[case testEllipsisWithArbitraryArgsOnInstanceMethod] |
| 2133 |
class A: |
| 2134 |
def f(self, x, y, z): # type: (...) -> None |
| 2135 |
pass |
| 2136 |
|
| 2137 |
[case testEllipsisWithArbitraryArgsOnClassMethod] |
| 2138 |
class A: |
| 2139 |
@classmethod |
| 2140 |
def f(cls, x, y, z): # type: (...) -> None |
| 2141 |
pass |
| 2142 |
[builtins fixtures/classmethod.pyi] |
| 2143 |
|
| 2144 |
[case testEllipsisWithArbitraryArgsOnStaticMethod] |
| 2145 |
class A: |
| 2146 |
@staticmethod |
| 2147 |
def f(x, y, z): # type: (...) -> None |
| 2148 |
pass |
| 2149 |
[builtins fixtures/staticmethod.pyi] |
| 2150 |
|
| 2151 |
[case testEllipsisWithSomethingAfterItFails_no_native_parse] |
| 2152 |
def f(x, y, z): # type: (..., int) -> None |
| 2153 |
pass |
| 2154 |
[out] |
| 2155 |
main:1: error: Ellipses cannot accompany other parameter types in function type signature |
| 2156 |
|
| 2157 |
[case testEllipsisWithSomethingBeforeItFails_no_native_parse] |
| 2158 |
def f(x, y, z): # type: (int, ...) -> None |
| 2159 |
pass |
| 2160 |
[out] |
| 2161 |
main:1: error: Ellipses cannot accompany other parameter types in function type signature |
| 2162 |
|
| 2163 |
[case testRejectCovariantArgument] |
| 2164 |
from typing import TypeVar, Generic |
| 2165 |
|
| 2166 |
t = TypeVar('t', covariant=True) |
| 2167 |
class A(Generic[t]): |
| 2168 |
def foo(self, x: t) -> None: |
| 2169 |
return None |
| 2170 |
[builtins fixtures/bool.pyi] |
| 2171 |
[out] |
| 2172 |
main:5: error: Cannot use a covariant type variable as a parameter |
| 2173 |
|
| 2174 |
[case testRejectCovariantArgumentSplitLine] |
| 2175 |
from typing import TypeVar, Generic |
| 2176 |
|
| 2177 |
t = TypeVar('t', covariant=True) |
| 2178 |
class A(Generic[t]): |
| 2179 |
def foo(self, |
| 2180 |
x: t) -> None: |
| 2181 |
return None |
| 2182 |
[builtins fixtures/bool.pyi] |
| 2183 |
[out] |
| 2184 |
main:6: error: Cannot use a covariant type variable as a parameter |
| 2185 |
|
| 2186 |
[case testRejectCovariantArgumentInLambda] |
| 2187 |
from typing import TypeVar, Generic, Callable |
| 2188 |
|
| 2189 |
t = TypeVar('t', covariant=True) |
| 2190 |
class Thing(Generic[t]): |
| 2191 |
def chain(self, func: Callable[[t], None]) -> None: pass |
| 2192 |
def end(self) -> None: |
| 2193 |
return self.chain( # Note that lambda args have no line numbers |
| 2194 |
lambda _: None) |
| 2195 |
[builtins fixtures/bool.pyi] |
| 2196 |
[out] |
| 2197 |
main:8: error: Cannot use a covariant type variable as a parameter |
| 2198 |
|
| 2199 |
[case testRejectCovariantArgumentInLambdaSplitLine] |
| 2200 |
from typing import TypeVar, Generic, Callable |
| 2201 |
|
| 2202 |
[case testRejectContravariantReturnType] |
| 2203 |
# flags: --no-strict-optional |
| 2204 |
from typing import TypeVar, Generic |
| 2205 |
|
| 2206 |
t = TypeVar('t', contravariant=True) |
| 2207 |
class A(Generic[t]): |
| 2208 |
def foo(self) -> t: |
| 2209 |
return None |
| 2210 |
[builtins fixtures/bool.pyi] |
| 2211 |
[out] |
| 2212 |
main:6: error: Cannot use a contravariant type variable as return type |
| 2213 |
|
| 2214 |
[case testAcceptCovariantReturnType] |
| 2215 |
# flags: --no-strict-optional |
| 2216 |
from typing import TypeVar, Generic |
| 2217 |
|
| 2218 |
t = TypeVar('t', covariant=True) |
| 2219 |
class A(Generic[t]): |
| 2220 |
def foo(self) -> t: |
| 2221 |
return None |
| 2222 |
[builtins fixtures/bool.pyi] |
| 2223 |
|
| 2224 |
[case testAcceptContravariantArgument] |
| 2225 |
from typing import TypeVar, Generic |
| 2226 |
|
| 2227 |
t = TypeVar('t', contravariant=True) |
| 2228 |
class A(Generic[t]): |
| 2229 |
def foo(self, x: t) -> None: |
| 2230 |
return None |
| 2231 |
[builtins fixtures/bool.pyi] |
| 2232 |
|
| 2233 |
|
| 2234 |
-- Redefining functions |
| 2235 |
-- -------------------- |
| 2236 |
|
| 2237 |
|
| 2238 |
[case testRedefineFunction] |
| 2239 |
from typing import Any |
| 2240 |
def f(x) -> Any: pass |
| 2241 |
def g(x, y): pass |
| 2242 |
def h(x): pass |
| 2243 |
def j(y) -> Any: pass |
| 2244 |
f = h |
| 2245 |
f = j # E: Incompatible types in assignment (expression has type "def j(y: Any) -> Any", variable has type "def f(x: Any) -> Any") |
| 2246 |
f = g # E: Incompatible types in assignment (expression has type "Callable[[Any, Any], Any]", variable has type "Callable[[Any], Any]") |
| 2247 |
|
| 2248 |
[case testRedefineFunction2] |
| 2249 |
def f() -> None: pass |
| 2250 |
def f() -> None: pass # E: Name "f" already defined on line 1 |
| 2251 |
|
| 2252 |
|
| 2253 |
-- Special cases |
| 2254 |
-- ------------- |
| 2255 |
|
| 2256 |
|
| 2257 |
[case testFunctionDefinitionWithForStatement] |
| 2258 |
for _ in [1]: |
| 2259 |
def f(): pass |
| 2260 |
else: |
| 2261 |
def g(): pass |
| 2262 |
f() |
| 2263 |
g() |
| 2264 |
[builtins fixtures/list.pyi] |
| 2265 |
|
| 2266 |
[case testFunctionDefinitionWithWhileStatement] |
| 2267 |
while bool(): |
| 2268 |
def f(): pass |
| 2269 |
else: |
| 2270 |
def g(): pass |
| 2271 |
f() |
| 2272 |
g() |
| 2273 |
[builtins fixtures/bool.pyi] |
| 2274 |
|
| 2275 |
[case testBareCallable] |
| 2276 |
from typing import Callable, Any |
| 2277 |
|
| 2278 |
def foo(f: Callable) -> bool: |
| 2279 |
return f() |
| 2280 |
|
| 2281 |
def f1() -> bool: |
| 2282 |
return False |
| 2283 |
|
| 2284 |
foo(f1) |
| 2285 |
[builtins fixtures/bool.pyi] |
| 2286 |
|
| 2287 |
[case testFunctionNestedWithinWith] |
| 2288 |
from typing import Any |
| 2289 |
a = 1 # type: Any |
| 2290 |
with a: |
| 2291 |
def f() -> None: |
| 2292 |
pass |
| 2293 |
f(1) # E: Too many arguments for "f" |
| 2294 |
|
| 2295 |
|
| 2296 |
[case testNameForDecoratorMethod] |
| 2297 |
from typing import Callable |
| 2298 |
|
| 2299 |
class A: |
| 2300 |
def f(self) -> None: |
| 2301 |
# In particular, test that the error message contains "g" of "A". |
| 2302 |
self.g() # E: Too few arguments for "g" of "A" |
| 2303 |
self.g(1) |
| 2304 |
@dec |
| 2305 |
def g(self, x: str) -> None: pass |
| 2306 |
|
| 2307 |
def dec(f: Callable[[A, str], None]) -> Callable[[A, int], None]: pass |
| 2308 |
[out] |
| 2309 |
|
| 2310 |
[case testUnknownFunctionNotCallable] |
| 2311 |
from typing import TypeVar |
| 2312 |
|
| 2313 |
def f() -> None: |
| 2314 |
pass |
| 2315 |
def g(x: int) -> None: |
| 2316 |
pass |
| 2317 |
h = f if bool() else g |
| 2318 |
reveal_type(h) # N: Revealed type is "(def ()) | (def (x: builtins.int))" |
| 2319 |
h(7) # E: Too many arguments for "f" |
| 2320 |
|
| 2321 |
T = TypeVar("T") |
| 2322 |
def join(x: T, y: T) -> T: ... |
| 2323 |
|
| 2324 |
h2 = join(f, g) |
| 2325 |
reveal_type(h2) # N: Revealed type is "builtins.function" |
| 2326 |
h2(7) # E: Cannot call function of unknown type |
| 2327 |
|
| 2328 |
h3 = join(g, f) |
| 2329 |
reveal_type(h3) # N: Revealed type is "builtins.function" |
| 2330 |
h3(7) # E: Cannot call function of unknown type |
| 2331 |
[builtins fixtures/bool.pyi] |
| 2332 |
|
| 2333 |
[case testFunctionWithNameUnderscore] |
| 2334 |
def _(x: int) -> None: pass |
| 2335 |
|
| 2336 |
_(1) |
| 2337 |
_('x') # E: Argument 1 to "_" has incompatible type "str"; expected "int" |
| 2338 |
|
| 2339 |
-- Positional-only arguments |
| 2340 |
-- ------------------------- |
| 2341 |
|
| 2342 |
[case testPositionalOnlyArg] |
| 2343 |
def f(__a: int) -> None: pass |
| 2344 |
def g(__a__: int) -> None: pass |
| 2345 |
|
| 2346 |
f(1) |
| 2347 |
f(__a=1) # E: Unexpected keyword argument "__a" for "f" |
| 2348 |
|
| 2349 |
g(1) |
| 2350 |
# Argument names that also end with __ are not positional-only. |
| 2351 |
g(__a__=1) |
| 2352 |
|
| 2353 |
[builtins fixtures/bool.pyi] |
| 2354 |
[out] |
| 2355 |
|
| 2356 |
[case testMagicMethodPositionalOnlyArg] |
| 2357 |
class A(object): |
| 2358 |
def __eq__(self, other) -> bool: return True # We are all equal. |
| 2359 |
|
| 2360 |
a = A() |
| 2361 |
a.__eq__(a) |
| 2362 |
a.__eq__(other=a) # E: Unexpected keyword argument "other" for "__eq__" of "A" |
| 2363 |
|
| 2364 |
[builtins fixtures/bool.pyi] |
| 2365 |
|
| 2366 |
[case testMagicMethodPositionalOnlyArgFastparse] |
| 2367 |
|
| 2368 |
|
| 2369 |
class A(object): |
| 2370 |
def __eq__(self, other) -> bool: return True # We are all equal. |
| 2371 |
|
| 2372 |
a = A() |
| 2373 |
a.__eq__(a) |
| 2374 |
a.__eq__(other=a) # E: Unexpected keyword argument "other" for "__eq__" of "A" |
| 2375 |
|
| 2376 |
[builtins fixtures/bool.pyi] |
| 2377 |
|
| 2378 |
-- Type variable shenanigans |
| 2379 |
-- ------------------------- |
| 2380 |
|
| 2381 |
[case testGenericFunctionTypeDecl] |
| 2382 |
from typing import Callable, TypeVar |
| 2383 |
|
| 2384 |
T = TypeVar('T') |
| 2385 |
|
| 2386 |
f: Callable[[T], T] |
| 2387 |
reveal_type(f) # N: Revealed type is "def [T] (T`-1) -> T`-1" |
| 2388 |
def g(__x: T) -> T: pass |
| 2389 |
f = g |
| 2390 |
reveal_type(f) # N: Revealed type is "def [T] (T`-1) -> T`-1" |
| 2391 |
i = f(3) |
| 2392 |
reveal_type(i) # N: Revealed type is "builtins.int" |
| 2393 |
|
| 2394 |
[case testFunctionReturningGenericFunction] |
| 2395 |
from typing import Callable, TypeVar |
| 2396 |
|
| 2397 |
T = TypeVar('T') |
| 2398 |
def deco() -> Callable[[T], T]: pass |
| 2399 |
reveal_type(deco) # N: Revealed type is "def () -> def [T] (T`-1) -> T`-1" |
| 2400 |
f = deco() |
| 2401 |
reveal_type(f) # N: Revealed type is "def [T] (T`1) -> T`1" |
| 2402 |
i = f(3) |
| 2403 |
reveal_type(i) # N: Revealed type is "builtins.int" |
| 2404 |
|
| 2405 |
[case testFunctionReturningGenericFunctionPartialBinding] |
| 2406 |
from typing import Callable, TypeVar |
| 2407 |
|
| 2408 |
T = TypeVar('T') |
| 2409 |
U = TypeVar('U') |
| 2410 |
|
| 2411 |
def deco(x: U) -> Callable[[T, U], T]: pass |
| 2412 |
reveal_type(deco) # N: Revealed type is "def [U] (x: U`-1) -> def [T] (T`-2, U`-1) -> T`-2" |
| 2413 |
f = deco("foo") |
| 2414 |
reveal_type(f) # N: Revealed type is "def [T] (T`1, builtins.str) -> T`1" |
| 2415 |
i = f(3, "eggs") |
| 2416 |
reveal_type(i) # N: Revealed type is "builtins.int" |
| 2417 |
|
| 2418 |
[case testFunctionReturningGenericFunctionTwoLevelBinding] |
| 2419 |
from typing import Callable, TypeVar |
| 2420 |
|
| 2421 |
T = TypeVar('T') |
| 2422 |
R = TypeVar('R') |
| 2423 |
def deco() -> Callable[[T], Callable[[T, R], R]]: pass |
| 2424 |
f = deco() |
| 2425 |
reveal_type(f) # N: Revealed type is "def [T] (T`2) -> def [R] (T`2, R`1) -> R`1" |
| 2426 |
g = f(3) |
| 2427 |
reveal_type(g) # N: Revealed type is "def [R] (builtins.int, R`3) -> R`3" |
| 2428 |
s = g(4, "foo") |
| 2429 |
reveal_type(s) # N: Revealed type is "builtins.str" |
| 2430 |
|
| 2431 |
[case testGenericFunctionReturnAsDecorator] |
| 2432 |
from typing import Callable, TypeVar |
| 2433 |
|
| 2434 |
T = TypeVar('T') |
| 2435 |
def deco(__i: int) -> Callable[[T], T]: pass |
| 2436 |
|
| 2437 |
@deco(3) |
| 2438 |
def lol(x: int) -> str: ... |
| 2439 |
|
| 2440 |
reveal_type(lol) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 2441 |
s = lol(4) |
| 2442 |
reveal_type(s) # N: Revealed type is "builtins.str" |
| 2443 |
|
| 2444 |
[case testGenericFunctionOnReturnTypeOnly] |
| 2445 |
from typing import TypeVar, List |
| 2446 |
|
| 2447 |
T = TypeVar('T') |
| 2448 |
|
| 2449 |
def make_list() -> List[T]: pass |
| 2450 |
|
| 2451 |
l: List[int] = make_list() |
| 2452 |
|
| 2453 |
bad = make_list() # E: Need type annotation for "bad" (hint: "bad: list[<type>] = ...") |
| 2454 |
[builtins fixtures/list.pyi] |
| 2455 |
|
| 2456 |
[case testAnonymousArgumentError] |
| 2457 |
def foo(__b: int, x: int, y: int) -> int: pass |
| 2458 |
foo(x=2, y=2) # E: Too few arguments for "foo" |
| 2459 |
foo(y=2) # E: Too few arguments for "foo" |
| 2460 |
|
| 2461 |
[case testMissingArgumentError] |
| 2462 |
def f(a, b, c, d=None) -> None: pass |
| 2463 |
f(1, 2, d=3) # E: Missing positional argument "c" in call to "f" |
| 2464 |
|
| 2465 |
[case testMissingArgumentErrorMoreThanOneOptional] |
| 2466 |
def f(a: int, b=None, c=None) -> None: pass |
| 2467 |
f(b=4) # E: Missing positional argument "a" in call to "f" |
| 2468 |
|
| 2469 |
[case testMissingArgumentsError] |
| 2470 |
def f(a, b, c, d=None) -> None: pass |
| 2471 |
f(1, d=3) # E: Missing positional arguments "b", "c" in call to "f" |
| 2472 |
|
| 2473 |
[case testReturnTypeLineNumberWithDecorator] |
| 2474 |
def dec(f): pass |
| 2475 |
|
| 2476 |
@dec |
| 2477 |
def test(a: str) -> (str,): # E: Syntax error in type annotation # N: Suggestion: Is there a spurious trailing comma? |
| 2478 |
return None |
| 2479 |
|
| 2480 |
[case testReturnTypeLineNumberNewLine] |
| 2481 |
def fn(a: str |
| 2482 |
) -> badtype: # E: Name "badtype" is not defined |
| 2483 |
pass |
| 2484 |
|
| 2485 |
[case testArgumentTypeLineNumberWithDecorator] |
| 2486 |
def dec(f): pass |
| 2487 |
|
| 2488 |
@dec |
| 2489 |
def some_method(self: badtype): pass # E: Name "badtype" is not defined |
| 2490 |
|
| 2491 |
[case TestArgumentTypeLineNumberNewline] |
| 2492 |
def fn( |
| 2493 |
a: badtype) -> None: # E: Name "badtype" is not defined |
| 2494 |
pass |
| 2495 |
|
| 2496 |
[case testInferredTypeSubTypeOfReturnType] |
| 2497 |
from typing import Union, Dict, List |
| 2498 |
def f() -> List[Union[str, int]]: |
| 2499 |
x = ['a'] |
| 2500 |
return x # E: Incompatible return value type (got "list[str]", expected "list[str | int]") \ |
| 2501 |
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 2502 |
# N: Consider using "Sequence" instead, which is covariant \ |
| 2503 |
# N: Perhaps you need a type annotation for "x"? Suggestion: "list[str | int]" |
| 2504 |
|
| 2505 |
def g() -> Dict[str, Union[str, int]]: |
| 2506 |
x = {'a': 'a'} |
| 2507 |
return x # E: Incompatible return value type (got "dict[str, str]", expected "dict[str, str | int]") \ |
| 2508 |
# N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 2509 |
# N: Consider using "Mapping" instead, which is covariant in the value type \ |
| 2510 |
# N: Perhaps you need a type annotation for "x"? Suggestion: "dict[str, str | int]" |
| 2511 |
|
| 2512 |
def h() -> Dict[Union[str, int], str]: |
| 2513 |
x = {'a': 'a'} |
| 2514 |
return x # E: Incompatible return value type (got "dict[str, str]", expected "dict[str | int, str]") \ |
| 2515 |
# N: Perhaps you need a type annotation for "x"? Suggestion: "dict[str | int, str]" |
| 2516 |
|
| 2517 |
def i() -> List[Union[int, float]]: |
| 2518 |
x: List[int] = [1] |
| 2519 |
return x # E: Incompatible return value type (got "list[int]", expected "list[int | float]") \ |
| 2520 |
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 2521 |
# N: Consider using "Sequence" instead, which is covariant |
| 2522 |
|
| 2523 |
[builtins fixtures/dict.pyi] |
| 2524 |
|
| 2525 |
[case testInferredTypeNotSubTypeOfReturnType] |
| 2526 |
from typing import Union, List |
| 2527 |
def f() -> List[Union[int, float]]: |
| 2528 |
x = ['a'] |
| 2529 |
return x # E: Incompatible return value type (got "list[str]", expected "list[int | float]") |
| 2530 |
|
| 2531 |
def g() -> List[Union[str, int]]: |
| 2532 |
x = ('a', 2) |
| 2533 |
return x # E: Incompatible return value type (got "tuple[str, int]", expected "list[str | int]") |
| 2534 |
|
| 2535 |
[builtins fixtures/list.pyi] |
| 2536 |
|
| 2537 |
[case testInferredTypeIsObjectMismatch] |
| 2538 |
from typing import Union, Dict, List |
| 2539 |
def f() -> Dict[str, Union[str, int]]: |
| 2540 |
x = {'a': 'a', 'b': 2} |
| 2541 |
return x # E: Incompatible return value type (got "dict[str, object]", expected "dict[str, str | int]") |
| 2542 |
|
| 2543 |
def g() -> Dict[str, Union[str, int]]: |
| 2544 |
x: Dict[str, Union[str, int]] = {'a': 'a', 'b': 2} |
| 2545 |
return x |
| 2546 |
|
| 2547 |
def h() -> List[Union[str, int]]: |
| 2548 |
x = ['a', 2] |
| 2549 |
return x # E: Incompatible return value type (got "list[object]", expected "list[str | int]") |
| 2550 |
|
| 2551 |
def i() -> List[Union[str, int]]: |
| 2552 |
x: List[Union[str, int]] = ['a', 2] |
| 2553 |
return x |
| 2554 |
|
| 2555 |
[builtins fixtures/dict.pyi] |
| 2556 |
|
| 2557 |
[case testLambdaSemanal] |
| 2558 |
f = lambda: xyz |
| 2559 |
[out] |
| 2560 |
main:1: error: Name "xyz" is not defined |
| 2561 |
|
| 2562 |
[case testLambdaTypeCheck] |
| 2563 |
f = lambda: 1 + '1' |
| 2564 |
[out] |
| 2565 |
main:1: error: Unsupported operand types for + ("int" and "str") |
| 2566 |
|
| 2567 |
[case testLambdaTypeInference] |
| 2568 |
f = lambda: 5 |
| 2569 |
reveal_type(f) |
| 2570 |
[out] |
| 2571 |
main:2: note: Revealed type is "def () -> builtins.int" |
| 2572 |
|
| 2573 |
[case testDeferredLambdaInOverload] |
| 2574 |
from typing import Any, Callable, overload |
| 2575 |
|
| 2576 |
@overload |
| 2577 |
def option(*, callback: Callable[[str], object] = ...) -> Any: ... |
| 2578 |
@overload |
| 2579 |
def option(*, callback: Callable[[int], object] = ...) -> Any: ... |
| 2580 |
def option(**kwargs: object) -> None: pass |
| 2581 |
|
| 2582 |
def test() -> None: |
| 2583 |
option(callback=lambda x: [y for y in x]) # OK |
| 2584 |
x = C().x |
| 2585 |
|
| 2586 |
class C: |
| 2587 |
def __init__(self) -> None: |
| 2588 |
self.x = defer() |
| 2589 |
|
| 2590 |
def defer() -> int: ... |
| 2591 |
[builtins fixtures/primitives.pyi] |
| 2592 |
|
| 2593 |
[case testRevealLocalsFunction] |
| 2594 |
a = 1.0 |
| 2595 |
|
| 2596 |
class A: pass |
| 2597 |
|
| 2598 |
def f(a: int, b: int) -> int: |
| 2599 |
reveal_locals() |
| 2600 |
c = a + b |
| 2601 |
class C: pass |
| 2602 |
reveal_locals() |
| 2603 |
return c |
| 2604 |
|
| 2605 |
reveal_locals() |
| 2606 |
[out] |
| 2607 |
main:6: note: Revealed local types are: |
| 2608 |
main:6: note: a: builtins.int |
| 2609 |
main:6: note: b: builtins.int |
| 2610 |
main:9: note: Revealed local types are: |
| 2611 |
main:9: note: a: builtins.int |
| 2612 |
main:9: note: b: builtins.int |
| 2613 |
main:9: note: c: builtins.int |
| 2614 |
main:12: note: Revealed local types are: |
| 2615 |
main:12: note: a: builtins.float |
| 2616 |
|
| 2617 |
[case testNoComplainOverloadNone] |
| 2618 |
# flags: --no-strict-optional |
| 2619 |
from typing import overload, Optional |
| 2620 |
@overload |
| 2621 |
def bar(x: None) -> None: |
| 2622 |
... |
| 2623 |
@overload |
| 2624 |
def bar(x: int) -> str: |
| 2625 |
... |
| 2626 |
def bar(x: Optional[int]) -> Optional[str]: |
| 2627 |
if x is None: |
| 2628 |
return None |
| 2629 |
return "number" |
| 2630 |
|
| 2631 |
reveal_type(bar(None)) # N: Revealed type is "None" |
| 2632 |
[builtins fixtures/isinstance.pyi] |
| 2633 |
[out] |
| 2634 |
|
| 2635 |
[case testNoComplainOverloadNoneStrict] |
| 2636 |
from typing import overload, Optional |
| 2637 |
@overload |
| 2638 |
def bar(x: None) -> None: |
| 2639 |
... |
| 2640 |
@overload |
| 2641 |
def bar(x: int) -> str: |
| 2642 |
... |
| 2643 |
def bar(x: Optional[int]) -> Optional[str]: |
| 2644 |
if x is None: |
| 2645 |
return None |
| 2646 |
return "number" |
| 2647 |
|
| 2648 |
reveal_type(bar(None)) # N: Revealed type is "None" |
| 2649 |
[builtins fixtures/isinstance.pyi] |
| 2650 |
[out] |
| 2651 |
|
| 2652 |
[case testNoComplainInferredNone] |
| 2653 |
# flags: --no-strict-optional --no-local-partial-types |
| 2654 |
from typing import TypeVar, Optional |
| 2655 |
T = TypeVar('T') |
| 2656 |
def X(val: T) -> T: ... |
| 2657 |
x_in = None |
| 2658 |
def Y(x: Optional[str] = X(x_in)): ... |
| 2659 |
|
| 2660 |
xx: Optional[int] = X(x_in) |
| 2661 |
|
| 2662 |
[case testNoComplainInferredNoneStrict] |
| 2663 |
from typing import TypeVar, Optional |
| 2664 |
T = TypeVar('T') |
| 2665 |
def X(val: T) -> T: ... |
| 2666 |
x_in = X(None) |
| 2667 |
def Y(x: Optional[str] = X(x_in)): ... |
| 2668 |
|
| 2669 |
xx: Optional[int] = X(x_in) |
| 2670 |
|
| 2671 |
[case testNoComplainNoneReturnFromUntyped] |
| 2672 |
def foo() -> None: |
| 2673 |
pass |
| 2674 |
|
| 2675 |
def lol(): |
| 2676 |
x = foo() |
| 2677 |
|
| 2678 |
[case testConditionalImportFunction] |
| 2679 |
import p |
| 2680 |
[file p/__init__.py] |
| 2681 |
|
| 2682 |
if int(): |
| 2683 |
from p.a import f |
| 2684 |
elif int(): |
| 2685 |
from p.b import f |
| 2686 |
else: |
| 2687 |
from p.c import f |
| 2688 |
|
| 2689 |
[file p/a.py] |
| 2690 |
def f() -> int: ... |
| 2691 |
|
| 2692 |
[file p/b.py] |
| 2693 |
from p.d import f |
| 2694 |
|
| 2695 |
[file p/c.py] |
| 2696 |
def f() -> int: ... |
| 2697 |
|
| 2698 |
[file p/d.py] |
| 2699 |
import p |
| 2700 |
def f() -> int: ... |
| 2701 |
|
| 2702 |
[case testLambdaDefaultTypeErrors] |
| 2703 |
lambda a=(1 + 'asdf'): a # E: Unsupported operand types for + ("int" and "str") |
| 2704 |
lambda a=nonsense: a # E: Name "nonsense" is not defined |
| 2705 |
def f(x: int = i): # E: Name "i" is not defined |
| 2706 |
i = 42 |
| 2707 |
|
| 2708 |
[case testRevealTypeOfCallExpressionReturningNoneWorks] |
| 2709 |
def foo() -> None: |
| 2710 |
pass |
| 2711 |
|
| 2712 |
reveal_type(foo()) # N: Revealed type is "None" |
| 2713 |
|
| 2714 |
[case testAnyArgument] |
| 2715 |
def a(b: any): pass # E: Function "builtins.any" is not valid as a type \ |
| 2716 |
# N: Perhaps you meant "typing.Any" instead of "any"? |
| 2717 |
[builtins fixtures/any.pyi] |
| 2718 |
|
| 2719 |
[case testCallableArgument] |
| 2720 |
def a(b: callable): pass # E: Function "builtins.callable" is not valid as a type \ |
| 2721 |
# N: Perhaps you meant "typing.Callable" instead of "callable"? |
| 2722 |
[builtins fixtures/callable.pyi] |
| 2723 |
|
| 2724 |
[case testDecoratedProperty] |
| 2725 |
from typing import TypeVar, Callable, final |
| 2726 |
|
| 2727 |
T = TypeVar("T") |
| 2728 |
|
| 2729 |
def dec(f: Callable[[T], int]) -> Callable[[T], str]: ... |
| 2730 |
def dec2(f: T) -> T: ... |
| 2731 |
|
| 2732 |
class A: |
| 2733 |
@property |
| 2734 |
@dec |
| 2735 |
def f(self) -> int: pass |
| 2736 |
@property |
| 2737 |
@dec2 |
| 2738 |
def g(self) -> int: pass |
| 2739 |
reveal_type(A().f) # N: Revealed type is "builtins.str" |
| 2740 |
reveal_type(A().g) # N: Revealed type is "builtins.int" |
| 2741 |
|
| 2742 |
class B: |
| 2743 |
@final |
| 2744 |
@property |
| 2745 |
@dec |
| 2746 |
def f(self) -> int: pass |
| 2747 |
reveal_type(B().f) # N: Revealed type is "builtins.str" |
| 2748 |
|
| 2749 |
class C: |
| 2750 |
@property # E: Only instance methods can be decorated with @property |
| 2751 |
@classmethod |
| 2752 |
def f(cls) -> int: pass |
| 2753 |
reveal_type(C().f) # N: Revealed type is "builtins.int" |
| 2754 |
[builtins fixtures/property.pyi] |
| 2755 |
[out] |
| 2756 |
|
| 2757 |
[case testDecoratedPropertySetter] |
| 2758 |
from typing import TypeVar, Callable, final |
| 2759 |
|
| 2760 |
T = TypeVar("T") |
| 2761 |
def dec(f: T) -> T: ... |
| 2762 |
|
| 2763 |
class A: |
| 2764 |
@property |
| 2765 |
@dec |
| 2766 |
def f(self) -> int: pass |
| 2767 |
@f.setter |
| 2768 |
@dec |
| 2769 |
def f(self, v: int) -> None: pass |
| 2770 |
reveal_type(A().f) # N: Revealed type is "builtins.int" |
| 2771 |
|
| 2772 |
class B: |
| 2773 |
@property |
| 2774 |
@dec |
| 2775 |
def f(self) -> int: pass |
| 2776 |
@dec # E: Only supported top decorators are "@f.setter" and "@f.deleter" |
| 2777 |
@f.setter |
| 2778 |
def f(self, v: int) -> None: pass |
| 2779 |
|
| 2780 |
class C: |
| 2781 |
@dec # E: Decorators on top of @property are not supported |
| 2782 |
@property |
| 2783 |
def f(self) -> int: pass |
| 2784 |
@f.setter |
| 2785 |
@dec |
| 2786 |
def f(self, v: int) -> None: pass |
| 2787 |
[builtins fixtures/property.pyi] |
| 2788 |
|
| 2789 |
[case testInvalidArgCountForProperty] |
| 2790 |
from typing import Callable, TypeVar |
| 2791 |
|
| 2792 |
T = TypeVar("T") |
| 2793 |
def dec(f: Callable[[T], int]) -> Callable[[T, int], int]: ... |
| 2794 |
|
| 2795 |
class A: |
| 2796 |
@property # E: Too many arguments for property |
| 2797 |
def f(self, x) -> int: pass |
| 2798 |
@property # E: Too many arguments for property |
| 2799 |
@dec |
| 2800 |
def e(self) -> int: pass |
| 2801 |
@property |
| 2802 |
def g() -> int: pass # E: Method must have at least one argument. Did you forget the "self" argument? |
| 2803 |
@property |
| 2804 |
def h(self, *args, **kwargs) -> int: pass # OK |
| 2805 |
[builtins fixtures/property.pyi] |
| 2806 |
|
| 2807 |
[case testSubtypingUnionGenericBounds] |
| 2808 |
from typing import Callable, TypeVar, Union, Sequence |
| 2809 |
|
| 2810 |
TI = TypeVar("TI", bound=int) |
| 2811 |
TS = TypeVar("TS", bound=str) |
| 2812 |
|
| 2813 |
f: Callable[[Sequence[TI]], None] |
| 2814 |
g: Callable[[Union[Sequence[TI], Sequence[TS]]], None] |
| 2815 |
f = g |
| 2816 |
|
| 2817 |
[case testOverrideDecoratedProperty] |
| 2818 |
class Base: |
| 2819 |
@property |
| 2820 |
def foo(self) -> int: ... |
| 2821 |
|
| 2822 |
class decorator: |
| 2823 |
def __init__(self, fn): |
| 2824 |
self.fn = fn |
| 2825 |
def __call__(self, decorated_self) -> int: |
| 2826 |
return self.fn(decorated_self) |
| 2827 |
|
| 2828 |
class Child(Base): |
| 2829 |
@property |
| 2830 |
@decorator |
| 2831 |
def foo(self) -> int: |
| 2832 |
return 42 |
| 2833 |
reveal_type(Child().foo) # N: Revealed type is "builtins.int" |
| 2834 |
Child().foo = 1 # E: Property "foo" defined in "Child" is read-only |
| 2835 |
|
| 2836 |
reveal_type(Child().foo) # N: Revealed type is "builtins.int" |
| 2837 |
|
| 2838 |
class BadChild1(Base): |
| 2839 |
@decorator |
| 2840 |
def foo(self) -> int: # E: Signature of "foo" incompatible with supertype "Base" \ |
| 2841 |
# N: Superclass: \ |
| 2842 |
# N: int \ |
| 2843 |
# N: Subclass: \ |
| 2844 |
# N: decorator |
| 2845 |
return 42 |
| 2846 |
|
| 2847 |
class not_a_decorator: |
| 2848 |
def __init__(self, fn): ... |
| 2849 |
|
| 2850 |
class BadChild2(Base): |
| 2851 |
# Override error not shown as accessing 'foo' on BadChild2 returns Any. |
| 2852 |
@property |
| 2853 |
@not_a_decorator |
| 2854 |
def foo(self) -> int: |
| 2855 |
return 42 |
| 2856 |
reveal_type(BadChild2().foo) # E: "not_a_decorator" not callable \ |
| 2857 |
# N: Revealed type is "Any" |
| 2858 |
[builtins fixtures/property.pyi] |
| 2859 |
|
| 2860 |
[case explicitOverride] |
| 2861 |
# flags: --python-version 3.12 |
| 2862 |
from typing import override |
| 2863 |
|
| 2864 |
class A: |
| 2865 |
def f(self, x: int) -> str: pass |
| 2866 |
@override |
| 2867 |
def g(self, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name |
| 2868 |
|
| 2869 |
class B(A): |
| 2870 |
@override |
| 2871 |
def f(self, x: int) -> str: pass |
| 2872 |
@override |
| 2873 |
def g(self, x: int) -> str: pass |
| 2874 |
|
| 2875 |
class C(A): |
| 2876 |
@override |
| 2877 |
def f(self, x: str) -> str: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \ |
| 2878 |
# N: This violates the Liskov substitution principle \ |
| 2879 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 2880 |
def g(self, x: int) -> str: pass |
| 2881 |
|
| 2882 |
class D(A): pass |
| 2883 |
class E(D): pass |
| 2884 |
class F(E): |
| 2885 |
@override |
| 2886 |
def f(self, x: int) -> str: pass |
| 2887 |
[typing fixtures/typing-override.pyi] |
| 2888 |
|
| 2889 |
[case explicitOverrideStaticmethod] |
| 2890 |
# flags: --python-version 3.12 |
| 2891 |
from typing import override |
| 2892 |
|
| 2893 |
class A: |
| 2894 |
@staticmethod |
| 2895 |
def f(x: int) -> str: pass |
| 2896 |
|
| 2897 |
class B(A): |
| 2898 |
@staticmethod |
| 2899 |
@override |
| 2900 |
def f(x: int) -> str: pass |
| 2901 |
@override |
| 2902 |
@staticmethod |
| 2903 |
def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name |
| 2904 |
|
| 2905 |
class C(A): # inverted order of decorators |
| 2906 |
@override |
| 2907 |
@staticmethod |
| 2908 |
def f(x: int) -> str: pass |
| 2909 |
@override |
| 2910 |
@staticmethod |
| 2911 |
def g(x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name |
| 2912 |
|
| 2913 |
class D(A): |
| 2914 |
@staticmethod |
| 2915 |
@override |
| 2916 |
def f(x: str) -> str: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \ |
| 2917 |
# N: This violates the Liskov substitution principle \ |
| 2918 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 2919 |
[typing fixtures/typing-override.pyi] |
| 2920 |
[builtins fixtures/staticmethod.pyi] |
| 2921 |
|
| 2922 |
[case explicitOverrideClassmethod] |
| 2923 |
# flags: --python-version 3.12 |
| 2924 |
from typing import override |
| 2925 |
|
| 2926 |
class A: |
| 2927 |
@classmethod |
| 2928 |
def f(cls, x: int) -> str: pass |
| 2929 |
|
| 2930 |
class B(A): |
| 2931 |
@classmethod |
| 2932 |
@override |
| 2933 |
def f(cls, x: int) -> str: pass |
| 2934 |
@override |
| 2935 |
@classmethod |
| 2936 |
def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name |
| 2937 |
|
| 2938 |
class C(A): # inverted order of decorators |
| 2939 |
@override |
| 2940 |
@classmethod |
| 2941 |
def f(cls, x: int) -> str: pass |
| 2942 |
@override |
| 2943 |
@classmethod |
| 2944 |
def g(cls, x: int) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name |
| 2945 |
|
| 2946 |
class D(A): |
| 2947 |
@classmethod |
| 2948 |
@override |
| 2949 |
def f(cls, x: str) -> str: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \ |
| 2950 |
# N: This violates the Liskov substitution principle \ |
| 2951 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 2952 |
[typing fixtures/typing-override.pyi] |
| 2953 |
[builtins fixtures/classmethod.pyi] |
| 2954 |
|
| 2955 |
[case explicitOverrideProperty] |
| 2956 |
# flags: --python-version 3.12 |
| 2957 |
from typing import override |
| 2958 |
|
| 2959 |
class A: |
| 2960 |
@property |
| 2961 |
def f(self) -> str: pass |
| 2962 |
|
| 2963 |
class B(A): |
| 2964 |
@property |
| 2965 |
@override |
| 2966 |
def f(self) -> str: pass |
| 2967 |
@override |
| 2968 |
@property |
| 2969 |
def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name |
| 2970 |
|
| 2971 |
class C(A): # inverted order of decorators |
| 2972 |
@override |
| 2973 |
@property |
| 2974 |
def f(self) -> str: pass |
| 2975 |
@override |
| 2976 |
@property |
| 2977 |
def g(self) -> str: pass # E: Method "g" is marked as an override, but no base method was found with this name |
| 2978 |
|
| 2979 |
class D(A): |
| 2980 |
@property |
| 2981 |
@override |
| 2982 |
def f(self) -> int: pass # E: Signature of "f" incompatible with supertype "A" \ |
| 2983 |
# N: Superclass: \ |
| 2984 |
# N: str \ |
| 2985 |
# N: Subclass: \ |
| 2986 |
# N: int |
| 2987 |
[typing fixtures/typing-override.pyi] |
| 2988 |
[builtins fixtures/property.pyi] |
| 2989 |
|
| 2990 |
[case explicitOverrideSettableProperty] |
| 2991 |
# flags: --python-version 3.12 |
| 2992 |
from typing import override |
| 2993 |
|
| 2994 |
class A: |
| 2995 |
@property |
| 2996 |
def f(self) -> str: pass |
| 2997 |
|
| 2998 |
@f.setter |
| 2999 |
def f(self, value: str) -> None: pass |
| 3000 |
|
| 3001 |
class B(A): |
| 3002 |
@property # E: Read-only property cannot override read-write property |
| 3003 |
@override |
| 3004 |
def f(self) -> str: pass |
| 3005 |
|
| 3006 |
class C(A): |
| 3007 |
@override |
| 3008 |
@property |
| 3009 |
def f(self) -> str: pass |
| 3010 |
|
| 3011 |
@f.setter |
| 3012 |
def f(self, value: str) -> None: pass |
| 3013 |
|
| 3014 |
class D(A): |
| 3015 |
@override # E: Signature of "f" incompatible with supertype "A" \ |
| 3016 |
# N: Superclass: \ |
| 3017 |
# N: str \ |
| 3018 |
# N: Subclass: \ |
| 3019 |
# N: int |
| 3020 |
@property |
| 3021 |
def f(self) -> int: pass |
| 3022 |
|
| 3023 |
@f.setter |
| 3024 |
def f(self, value: int) -> None: pass |
| 3025 |
[typing fixtures/typing-override.pyi] |
| 3026 |
[builtins fixtures/property.pyi] |
| 3027 |
|
| 3028 |
[case invalidExplicitOverride] |
| 3029 |
# flags: --python-version 3.12 |
| 3030 |
from typing import override |
| 3031 |
|
| 3032 |
@override # E: "override" used with a non-method |
| 3033 |
def f(x: int) -> str: pass |
| 3034 |
|
| 3035 |
@override # this should probably throw an error but the signature from typeshed should ensure this already |
| 3036 |
class A: pass |
| 3037 |
|
| 3038 |
def g() -> None: |
| 3039 |
@override # E: "override" used with a non-method |
| 3040 |
def h(b: bool) -> int: pass |
| 3041 |
[typing fixtures/typing-override.pyi] |
| 3042 |
|
| 3043 |
[case explicitOverrideSpecialMethods] |
| 3044 |
# flags: --python-version 3.12 |
| 3045 |
from typing import override |
| 3046 |
|
| 3047 |
class A: |
| 3048 |
def __init__(self, a: int) -> None: pass |
| 3049 |
|
| 3050 |
class B(A): |
| 3051 |
@override |
| 3052 |
def __init__(self, b: str) -> None: pass |
| 3053 |
|
| 3054 |
class C: |
| 3055 |
@override |
| 3056 |
def __init__(self, a: int) -> None: pass |
| 3057 |
[typing fixtures/typing-override.pyi] |
| 3058 |
|
| 3059 |
[case explicitOverrideFromExtensions] |
| 3060 |
from typing_extensions import override |
| 3061 |
|
| 3062 |
class A: |
| 3063 |
def f(self, x: int) -> str: pass |
| 3064 |
|
| 3065 |
class B(A): |
| 3066 |
@override |
| 3067 |
def f2(self, x: int) -> str: pass # E: Method "f2" is marked as an override, but no base method was found with this name |
| 3068 |
[builtins fixtures/tuple.pyi] |
| 3069 |
|
| 3070 |
[case explicitOverrideOverloads] |
| 3071 |
# flags: --python-version 3.12 |
| 3072 |
from typing import overload, override |
| 3073 |
|
| 3074 |
class A: |
| 3075 |
def f(self, x: int) -> str: pass |
| 3076 |
|
| 3077 |
class B(A): |
| 3078 |
@overload # E: Method "f2" is marked as an override, but no base method was found with this name |
| 3079 |
def f2(self, x: int) -> str: pass |
| 3080 |
@overload |
| 3081 |
def f2(self, x: str) -> str: pass |
| 3082 |
@override |
| 3083 |
def f2(self, x: int | str) -> str: pass |
| 3084 |
[typing fixtures/typing-override.pyi] |
| 3085 |
|
| 3086 |
[case explicitOverrideNotOnOverloadsImplementation] |
| 3087 |
# flags: --python-version 3.12 |
| 3088 |
from typing import overload, override |
| 3089 |
|
| 3090 |
class A: |
| 3091 |
def f(self, x: int) -> str: pass |
| 3092 |
|
| 3093 |
class B(A): |
| 3094 |
@overload # E: Method "f2" is marked as an override, but no base method was found with this name |
| 3095 |
def f2(self, x: int) -> str: pass |
| 3096 |
@override |
| 3097 |
@overload |
| 3098 |
def f2(self, x: str) -> str: pass |
| 3099 |
def f2(self, x: int | str) -> str: pass |
| 3100 |
|
| 3101 |
class C(A): |
| 3102 |
@overload |
| 3103 |
def f(self, y: int) -> str: pass |
| 3104 |
@override |
| 3105 |
@overload |
| 3106 |
def f(self, y: str) -> str: pass |
| 3107 |
def f(self, y: int | str) -> str: pass |
| 3108 |
[typing fixtures/typing-override.pyi] |
| 3109 |
|
| 3110 |
[case explicitOverrideOnMultipleOverloads] |
| 3111 |
# flags: --python-version 3.12 |
| 3112 |
from typing import overload, override |
| 3113 |
|
| 3114 |
class A: |
| 3115 |
def f(self, x: int) -> str: pass |
| 3116 |
|
| 3117 |
class B(A): |
| 3118 |
@override # E: Method "f2" is marked as an override, but no base method was found with this name |
| 3119 |
@overload |
| 3120 |
def f2(self, x: int) -> str: pass |
| 3121 |
@override |
| 3122 |
@overload |
| 3123 |
def f2(self, x: str) -> str: pass |
| 3124 |
def f2(self, x: int | str) -> str: pass |
| 3125 |
|
| 3126 |
class C(A): |
| 3127 |
@overload |
| 3128 |
def f(self, y: int) -> str: pass |
| 3129 |
@override |
| 3130 |
@overload |
| 3131 |
def f(self, y: str) -> str: pass |
| 3132 |
@override |
| 3133 |
def f(self, y: int | str) -> str: pass |
| 3134 |
[typing fixtures/typing-override.pyi] |
| 3135 |
|
| 3136 |
[case explicitOverrideCyclicDependency] |
| 3137 |
# flags: --python-version 3.12 |
| 3138 |
import b |
| 3139 |
[file a.py] |
| 3140 |
from typing import override |
| 3141 |
import b |
| 3142 |
import c |
| 3143 |
|
| 3144 |
class A(b.B): |
| 3145 |
@override # This is fine |
| 3146 |
@c.deco |
| 3147 |
def meth(self) -> int: ... |
| 3148 |
[file b.py] |
| 3149 |
import a |
| 3150 |
import c |
| 3151 |
|
| 3152 |
class B: |
| 3153 |
@c.deco |
| 3154 |
def meth(self) -> int: ... |
| 3155 |
[file c.py] |
| 3156 |
from typing import TypeVar, Tuple, Callable |
| 3157 |
T = TypeVar('T') |
| 3158 |
def deco(f: Callable[..., T]) -> Callable[..., Tuple[T, int]]: ... |
| 3159 |
[builtins fixtures/tuple.pyi] |
| 3160 |
[typing fixtures/typing-override.pyi] |
| 3161 |
|
| 3162 |
[case requireExplicitOverrideMethod] |
| 3163 |
# flags: --enable-error-code explicit-override --python-version 3.12 |
| 3164 |
from typing import override |
| 3165 |
|
| 3166 |
class A: |
| 3167 |
def f(self, x: int) -> str: pass |
| 3168 |
|
| 3169 |
class B(A): |
| 3170 |
@override |
| 3171 |
def f(self, y: int) -> str: pass |
| 3172 |
|
| 3173 |
class C(A): |
| 3174 |
def f(self, y: int) -> str: pass # E: Method "f" is not using @override but is overriding a method in class "__main__.A" |
| 3175 |
|
| 3176 |
class D(B): |
| 3177 |
def f(self, y: int) -> str: pass # E: Method "f" is not using @override but is overriding a method in class "__main__.B" |
| 3178 |
[typing fixtures/typing-override.pyi] |
| 3179 |
|
| 3180 |
[case requireExplicitOverrideSpecialMethod] |
| 3181 |
# flags: --enable-error-code explicit-override --python-version 3.12 |
| 3182 |
from typing import Callable, Self, TypeVar, override, overload |
| 3183 |
|
| 3184 |
T = TypeVar('T') |
| 3185 |
def some_decorator(f: Callable[..., T]) -> Callable[..., T]: ... |
| 3186 |
|
| 3187 |
# Don't require override decorator for __init__ and __new__ |
| 3188 |
# See: https://github.com/python/typing/issues/1376 |
| 3189 |
class A: |
| 3190 |
def __init__(self) -> None: pass |
| 3191 |
def __new__(cls) -> Self: pass |
| 3192 |
|
| 3193 |
class B(A): |
| 3194 |
def __init__(self) -> None: pass |
| 3195 |
def __new__(cls) -> Self: pass |
| 3196 |
|
| 3197 |
class C(A): |
| 3198 |
@some_decorator |
| 3199 |
def __init__(self) -> None: pass |
| 3200 |
|
| 3201 |
@some_decorator |
| 3202 |
def __new__(cls) -> Self: pass |
| 3203 |
|
| 3204 |
class D(A): |
| 3205 |
@overload |
| 3206 |
def __init__(self, x: int) -> None: ... |
| 3207 |
@overload |
| 3208 |
def __init__(self, x: str) -> None: ... |
| 3209 |
def __init__(self, x): pass |
| 3210 |
|
| 3211 |
@overload |
| 3212 |
def __new__(cls, x: int) -> Self: pass |
| 3213 |
@overload |
| 3214 |
def __new__(cls, x: str) -> Self: pass |
| 3215 |
def __new__(cls, x): pass |
| 3216 |
[typing fixtures/typing-override.pyi] |
| 3217 |
|
| 3218 |
[case requireExplicitOverrideProperty] |
| 3219 |
# flags: --enable-error-code explicit-override --python-version 3.12 |
| 3220 |
from typing import override |
| 3221 |
|
| 3222 |
class A: |
| 3223 |
@property |
| 3224 |
def prop(self) -> int: pass |
| 3225 |
|
| 3226 |
class B(A): |
| 3227 |
@override |
| 3228 |
@property |
| 3229 |
def prop(self) -> int: pass |
| 3230 |
|
| 3231 |
class C(A): |
| 3232 |
@property |
| 3233 |
def prop(self) -> int: pass # E: Method "prop" is not using @override but is overriding a method in class "__main__.A" |
| 3234 |
[typing fixtures/typing-override.pyi] |
| 3235 |
[builtins fixtures/property.pyi] |
| 3236 |
|
| 3237 |
[case requireExplicitOverrideOverload] |
| 3238 |
# flags: --enable-error-code explicit-override --python-version 3.12 |
| 3239 |
from typing import overload, override |
| 3240 |
|
| 3241 |
class A: |
| 3242 |
@overload |
| 3243 |
def f(self, x: int) -> str: ... |
| 3244 |
@overload |
| 3245 |
def f(self, x: str) -> str: ... |
| 3246 |
def f(self, x): pass |
| 3247 |
|
| 3248 |
class B(A): |
| 3249 |
@overload |
| 3250 |
def f(self, y: int) -> str: ... |
| 3251 |
@overload |
| 3252 |
def f(self, y: str) -> str: ... |
| 3253 |
@override |
| 3254 |
def f(self, y): pass |
| 3255 |
|
| 3256 |
class C(A): |
| 3257 |
@overload |
| 3258 |
@override |
| 3259 |
def f(self, y: int) -> str: ... |
| 3260 |
@overload |
| 3261 |
def f(self, y: str) -> str: ... |
| 3262 |
def f(self, y): pass |
| 3263 |
|
| 3264 |
class D(A): |
| 3265 |
@overload |
| 3266 |
def f(self, y: int) -> str: ... |
| 3267 |
@overload |
| 3268 |
def f(self, y: str) -> str: ... |
| 3269 |
def f(self, y): pass # E: Method "f" is not using @override but is overriding a method in class "__main__.A" |
| 3270 |
[typing fixtures/typing-override.pyi] |
| 3271 |
|
| 3272 |
[case requireExplicitOverrideMultipleInheritance] |
| 3273 |
# flags: --enable-error-code explicit-override --python-version 3.12 |
| 3274 |
from typing import override |
| 3275 |
|
| 3276 |
class A: |
| 3277 |
def f(self, x: int) -> str: pass |
| 3278 |
class B: |
| 3279 |
def f(self, y: int) -> str: pass |
| 3280 |
|
| 3281 |
class C(A, B): |
| 3282 |
@override |
| 3283 |
def f(self, z: int) -> str: pass |
| 3284 |
|
| 3285 |
class D(A, B): |
| 3286 |
def f(self, z: int) -> str: pass # E: Method "f" is not using @override but is overriding a method in class "__main__.A" |
| 3287 |
[typing fixtures/typing-override.pyi] |
| 3288 |
|
| 3289 |
[case testExplicitOverrideAllowedForPrivate] |
| 3290 |
# flags: --enable-error-code explicit-override --python-version 3.12 |
| 3291 |
|
| 3292 |
class B: |
| 3293 |
def __f(self, y: int) -> str: pass |
| 3294 |
|
| 3295 |
class C(B): |
| 3296 |
def __f(self, y: int) -> str: pass # OK |
| 3297 |
[typing fixtures/typing-override.pyi] |
| 3298 |
|
| 3299 |
[case testOverrideUntypedDef] |
| 3300 |
# flags: --python-version 3.12 |
| 3301 |
from typing import override |
| 3302 |
|
| 3303 |
class Parent: pass |
| 3304 |
|
| 3305 |
class Child(Parent): |
| 3306 |
@override |
| 3307 |
def foo(self, y): pass # E: Method "foo" is marked as an override, but no base method was found with this name |
| 3308 |
|
| 3309 |
[typing fixtures/typing-override.pyi] |
| 3310 |
|
| 3311 |
[case testOverrideOnUnknownBaseClass] |
| 3312 |
# flags: --python-version 3.12 |
| 3313 |
from typing import overload, override |
| 3314 |
|
| 3315 |
from unknown import UnknownParent # type: ignore[import-not-found] |
| 3316 |
|
| 3317 |
class UnknownChild(UnknownParent): |
| 3318 |
@override |
| 3319 |
def foo(self, y): pass # OK |
| 3320 |
@override |
| 3321 |
def bar(self, y: str) -> None: pass # OK |
| 3322 |
|
| 3323 |
@override |
| 3324 |
@overload |
| 3325 |
def baz(self, y: str) -> None: ... |
| 3326 |
@override |
| 3327 |
@overload |
| 3328 |
def baz(self, y: int) -> None: ... |
| 3329 |
def baz(self, y: str | int) -> None: ... |
| 3330 |
[typing fixtures/typing-override.pyi] |
| 3331 |
|
| 3332 |
[case testCallableProperty] |
| 3333 |
from typing import Callable |
| 3334 |
|
| 3335 |
class something_callable: |
| 3336 |
def __call__(self, fn) -> str: ... |
| 3337 |
|
| 3338 |
def decorator(fn: Callable[..., int]) -> something_callable: ... |
| 3339 |
|
| 3340 |
class A: |
| 3341 |
@property |
| 3342 |
@decorator |
| 3343 |
def f(self) -> int: ... |
| 3344 |
|
| 3345 |
reveal_type(A.f) # N: Revealed type is "__main__.something_callable" |
| 3346 |
reveal_type(A().f) # N: Revealed type is "builtins.str" |
| 3347 |
[builtins fixtures/property.pyi] |
| 3348 |
|
| 3349 |
[case testFinalOverrideOnUntypedDef] |
| 3350 |
from typing import final |
| 3351 |
|
| 3352 |
class Base: |
| 3353 |
@final |
| 3354 |
def foo(self): |
| 3355 |
pass |
| 3356 |
|
| 3357 |
class Derived(Base): |
| 3358 |
def foo(self): # E: Cannot override final attribute "foo" (previously declared in base class "Base") |
| 3359 |
pass |
| 3360 |
|
| 3361 |
[case testTypeVarIdClashPolymorphic] |
| 3362 |
from typing import Callable, Generic, TypeVar |
| 3363 |
|
| 3364 |
A = TypeVar("A") |
| 3365 |
B = TypeVar("B") |
| 3366 |
|
| 3367 |
class Gen(Generic[A]): ... |
| 3368 |
|
| 3369 |
def id_(x: A) -> A: ... |
| 3370 |
def f(x: Gen[A], y: A) -> Gen[Gen[A]]: ... |
| 3371 |
def g(x: Gen[A], id_: Callable[[B], B], f: Callable[[A, B], Gen[A]]) -> A: ... |
| 3372 |
|
| 3373 |
def test(x: Gen[Gen[A]]) -> Gen[A]: |
| 3374 |
return g(x, id_, f) # Technically OK |
| 3375 |
|
| 3376 |
x: Gen[Gen[int]] |
| 3377 |
reveal_type(g(x, id_, f)) # N: Revealed type is "__main__.Gen[builtins.int]" |
| 3378 |
|
| 3379 |
def h(x: A, y: A) -> A: ... |
| 3380 |
def gn(id_: Callable[[B], B], step: Callable[[A, B], A]) -> A: ... |
| 3381 |
|
| 3382 |
def fn(x: A) -> A: |
| 3383 |
return gn(id_, h) # Technically OK |
| 3384 |
|
| 3385 |
[case testTypeVarIdsNested] |
| 3386 |
from typing import Callable, TypeVar |
| 3387 |
|
| 3388 |
A = TypeVar("A") |
| 3389 |
B = TypeVar("B") |
| 3390 |
|
| 3391 |
def f(x: Callable[[A], A]) -> Callable[[B], B]: |
| 3392 |
def g(x: B) -> B: ... |
| 3393 |
return g |
| 3394 |
|
| 3395 |
reveal_type(f(f)) # N: Revealed type is "def [B] (B`1) -> B`1" |
| 3396 |
reveal_type(f(f)(f)) # N: Revealed type is "def [A] (x: def (A`-1) -> A`-1) -> def [B] (B`-2) -> B`-2" |
| 3397 |
|
| 3398 |
[case testGenericUnionFunctionJoin] |
| 3399 |
from typing import TypeVar, Union |
| 3400 |
|
| 3401 |
T = TypeVar("T") |
| 3402 |
S = TypeVar("S") |
| 3403 |
|
| 3404 |
def f(x: T, y: S) -> Union[T, S]: ... |
| 3405 |
def g(x: T, y: S) -> Union[T, S]: ... |
| 3406 |
|
| 3407 |
x = [f, g] |
| 3408 |
reveal_type(x) # N: Revealed type is "builtins.list[def [T, S] (x: T`4, y: S`5) -> T`4 | S`5]" |
| 3409 |
[builtins fixtures/list.pyi] |
| 3410 |
|
| 3411 |
[case testTypeVariableClashErrorMessage] |
| 3412 |
from typing import TypeVar |
| 3413 |
|
| 3414 |
T = TypeVar("T") |
| 3415 |
|
| 3416 |
class C: # Note: Generic[T] missing |
| 3417 |
def bad_idea(self, x: T) -> None: |
| 3418 |
self.x = x |
| 3419 |
|
| 3420 |
def nope(self, x: T) -> None: |
| 3421 |
self.x = x # E: Incompatible types in assignment (expression has type "T@nope", variable has type "T@bad_idea") |
| 3422 |
|
| 3423 |
[case testNoCrashOnBadCallablePropertyOverride] |
| 3424 |
from typing import Callable, Union |
| 3425 |
|
| 3426 |
class C: ... |
| 3427 |
class D: ... |
| 3428 |
|
| 3429 |
A = Callable[[C], None] |
| 3430 |
B = Callable[[D], None] |
| 3431 |
|
| 3432 |
class Foo: |
| 3433 |
@property |
| 3434 |
def method(self) -> Callable[[int, Union[A, B]], None]: |
| 3435 |
... |
| 3436 |
|
| 3437 |
class Bar(Foo): |
| 3438 |
@property |
| 3439 |
def method(self) -> Callable[[int, A], None]: # E: Argument 2 of "method" is incompatible with supertype "Foo"; supertype defines the argument type as "Callable[[C], None] | Callable[[D], None]" \ |
| 3440 |
# N: This violates the Liskov substitution principle \ |
| 3441 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 3442 |
... |
| 3443 |
[builtins fixtures/property.pyi] |
| 3444 |
|
| 3445 |
[case testNoCrashOnUnpackOverride] |
| 3446 |
from typing import TypedDict, Unpack |
| 3447 |
|
| 3448 |
class Params(TypedDict): |
| 3449 |
x: int |
| 3450 |
y: str |
| 3451 |
|
| 3452 |
class Other(TypedDict): |
| 3453 |
x: int |
| 3454 |
y: int |
| 3455 |
|
| 3456 |
class B: |
| 3457 |
def meth(self, **kwargs: Unpack[Params]) -> None: |
| 3458 |
... |
| 3459 |
class C(B): |
| 3460 |
def meth(self, **kwargs: Unpack[Other]) -> None: # E: Signature of "meth" incompatible with supertype "B" \ |
| 3461 |
# N: Superclass: \ |
| 3462 |
# N: def meth(*, x: int, y: str) -> None \ |
| 3463 |
# N: Subclass: \ |
| 3464 |
# N: def meth(*, x: int, y: int) -> None |
| 3465 |
... |
| 3466 |
[builtins fixtures/dict.pyi] |
| 3467 |
[typing fixtures/typing-full.pyi] |
| 3468 |
|
| 3469 |
[case testOverrideErrorLocationNamed] |
| 3470 |
class B: |
| 3471 |
def meth( |
| 3472 |
self, *, |
| 3473 |
x: int, |
| 3474 |
y: str, |
| 3475 |
) -> None: |
| 3476 |
... |
| 3477 |
class C(B): |
| 3478 |
def meth( |
| 3479 |
self, *, |
| 3480 |
y: int, # E: Argument 1 of "meth" is incompatible with supertype "B"; supertype defines the argument type as "str" \ |
| 3481 |
# N: This violates the Liskov substitution principle \ |
| 3482 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 3483 |
x: int, |
| 3484 |
) -> None: |
| 3485 |
... |
| 3486 |
[builtins fixtures/tuple.pyi] |
| 3487 |
|
| 3488 |
[case testLambdaAlwaysAllowed] |
| 3489 |
# flags: --disallow-untyped-calls |
| 3490 |
from typing import Callable, Optional |
| 3491 |
|
| 3492 |
def func() -> Optional[str]: ... |
| 3493 |
var: Optional[str] |
| 3494 |
|
| 3495 |
factory: Callable[[], Optional[str]] |
| 3496 |
for factory in ( |
| 3497 |
lambda: var, |
| 3498 |
func, |
| 3499 |
): |
| 3500 |
reveal_type(factory) # N: Revealed type is "def () -> builtins.str | None" |
| 3501 |
var = factory() |
| 3502 |
[builtins fixtures/tuple.pyi] |
| 3503 |
|
| 3504 |
[case testLambdaInDeferredDecoratorNoCrash] |
| 3505 |
def foo(x): |
| 3506 |
pass |
| 3507 |
|
| 3508 |
class Bar: |
| 3509 |
def baz(self, x): |
| 3510 |
pass |
| 3511 |
|
| 3512 |
class Qux(Bar): |
| 3513 |
@foo(lambda x: None) |
| 3514 |
def baz(self, x) -> None: |
| 3515 |
pass |
| 3516 |
[builtins fixtures/tuple.pyi] |
| 3517 |
|
| 3518 |
[case testGeneratorInDeferredDecoratorNoCrash] |
| 3519 |
from typing import Protocol, TypeVar |
| 3520 |
|
| 3521 |
T = TypeVar("T", covariant=True) |
| 3522 |
|
| 3523 |
class SupportsNext(Protocol[T]): |
| 3524 |
def __next__(self) -> T: ... |
| 3525 |
|
| 3526 |
def next(i: SupportsNext[T]) -> T: ... |
| 3527 |
|
| 3528 |
def foo(x): |
| 3529 |
pass |
| 3530 |
|
| 3531 |
class Bar: |
| 3532 |
def baz(self, x): |
| 3533 |
pass |
| 3534 |
|
| 3535 |
class Qux(Bar): |
| 3536 |
@next(f for f in [foo]) |
| 3537 |
def baz(self, x) -> None: |
| 3538 |
pass |
| 3539 |
[builtins fixtures/tuple.pyi] |
| 3540 |
|
| 3541 |
[case testCallableJoinWithDefaults] |
| 3542 |
from typing import Callable, TypeVar |
| 3543 |
|
| 3544 |
T = TypeVar("T") |
| 3545 |
|
| 3546 |
def join(t1: T, t2: T) -> T: ... |
| 3547 |
|
| 3548 |
def f1() -> None: ... |
| 3549 |
def f2(i: int = 0) -> None: ... |
| 3550 |
def f3(i: str = "") -> None: ... |
| 3551 |
|
| 3552 |
reveal_type(join(f1, f2)) # N: Revealed type is "def ()" |
| 3553 |
reveal_type(join(f1, f3)) # N: Revealed type is "def ()" |
| 3554 |
reveal_type(join(f2, f3)) # N: Revealed type is "builtins.function" # TODO: this could be better |
| 3555 |
[builtins fixtures/tuple.pyi] |
| 3556 |
|
| 3557 |
[case testCallableJoinWithDefaultsMultiple] |
| 3558 |
from typing import TypeVar |
| 3559 |
T = TypeVar("T") |
| 3560 |
def join(t1: T, t2: T, t3: T) -> T: ... |
| 3561 |
|
| 3562 |
def f_1(common, a=None): ... |
| 3563 |
def f_any(*_, **__): ... |
| 3564 |
def f_3(common, b=None, x=None): ... |
| 3565 |
|
| 3566 |
fdict = { |
| 3567 |
"f_1": f_1, |
| 3568 |
"f_any": f_any, |
| 3569 |
"f_3": f_3, |
| 3570 |
} |
| 3571 |
reveal_type(fdict) # N: Revealed type is "builtins.dict[builtins.str, def (common: Any, a: Any =) -> Any]" |
| 3572 |
|
| 3573 |
reveal_type(join(f_1, f_any, f_3)) # N: Revealed type is "def (common: Any, a: Any =) -> Any" |
| 3574 |
|
| 3575 |
[builtins fixtures/tuple.pyi] |
| 3576 |
|
| 3577 |
[case testCallableJoinWithType] |
| 3578 |
from __future__ import annotations |
| 3579 |
class Exc: ... |
| 3580 |
class AttributeErr(Exc): |
| 3581 |
def __init__(self, *args: object) -> None: ... |
| 3582 |
class FnfErr(Exc): ... |
| 3583 |
|
| 3584 |
x = [ |
| 3585 |
FnfErr, |
| 3586 |
AttributeErr, |
| 3587 |
] |
| 3588 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.type]" |
| 3589 |
[builtins fixtures/type.pyi] |
| 3590 |
|
| 3591 |
[case testDistinctFormatting] |
| 3592 |
from typing import Awaitable, Callable, ParamSpec |
| 3593 |
|
| 3594 |
P = ParamSpec("P") |
| 3595 |
|
| 3596 |
class A: pass |
| 3597 |
class B(A): pass |
| 3598 |
|
| 3599 |
def decorator(f: Callable[P, None]) -> Callable[[Callable[P, A]], None]: |
| 3600 |
return lambda _: None |
| 3601 |
|
| 3602 |
def key(x: int) -> None: ... |
| 3603 |
def fn_b(b: int) -> B: ... |
| 3604 |
|
| 3605 |
decorator(key)(fn_b) # E: Argument 1 has incompatible type "def fn_b(b: int) -> B"; expected "def (x: int) -> A" |
| 3606 |
|
| 3607 |
def decorator2(f: Callable[P, None]) -> Callable[ |
| 3608 |
[Callable[P, Awaitable[None]]], |
| 3609 |
Callable[P, Awaitable[None]], |
| 3610 |
]: |
| 3611 |
return lambda f: f |
| 3612 |
|
| 3613 |
def key2(x: int) -> None: |
| 3614 |
... |
| 3615 |
|
| 3616 |
@decorator2(key2) # E: Argument 1 has incompatible type "def foo2(y: int) -> Coroutine[Any, Any, None]"; expected "def (x: int) -> Awaitable[None]" |
| 3617 |
async def foo2(y: int) -> None: |
| 3618 |
... |
| 3619 |
|
| 3620 |
class Parent: |
| 3621 |
def method_without(self) -> "Parent": ... |
| 3622 |
def method_with(self, param: str) -> "Parent": ... |
| 3623 |
|
| 3624 |
class Child(Parent): |
| 3625 |
method_without: Callable[[], "Child"] |
| 3626 |
method_with: Callable[[str], "Child"] # E: Incompatible types in assignment (expression has type "Callable[[str], Child]", base class "Parent" defined the type as "def method_with(self, param: str) -> Parent") |
| 3627 |
[builtins fixtures/tuple.pyi] |
| 3628 |
|
| 3629 |
[case testDistinctFormattingUnion] |
| 3630 |
from typing import Callable, Union |
| 3631 |
from mypy_extensions import Arg |
| 3632 |
|
| 3633 |
def f(x: Callable[[Arg(int, 'x')], None]) -> None: pass |
| 3634 |
|
| 3635 |
y: Callable[[Union[int, str]], None] |
| 3636 |
f(y) # E: Argument 1 to "f" has incompatible type "Callable[[int | str], None]"; expected "def (x: int) -> None" |
| 3637 |
[builtins fixtures/tuple.pyi] |
| 3638 |
|
| 3639 |
[case testAbstractOverloadsWithoutImplementationAllowed] |
| 3640 |
from abc import abstractmethod |
| 3641 |
from typing import overload, Union |
| 3642 |
|
| 3643 |
class Foo: |
| 3644 |
@overload |
| 3645 |
@abstractmethod |
| 3646 |
def foo(self, value: int) -> int: |
| 3647 |
... |
| 3648 |
@overload |
| 3649 |
@abstractmethod |
| 3650 |
def foo(self, value: str) -> str: |
| 3651 |
... |
| 3652 |
|
| 3653 |
class Bar(Foo): |
| 3654 |
@overload |
| 3655 |
def foo(self, value: int) -> int: |
| 3656 |
... |
| 3657 |
@overload |
| 3658 |
def foo(self, value: str) -> str: |
| 3659 |
... |
| 3660 |
|
| 3661 |
def foo(self, value: Union[int, str]) -> Union[int, str]: |
| 3662 |
return super().foo(value) # E: Call to abstract method "foo" of "Foo" with trivial body via super() is unsafe |
| 3663 |
|
| 3664 |
[case fullNamesOfImportedBaseClassesDisplayed] |
| 3665 |
from a import A |
| 3666 |
|
| 3667 |
class B(A): |
| 3668 |
def f(self, x: str) -> None: # E: Argument 1 of "f" is incompatible with supertype "a.A"; supertype defines the argument type as "int" \ |
| 3669 |
# N: This violates the Liskov substitution principle \ |
| 3670 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 3671 |
... |
| 3672 |
def g(self, x: str) -> None: # E: Signature of "g" incompatible with supertype "a.A" \ |
| 3673 |
# N: Superclass: \ |
| 3674 |
# N: def g(self) -> None \ |
| 3675 |
# N: Subclass: \ |
| 3676 |
# N: def g(self, x: str) -> None |
| 3677 |
... |
| 3678 |
|
| 3679 |
[file a.py] |
| 3680 |
class A: |
| 3681 |
def f(self, x: int) -> None: |
| 3682 |
... |
| 3683 |
def g(self) -> None: |
| 3684 |
... |
| 3685 |
|
| 3686 |
[case testBoundMethodsAssignedInClassBody] |
| 3687 |
from typing import Callable |
| 3688 |
|
| 3689 |
class A: |
| 3690 |
def f(self, x: int) -> str: |
| 3691 |
pass |
| 3692 |
@classmethod |
| 3693 |
def g(cls, x: int) -> str: |
| 3694 |
pass |
| 3695 |
@staticmethod |
| 3696 |
def h(x: int) -> str: |
| 3697 |
pass |
| 3698 |
attr: Callable[[int], str] |
| 3699 |
|
| 3700 |
class C: |
| 3701 |
x1 = A.f |
| 3702 |
x2 = A.g |
| 3703 |
x3 = A().f |
| 3704 |
x4 = A().g |
| 3705 |
x5 = A.h |
| 3706 |
x6 = A().h |
| 3707 |
x7 = A().attr |
| 3708 |
|
| 3709 |
reveal_type(C.x1) # N: Revealed type is "def (self: __main__.A, x: builtins.int) -> builtins.str" |
| 3710 |
reveal_type(C.x2) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3711 |
reveal_type(C.x3) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3712 |
reveal_type(C.x4) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3713 |
reveal_type(C.x5) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3714 |
reveal_type(C.x6) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3715 |
reveal_type(C.x7) # N: Revealed type is "def (builtins.int) -> builtins.str" |
| 3716 |
|
| 3717 |
reveal_type(C().x1) # E: Invalid self argument "C" to attribute function "x1" with type "Callable[[A, int], str]" \ |
| 3718 |
# N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3719 |
reveal_type(C().x2) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3720 |
reveal_type(C().x3) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3721 |
reveal_type(C().x4) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3722 |
reveal_type(C().x5) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3723 |
reveal_type(C().x6) # N: Revealed type is "def (x: builtins.int) -> builtins.str" |
| 3724 |
reveal_type(C().x7) # E: Invalid self argument "C" to attribute function "x7" with type "Callable[[int], str]" \ |
| 3725 |
# N: Revealed type is "def () -> builtins.str" |
| 3726 |
[builtins fixtures/classmethod.pyi] |
| 3727 |
|
| 3728 |
[case testFunctionRedefinitionDeferred] |
| 3729 |
from typing import Callable, TypeVar |
| 3730 |
|
| 3731 |
def outer() -> None: |
| 3732 |
if bool(): |
| 3733 |
def inner() -> str: ... |
| 3734 |
else: |
| 3735 |
def inner() -> int: ... # E: All conditional function variants must have identical signatures \ |
| 3736 |
# N: Original: \ |
| 3737 |
# N: def inner() -> str \ |
| 3738 |
# N: Redefinition: \ |
| 3739 |
# N: def inner() -> int |
| 3740 |
x = defer() |
| 3741 |
|
| 3742 |
T = TypeVar("T") |
| 3743 |
def deco(fn: Callable[[], T]) -> Callable[[], list[T]]: ... |
| 3744 |
|
| 3745 |
@deco |
| 3746 |
def defer() -> int: ... |
| 3747 |
[builtins fixtures/list.pyi] |
| 3748 |
|
| 3749 |
[case testCheckFunctionErrorContextDuplicateDeferred] |
| 3750 |
# flags: --show-error-context |
| 3751 |
from typing import Callable, TypeVar |
| 3752 |
|
| 3753 |
def a() -> None: |
| 3754 |
def b() -> None: |
| 3755 |
1 + "" |
| 3756 |
x = defer() |
| 3757 |
|
| 3758 |
T = TypeVar("T") |
| 3759 |
def deco(fn: Callable[[], T]) -> Callable[[], list[T]]: ... |
| 3760 |
|
| 3761 |
@deco |
| 3762 |
def defer() -> int: ... |
| 3763 |
[out] |
| 3764 |
main: note: In function "a": |
| 3765 |
main:6: error: Unsupported operand types for + ("int" and "str") |
| 3766 |
|
| 3767 |
[case testNoExtraNoteForUnpacking] |
| 3768 |
from typing import Protocol |
| 3769 |
|
| 3770 |
class P(Protocol): |
| 3771 |
arg: int |
| 3772 |
# Something that list and dict also have |
| 3773 |
def __contains__(self, item: object) -> bool: ... |
| 3774 |
|
| 3775 |
def foo(x: P, y: P) -> None: ... |
| 3776 |
|
| 3777 |
args: list[object] |
| 3778 |
foo(*args) # E: Argument 1 to "foo" has incompatible type "*list[object]"; expected "P" |
| 3779 |
kwargs: dict[str, object] |
| 3780 |
foo(**kwargs) # E: Argument 1 to "foo" has incompatible type "**dict[str, object]"; expected "P" |
| 3781 |
[builtins fixtures/dict.pyi] |
| 3782 |
|
| 3783 |
[case testNoImplicitReturnErrorOnDeferral_no_empty] |
| 3784 |
# flags: --no-warn-no-return |
| 3785 |
def deferred() -> int: |
| 3786 |
c: C |
| 3787 |
x = 1 |
| 3788 |
if int(): |
| 3789 |
x = c.x |
| 3790 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 3791 |
return x |
| 3792 |
|
| 3793 |
class C: |
| 3794 |
def __init__(self) -> None: |
| 3795 |
self.x = defer() |
| 3796 |
|
| 3797 |
def defer() -> int: |
| 3798 |
return 1 |
| 3799 |
|
| 3800 |
[case testNarrowingForDefaultInInnerFunction] |
| 3801 |
from typing import Optional |
| 3802 |
|
| 3803 |
def outer(x: Optional[float]) -> None: |
| 3804 |
if x is not None: |
| 3805 |
def inner(y: float = x) -> None: ... # OK |
| 3806 |
def bad(y: float = x) -> None: ... # E: Incompatible default for parameter "y" (default has type "float | None", parameter has type "float") |
| 3807 |
|
| 3808 |
[case testMissingPositionalArgShiftDetectedMiddle] |
| 3809 |
def f(x: int, y: str, z: bytes, aa: int) -> None: ... |
| 3810 |
|
| 3811 |
f(1, b'x', 1) |
| 3812 |
[builtins fixtures/primitives.pyi] |
| 3813 |
[out] |
| 3814 |
main:3: error: Missing positional argument "y" in call to "f" |
| 3815 |
|
| 3816 |
[case testMissingPositionalArgShiftDetectedFirst] |
| 3817 |
def f(x: int, y: str, z: bytes, last: float) -> None: ... |
| 3818 |
|
| 3819 |
f("hello", b'x', 1.5) |
| 3820 |
[builtins fixtures/primitives.pyi] |
| 3821 |
[out] |
| 3822 |
main:3: error: Missing positional argument "x" in call to "f" |
| 3823 |
|
| 3824 |
[case testMissingPositionalArgShiftDetectedManyArgs] |
| 3825 |
def f(a: int, b: str, c: float, d: list[int], e: tuple[str, ...]) -> None: ... |
| 3826 |
|
| 3827 |
f(1, 1.5, [1, 2, 3], ("a", "b")) |
| 3828 |
[builtins fixtures/list.pyi] |
| 3829 |
[out] |
| 3830 |
main:3: error: Missing positional argument "b" in call to "f" |
| 3831 |
|
| 3832 |
[case testMissingPositionalArgShiftDetectedLast] |
| 3833 |
def f(x: int, y: str, z: bytes) -> None: ... |
| 3834 |
|
| 3835 |
f(1, "hello") |
| 3836 |
[builtins fixtures/primitives.pyi] |
| 3837 |
[out] |
| 3838 |
main:3: error: Missing positional argument "z" in call to "f" |
| 3839 |
|
| 3840 |
[case testMissingPositionalArgNoShiftPattern] |
| 3841 |
def f(x: int, y: str, z: bytes) -> None: ... |
| 3842 |
|
| 3843 |
f("wrong", 123) |
| 3844 |
[builtins fixtures/primitives.pyi] |
| 3845 |
[out] |
| 3846 |
main:3: error: Missing positional argument "z" in call to "f" |
| 3847 |
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 3848 |
main:3: error: Argument 2 to "f" has incompatible type "int"; expected "str" |
| 3849 |
|
| 3850 |
[case testMissingPositionalArgNoShiftPatternLast] |
| 3851 |
def f(x: int, y: str, z: bytes) -> None: ... |
| 3852 |
|
| 3853 |
f(123, "wrong") |
| 3854 |
[builtins fixtures/primitives.pyi] |
| 3855 |
[out] |
| 3856 |
main:3: error: Missing positional argument "z" in call to "f" |
| 3857 |
|
| 3858 |
[case testMissingPositionalArgNoShiftPatternNone] |
| 3859 |
def f(x: int) -> None: ... |
| 3860 |
|
| 3861 |
f() |
| 3862 |
[builtins fixtures/primitives.pyi] |
| 3863 |
[out] |
| 3864 |
main:3: error: Missing positional argument "x" in call to "f" |
| 3865 |
|
| 3866 |
[case testMissingPositionalArgMultipleMissing] |
| 3867 |
def f(a: int, b: str, c: float, d: list[int]) -> None: ... |
| 3868 |
|
| 3869 |
f(1.5, [1, 2, 3]) |
| 3870 |
[builtins fixtures/list.pyi] |
| 3871 |
[out] |
| 3872 |
main:3: error: Missing positional arguments "c", "d" in call to "f" |
| 3873 |
main:3: error: Argument 1 to "f" has incompatible type "float"; expected "int" |
| 3874 |
main:3: error: Argument 2 to "f" has incompatible type "list[int]"; expected "str" |
| 3875 |
|
| 3876 |
[case testMissingPositionalArgWithDefaults] |
| 3877 |
def f(x: int, y: str, z: bytes = b'default') -> None: ... |
| 3878 |
|
| 3879 |
f("hello") |
| 3880 |
[builtins fixtures/primitives.pyi] |
| 3881 |
[out] |
| 3882 |
main:3: error: Missing positional argument "y" in call to "f" |
| 3883 |
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 3884 |
|
| 3885 |
[case testMissingPositionalArgWithStarArgs] |
| 3886 |
def f(x: int, y: str, z: bytes, *args: int) -> None: ... |
| 3887 |
|
| 3888 |
f("hello", b'x') |
| 3889 |
[builtins fixtures/primitives.pyi] |
| 3890 |
[out] |
| 3891 |
main:3: error: Missing positional argument "z" in call to "f" |
| 3892 |
main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 3893 |
main:3: error: Argument 2 to "f" has incompatible type "bytes"; expected "str" |
| 3894 |
|
| 3895 |
[case testMissingPositionalArgNamesHigherN] |
| 3896 |
# See https://github.com/python/mypy/issues/21427 |
| 3897 |
def convert2(first: int, second: str) -> None: ... |
| 3898 |
|
| 3899 |
# Possibly omitted arg, but we still issue two errors because there is only one argument |
| 3900 |
convert2("hello") # E: Missing positional argument "second" in call to "convert2" \ |
| 3901 |
# E: Argument 1 to "convert2" has incompatible type "str"; expected "int" |
| 3902 |
|
| 3903 |
# Other cases |
| 3904 |
convert2() # E: Missing positional arguments "first", "second" in call to "convert2" |
| 3905 |
|
| 3906 |
convert2("hello", 1) # E: Argument 1 to "convert2" has incompatible type "str"; expected "int" \ |
| 3907 |
# E: Argument 2 to "convert2" has incompatible type "int"; expected "str" |
| 3908 |
|
| 3909 |
def convert3(first: int, second: str, third: float) -> None: ... |
| 3910 |
|
| 3911 |
# Possibly omitted arg, but we now only issue one error |
| 3912 |
convert3("hello", 3.15) # E: Missing positional argument "first" in call to "convert3" |
| 3913 |
|
| 3914 |
# Other cases |
| 3915 |
convert3("hello") # E: Missing positional arguments "second", "third" in call to "convert3" \ |
| 3916 |
# E: Argument 1 to "convert3" has incompatible type "str"; expected "int" |
| 3917 |
|
| 3918 |
convert3(3.15, "hello") # E: Missing positional argument "third" in call to "convert3" \ |
| 3919 |
# E: Argument 1 to "convert3" has incompatible type "float"; expected "int" |
| 3920 |
|
| 3921 |
def convert4(first: int, second: str, third: float, fourth: bytes) -> None: ... |
| 3922 |
|
| 3923 |
# Possibly omitted arg, but we now only issue one error |
| 3924 |
convert4("hello", 3.15, b'') # E: Missing positional argument "first" in call to "convert4" |
| 3925 |
|
| 3926 |
# Other cases |
| 3927 |
convert4("hello") # E: Missing positional arguments "second", "third", "fourth" in call to "convert4" \ |
| 3928 |
# E: Argument 1 to "convert4" has incompatible type "str"; expected "int" |
| 3929 |
|
| 3930 |
convert4("hello", 3.15) # E: Missing positional arguments "third", "fourth" in call to "convert4" \ |
| 3931 |
# E: Argument 1 to "convert4" has incompatible type "str"; expected "int" \ |
| 3932 |
# E: Argument 2 to "convert4" has incompatible type "float"; expected "str" |
| 3933 |
|
| 3934 |
convert4(b'', "hello", 3.15) # E: Missing positional argument "fourth" in call to "convert4" \ |
| 3935 |
# E: Argument 1 to "convert4" has incompatible type "bytes"; expected "int" |
| 3936 |
[builtins fixtures/primitives.pyi] |