| 1 |
-- Type checker test cases for conditional checks that result in some |
| 2 |
-- blocks classified as unreachable (they are not type checked or semantically |
| 3 |
-- analyzed). |
| 4 |
-- |
| 5 |
-- For example, we skip blocks that will not be executed on the active |
| 6 |
-- Python version. |
| 7 |
|
| 8 |
[case testConditionalTypeAliasPY3] |
| 9 |
import typing |
| 10 |
def f(): pass |
| 11 |
PY3 = f() |
| 12 |
if PY3: |
| 13 |
t = int |
| 14 |
x = object() + 'x' # E: Unsupported left operand type for + ("object") |
| 15 |
else: |
| 16 |
t = str |
| 17 |
y = 'x' / 1 |
| 18 |
x |
| 19 |
z = 1 # type: t |
| 20 |
|
| 21 |
[case testConditionalAssignmentPY2] |
| 22 |
import typing |
| 23 |
def f(): pass |
| 24 |
PY2 = f() |
| 25 |
if PY2: |
| 26 |
x = object() + 'x' |
| 27 |
else: |
| 28 |
y = 'x' / 1 # E: Unsupported left operand type for / ("str") |
| 29 |
y |
| 30 |
|
| 31 |
[case testConditionalImport] |
| 32 |
import typing |
| 33 |
def f(): pass |
| 34 |
PY2 = f() |
| 35 |
if PY2: |
| 36 |
import fuzzybar |
| 37 |
from barbar import * |
| 38 |
from pawwaw import a, bc |
| 39 |
else: |
| 40 |
import m |
| 41 |
[file m.py] |
| 42 |
import typing |
| 43 |
x = 1 |
| 44 |
if int(): |
| 45 |
x = 'a' |
| 46 |
[out] |
| 47 |
tmp/m.py:4: error: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 48 |
|
| 49 |
[case testNegatedMypyConditional] |
| 50 |
import typing |
| 51 |
MYPY = 0 |
| 52 |
if not MYPY: |
| 53 |
import xyz753 |
| 54 |
else: |
| 55 |
import pow123 # E |
| 56 |
[builtins fixtures/bool.pyi] |
| 57 |
[out] |
| 58 |
main:6: error: Cannot find implementation or library stub for module named "pow123" |
| 59 |
main:6: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 60 |
|
| 61 |
[case testMypyConditional] |
| 62 |
import typing |
| 63 |
MYPY = 0 |
| 64 |
if MYPY: |
| 65 |
None + 1 # E: Unsupported left operand type for + ("None") |
| 66 |
else: |
| 67 |
None + '' |
| 68 |
[builtins fixtures/bool.pyi] |
| 69 |
|
| 70 |
[case testTypeCheckingConditional] |
| 71 |
import typing |
| 72 |
if typing.TYPE_CHECKING: |
| 73 |
import pow123 # E |
| 74 |
else: |
| 75 |
import xyz753 |
| 76 |
[typing fixtures/typing-medium.pyi] |
| 77 |
[out] |
| 78 |
main:3: error: Cannot find implementation or library stub for module named "pow123" |
| 79 |
main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 80 |
|
| 81 |
[case testTypeCheckingConditionalFromImport] |
| 82 |
from typing import TYPE_CHECKING |
| 83 |
if TYPE_CHECKING: |
| 84 |
import pow123 # E |
| 85 |
else: |
| 86 |
import xyz753 |
| 87 |
[typing fixtures/typing-medium.pyi] |
| 88 |
[out] |
| 89 |
main:3: error: Cannot find implementation or library stub for module named "pow123" |
| 90 |
main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 91 |
|
| 92 |
[case testNegatedTypeCheckingConditional] |
| 93 |
import typing |
| 94 |
if not typing.TYPE_CHECKING: |
| 95 |
import pow123 # E |
| 96 |
else: |
| 97 |
import xyz753 |
| 98 |
[builtins fixtures/bool.pyi] |
| 99 |
[typing fixtures/typing-medium.pyi] |
| 100 |
[out] |
| 101 |
main:5: error: Cannot find implementation or library stub for module named "xyz753" |
| 102 |
main:5: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 103 |
|
| 104 |
[case testUndefinedTypeCheckingConditional] |
| 105 |
if not TYPE_CHECKING: # E |
| 106 |
import pow123 |
| 107 |
else: |
| 108 |
import xyz753 |
| 109 |
[builtins fixtures/bool.pyi] |
| 110 |
[out] |
| 111 |
main:1: error: Name "TYPE_CHECKING" is not defined |
| 112 |
main:4: error: Cannot find implementation or library stub for module named "xyz753" |
| 113 |
main:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 114 |
|
| 115 |
[case testConditionalClassDefPY3] |
| 116 |
def f(): pass |
| 117 |
PY3 = f() |
| 118 |
if PY3: |
| 119 |
pass |
| 120 |
else: |
| 121 |
class X(object): |
| 122 |
pass |
| 123 |
|
| 124 |
[case testUnreachabilityAndElifPY3] |
| 125 |
def f(): pass |
| 126 |
PY3 = f() |
| 127 |
if PY3: |
| 128 |
pass |
| 129 |
elif bool(): |
| 130 |
import nonexistent |
| 131 |
1 + '' |
| 132 |
else: |
| 133 |
import bad_name |
| 134 |
1 + '' |
| 135 |
[builtins fixtures/bool.pyi] |
| 136 |
[out] |
| 137 |
|
| 138 |
[case testSysVersionInfo] |
| 139 |
import sys |
| 140 |
if sys.version_info[0] >= 3: |
| 141 |
def foo() -> int: return 0 |
| 142 |
else: |
| 143 |
def foo() -> str: return '' |
| 144 |
reveal_type(foo()) # N: Revealed type is "builtins.int" |
| 145 |
[builtins fixtures/ops.pyi] |
| 146 |
[out] |
| 147 |
|
| 148 |
[case testSysVersionInfoReversedOperandsOrder] |
| 149 |
import sys |
| 150 |
if (3,) <= sys.version_info: |
| 151 |
def foo() -> int: return 0 |
| 152 |
else: |
| 153 |
def foo() -> str: return '' |
| 154 |
reveal_type(foo()) # N: Revealed type is "builtins.int" |
| 155 |
[builtins fixtures/ops.pyi] |
| 156 |
[out] |
| 157 |
|
| 158 |
[case testSysVersionInfoNegated] |
| 159 |
import sys |
| 160 |
if not (sys.version_info[0] < 3): |
| 161 |
def foo() -> int: return 0 |
| 162 |
else: |
| 163 |
def foo() -> str: return '' |
| 164 |
reveal_type(foo()) # N: Revealed type is "builtins.int" |
| 165 |
[builtins fixtures/ops.pyi] |
| 166 |
[out] |
| 167 |
|
| 168 |
[case testSysVersionInfoSliced1] |
| 169 |
import sys |
| 170 |
if sys.version_info[:1] >= (3,): |
| 171 |
def foo() -> int: return 0 |
| 172 |
else: |
| 173 |
def foo() -> str: return '' |
| 174 |
foo() + 0 |
| 175 |
[builtins fixtures/ops.pyi] |
| 176 |
[out] |
| 177 |
|
| 178 |
[case testSysVersionInfoSliced2] |
| 179 |
import sys |
| 180 |
if sys.version_info[:2] >= (3, 0): |
| 181 |
def foo() -> int: return 0 |
| 182 |
else: |
| 183 |
def foo() -> str: return '' |
| 184 |
foo() + 0 |
| 185 |
[builtins fixtures/ops.pyi] |
| 186 |
[out] |
| 187 |
|
| 188 |
[case testSysVersionInfoSliced3] |
| 189 |
import sys |
| 190 |
if sys.version_info[:] >= (3, 0): |
| 191 |
def foo() -> int: return 0 |
| 192 |
else: |
| 193 |
def foo() -> str: return '' |
| 194 |
foo() + 0 |
| 195 |
[builtins fixtures/ops.pyi] |
| 196 |
[out] |
| 197 |
|
| 198 |
[case testSysVersionInfoSliced4] |
| 199 |
import sys |
| 200 |
if sys.version_info[0:2] >= (3, 0): |
| 201 |
def foo() -> int: return 0 |
| 202 |
else: |
| 203 |
def foo() -> str: return '' |
| 204 |
foo() + 0 |
| 205 |
[builtins fixtures/ops.pyi] |
| 206 |
[out] |
| 207 |
|
| 208 |
[case testSysVersionInfoSliced5] |
| 209 |
import sys |
| 210 |
if sys.version_info[0:] >= (3,): |
| 211 |
def foo() -> int: return 0 |
| 212 |
else: |
| 213 |
def foo() -> str: return '' |
| 214 |
foo() + 0 |
| 215 |
[builtins fixtures/ops.pyi] |
| 216 |
[out] |
| 217 |
|
| 218 |
[case testSysVersionInfoSliced6] |
| 219 |
import sys |
| 220 |
if sys.version_info[1:] >= (5,): |
| 221 |
def foo() -> int: return 0 |
| 222 |
else: |
| 223 |
def foo() -> str: return '' |
| 224 |
foo() + 0 |
| 225 |
[builtins fixtures/ops.pyi] |
| 226 |
[out] |
| 227 |
|
| 228 |
[case testSysVersionInfoSliced7] |
| 229 |
import sys |
| 230 |
if sys.version_info >= (3, 5): |
| 231 |
def foo() -> int: return 0 |
| 232 |
else: |
| 233 |
def foo() -> str: return '' |
| 234 |
foo() + 0 |
| 235 |
[builtins fixtures/ops.pyi] |
| 236 |
[out] |
| 237 |
|
| 238 |
[case testSysVersionInfoSliced8] |
| 239 |
# Our pyversion only has (major, minor), |
| 240 |
# so testing for (major, minor, bugfix) is unsupported. |
| 241 |
import sys |
| 242 |
if sys.version_info >= (3, 5, 0): |
| 243 |
def foo() -> int: return 0 |
| 244 |
else: |
| 245 |
def foo() -> str: return '' # E: All conditional function variants must have identical signatures \ |
| 246 |
# N: Original: \ |
| 247 |
# N: def foo() -> int \ |
| 248 |
# N: Redefinition: \ |
| 249 |
# N: def foo() -> str |
| 250 |
[builtins fixtures/ops.pyi] |
| 251 |
[out] |
| 252 |
|
| 253 |
[case testSysVersionInfoSliced9] |
| 254 |
# Our pyversion only has (major, minor), |
| 255 |
# so testing for (minor, bugfix) is unsupported (also it's silly :-). |
| 256 |
import sys |
| 257 |
if sys.version_info[1:] >= (5, 0): |
| 258 |
def foo() -> int: return 0 |
| 259 |
else: |
| 260 |
def foo() -> str: return '' # E: All conditional function variants must have identical signatures \ |
| 261 |
# N: Original: \ |
| 262 |
# N: def foo() -> int \ |
| 263 |
# N: Redefinition: \ |
| 264 |
# N: def foo() -> str |
| 265 |
[builtins fixtures/ops.pyi] |
| 266 |
[out] |
| 267 |
|
| 268 |
[case testSysPlatform1] |
| 269 |
import sys |
| 270 |
if sys.platform == 'fictional': |
| 271 |
def foo() -> int: return 0 |
| 272 |
else: |
| 273 |
def foo() -> str: return '' |
| 274 |
foo() + '' |
| 275 |
[builtins fixtures/ops.pyi] |
| 276 |
[out] |
| 277 |
|
| 278 |
[case testSysPlatform2] |
| 279 |
import sys |
| 280 |
if sys.platform != 'fictional': |
| 281 |
def foo() -> int: return 0 |
| 282 |
else: |
| 283 |
def foo() -> str: return '' |
| 284 |
foo() + 0 |
| 285 |
[builtins fixtures/ops.pyi] |
| 286 |
[out] |
| 287 |
|
| 288 |
[case testSysPlatformNegated] |
| 289 |
import sys |
| 290 |
if not (sys.platform == 'fictional'): |
| 291 |
def foo() -> int: return 0 |
| 292 |
else: |
| 293 |
def foo() -> str: return '' |
| 294 |
foo() + 0 |
| 295 |
[builtins fixtures/ops.pyi] |
| 296 |
[out] |
| 297 |
|
| 298 |
[case testSysVersionInfoClass] |
| 299 |
import sys |
| 300 |
if sys.version_info < (3, 5): |
| 301 |
class C: |
| 302 |
pass |
| 303 |
else: |
| 304 |
class C: |
| 305 |
def foo(self) -> int: return 0 |
| 306 |
C().foo() + 0 |
| 307 |
[builtins fixtures/ops.pyi] |
| 308 |
[out] |
| 309 |
|
| 310 |
[case testSysVersionInfoImport] |
| 311 |
import sys |
| 312 |
if sys.version_info >= (3, 5): |
| 313 |
import collections |
| 314 |
else: |
| 315 |
collections = None |
| 316 |
Pt = collections.namedtuple('Pt', 'x y z') |
| 317 |
[builtins fixtures/ops.pyi] |
| 318 |
[out] |
| 319 |
|
| 320 |
[case testSysVersionInfoVariable] |
| 321 |
import sys |
| 322 |
if sys.version_info >= (3, 5): |
| 323 |
x = '' |
| 324 |
else: |
| 325 |
x = 0 |
| 326 |
x + '' |
| 327 |
[builtins fixtures/ops.pyi] |
| 328 |
[out] |
| 329 |
|
| 330 |
[case testSysVersionInfoInClass] |
| 331 |
import sys |
| 332 |
class C: |
| 333 |
if sys.version_info >= (3, 5): |
| 334 |
def foo(self) -> int: return 0 |
| 335 |
else: |
| 336 |
def foo(self) -> str: return '' |
| 337 |
reveal_type(C().foo()) # N: Revealed type is "builtins.int" |
| 338 |
[builtins fixtures/ops.pyi] |
| 339 |
[out] |
| 340 |
|
| 341 |
[case testSysVersionInfoInFunction] |
| 342 |
import sys |
| 343 |
def foo() -> None: |
| 344 |
if sys.version_info >= (3, 5): |
| 345 |
x = '' |
| 346 |
else: |
| 347 |
x = 0 |
| 348 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 349 |
[builtins fixtures/ops.pyi] |
| 350 |
[out] |
| 351 |
|
| 352 |
[case testSysPlatformInMethod] |
| 353 |
import sys |
| 354 |
class C: |
| 355 |
def foo(self) -> None: |
| 356 |
if sys.platform != 'fictional': |
| 357 |
x = '' |
| 358 |
else: |
| 359 |
x = 0 |
| 360 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 361 |
[builtins fixtures/ops.pyi] |
| 362 |
[out] |
| 363 |
|
| 364 |
[case testSysPlatformInFunctionImport1] |
| 365 |
import sys |
| 366 |
def foo() -> None: |
| 367 |
if sys.platform != 'fictional': |
| 368 |
import a |
| 369 |
else: |
| 370 |
import b as a |
| 371 |
a.x |
| 372 |
[file a.py] |
| 373 |
x = 1 |
| 374 |
[builtins fixtures/ops.pyi] |
| 375 |
[out] |
| 376 |
|
| 377 |
[case testSysPlatformInFunctionImport2] |
| 378 |
import sys |
| 379 |
def foo() -> None: |
| 380 |
if sys.platform == 'fictional': |
| 381 |
import b as a |
| 382 |
else: |
| 383 |
import a |
| 384 |
a.x |
| 385 |
[file a.py] |
| 386 |
x = 1 |
| 387 |
[builtins fixtures/ops.pyi] |
| 388 |
[out] |
| 389 |
|
| 390 |
[case testSysPlatformInFunctionImport3] |
| 391 |
from typing import Callable |
| 392 |
import sys |
| 393 |
|
| 394 |
def idf(x: Callable[[], None]) -> Callable[[], None]: return x |
| 395 |
|
| 396 |
@idf |
| 397 |
def foo() -> None: |
| 398 |
if sys.platform == 'fictional': |
| 399 |
import b as a |
| 400 |
else: |
| 401 |
import a |
| 402 |
a.x |
| 403 |
[file a.py] |
| 404 |
x = 1 |
| 405 |
[builtins fixtures/ops.pyi] |
| 406 |
[out] |
| 407 |
|
| 408 |
|
| 409 |
[case testSysPlatformInMethodImport2] |
| 410 |
|
| 411 |
import sys |
| 412 |
class A: |
| 413 |
def foo(self) -> None: |
| 414 |
if sys.platform == 'fictional': |
| 415 |
import b as a |
| 416 |
else: |
| 417 |
import a |
| 418 |
a.x |
| 419 |
[file a.py] |
| 420 |
x = 1 |
| 421 |
[builtins fixtures/ops.pyi] |
| 422 |
[out] |
| 423 |
|
| 424 |
[case testCustomSysVersionInfo] |
| 425 |
# flags: --python-version 3.11 |
| 426 |
import sys |
| 427 |
if sys.version_info >= (3, 11): |
| 428 |
x = "foo" |
| 429 |
else: |
| 430 |
x = 3 |
| 431 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 432 |
[builtins fixtures/ops.pyi] |
| 433 |
|
| 434 |
[case testCustomSysVersionInfo2] |
| 435 |
# flags: --python-version 3.11 |
| 436 |
import sys |
| 437 |
if sys.version_info >= (3, 12): |
| 438 |
x = "foo" |
| 439 |
else: |
| 440 |
x = 3 |
| 441 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 442 |
[builtins fixtures/ops.pyi] |
| 443 |
|
| 444 |
[case testCustomSysPlatform] |
| 445 |
# flags: --platform linux |
| 446 |
import sys |
| 447 |
if sys.platform == 'linux': |
| 448 |
x = "foo" |
| 449 |
else: |
| 450 |
x = 3 |
| 451 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 452 |
[builtins fixtures/ops.pyi] |
| 453 |
[out] |
| 454 |
|
| 455 |
[case testCustomSysPlatform2_no_native_parse] |
| 456 |
# flags: --platform win32 |
| 457 |
import sys |
| 458 |
if sys.platform == 'linux': |
| 459 |
x = "foo" |
| 460 |
else: |
| 461 |
x = 3 |
| 462 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 463 |
[builtins fixtures/ops.pyi] |
| 464 |
[out] |
| 465 |
|
| 466 |
[case testCustomSysPlatformStartsWith_no_native_parse] |
| 467 |
# flags: --platform win32 |
| 468 |
import sys |
| 469 |
if sys.platform.startswith('win'): |
| 470 |
x = "foo" |
| 471 |
else: |
| 472 |
x = 3 |
| 473 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 474 |
[builtins fixtures/ops.pyi] |
| 475 |
[out] |
| 476 |
|
| 477 |
[case testShortCircuitInExpression] |
| 478 |
import typing |
| 479 |
def make() -> bool: pass |
| 480 |
PY2 = PY3 = make() |
| 481 |
|
| 482 |
a = PY2 and str() |
| 483 |
b = PY3 and str() |
| 484 |
c = PY2 or str() |
| 485 |
d = PY3 or str() |
| 486 |
e = (PY2 or PY3) and str() |
| 487 |
f = (PY3 or PY2) and str() |
| 488 |
g = (PY2 or PY3) or str() |
| 489 |
h = (PY3 or PY2) or str() |
| 490 |
reveal_type(a) # N: Revealed type is "builtins.bool" |
| 491 |
reveal_type(b) # N: Revealed type is "builtins.str" |
| 492 |
reveal_type(c) # N: Revealed type is "builtins.str" |
| 493 |
reveal_type(d) # N: Revealed type is "builtins.bool" |
| 494 |
reveal_type(e) # N: Revealed type is "builtins.str" |
| 495 |
reveal_type(f) # N: Revealed type is "builtins.str" |
| 496 |
reveal_type(g) # N: Revealed type is "builtins.bool" |
| 497 |
reveal_type(h) # N: Revealed type is "builtins.bool" |
| 498 |
[builtins fixtures/ops.pyi] |
| 499 |
[out] |
| 500 |
|
| 501 |
[case testConditionalValuesBinaryOps] |
| 502 |
# flags: --platform linux |
| 503 |
import sys |
| 504 |
|
| 505 |
t_and_t = (sys.platform == 'linux' and sys.platform == 'linux') and str() |
| 506 |
t_or_t = (sys.platform == 'linux' or sys.platform == 'linux') and str() |
| 507 |
t_and_f = (sys.platform == 'linux' and sys.platform == 'windows') and str() |
| 508 |
t_or_f = (sys.platform == 'linux' or sys.platform == 'windows') and str() |
| 509 |
f_and_t = (sys.platform == 'windows' and sys.platform == 'linux') and str() |
| 510 |
f_or_t = (sys.platform == 'windows' or sys.platform == 'linux') and str() |
| 511 |
f_and_f = (sys.platform == 'windows' and sys.platform == 'windows') and str() |
| 512 |
f_or_f = (sys.platform == 'windows' or sys.platform == 'windows') and str() |
| 513 |
reveal_type(t_and_t) # N: Revealed type is "builtins.str" |
| 514 |
reveal_type(t_or_t) # N: Revealed type is "builtins.str" |
| 515 |
reveal_type(f_and_t) # N: Revealed type is "builtins.bool" |
| 516 |
reveal_type(f_or_t) # N: Revealed type is "builtins.str" |
| 517 |
reveal_type(t_and_f) # N: Revealed type is "builtins.bool" |
| 518 |
reveal_type(t_or_f) # N: Revealed type is "builtins.str" |
| 519 |
reveal_type(f_and_f) # N: Revealed type is "builtins.bool" |
| 520 |
reveal_type(f_or_f) # N: Revealed type is "builtins.bool" |
| 521 |
[builtins fixtures/ops.pyi] |
| 522 |
|
| 523 |
[case testConditionalValuesNegation] |
| 524 |
# flags: --platform linux |
| 525 |
import sys |
| 526 |
|
| 527 |
not_t = not sys.platform == 'linux' and str() |
| 528 |
not_f = not sys.platform == 'windows' and str() |
| 529 |
not_and_t = not (sys.platform == 'linux' and sys.platform == 'linux') and str() |
| 530 |
not_and_f = not (sys.platform == 'linux' and sys.platform == 'windows') and str() |
| 531 |
not_or_t = not (sys.platform == 'linux' or sys.platform == 'linux') and str() |
| 532 |
not_or_f = not (sys.platform == 'windows' or sys.platform == 'windows') and str() |
| 533 |
reveal_type(not_t) # N: Revealed type is "builtins.bool" |
| 534 |
reveal_type(not_f) # N: Revealed type is "builtins.str" |
| 535 |
reveal_type(not_and_t) # N: Revealed type is "builtins.bool" |
| 536 |
reveal_type(not_and_f) # N: Revealed type is "builtins.str" |
| 537 |
reveal_type(not_or_t) # N: Revealed type is "builtins.bool" |
| 538 |
reveal_type(not_or_f) # N: Revealed type is "builtins.str" |
| 539 |
[builtins fixtures/ops.pyi] |
| 540 |
|
| 541 |
[case testConditionalValuesUnsupportedOps] |
| 542 |
# flags: --platform linux |
| 543 |
import sys |
| 544 |
|
| 545 |
unary_minus = -(sys.platform == 'linux') and str() |
| 546 |
binary_minus = ((sys.platform == 'linux') - (sys.platform == 'linux')) and str() |
| 547 |
reveal_type(unary_minus) # N: Revealed type is "Literal[0] | builtins.str" |
| 548 |
reveal_type(binary_minus) # N: Revealed type is "Literal[0] | builtins.str" |
| 549 |
[builtins fixtures/ops.pyi] |
| 550 |
|
| 551 |
[case testMypyFalseValuesInBinaryOps_no_empty] |
| 552 |
# flags: --platform linux |
| 553 |
import sys |
| 554 |
from typing import TYPE_CHECKING |
| 555 |
|
| 556 |
MYPY = 0 |
| 557 |
|
| 558 |
if TYPE_CHECKING and sys.platform == 'linux': |
| 559 |
def foo1() -> int: ... |
| 560 |
if sys.platform == 'linux' and TYPE_CHECKING: |
| 561 |
def foo2() -> int: ... |
| 562 |
if MYPY and sys.platform == 'linux': |
| 563 |
def foo3() -> int: ... |
| 564 |
if sys.platform == 'linux' and MYPY: |
| 565 |
def foo4() -> int: ... |
| 566 |
|
| 567 |
if TYPE_CHECKING or sys.platform == 'linux': |
| 568 |
def bar1() -> int: ... # E: Missing return statement |
| 569 |
if sys.platform == 'linux' or TYPE_CHECKING: |
| 570 |
def bar2() -> int: ... # E: Missing return statement |
| 571 |
if MYPY or sys.platform == 'linux': |
| 572 |
def bar3() -> int: ... # E: Missing return statement |
| 573 |
if sys.platform == 'linux' or MYPY: |
| 574 |
def bar4() -> int: ... # E: Missing return statement |
| 575 |
[builtins fixtures/ops.pyi] |
| 576 |
|
| 577 |
[case testShortCircuitAndWithConditionalAssignment] |
| 578 |
# flags: --platform linux |
| 579 |
import sys |
| 580 |
|
| 581 |
def f(): pass |
| 582 |
PY2 = f() |
| 583 |
if PY2 and sys.platform == 'linux': |
| 584 |
x = 'foo' |
| 585 |
else: |
| 586 |
x = 3 |
| 587 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 588 |
if sys.platform == 'linux' and PY2: |
| 589 |
y = 'foo' |
| 590 |
else: |
| 591 |
y = 3 |
| 592 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 593 |
[builtins fixtures/ops.pyi] |
| 594 |
|
| 595 |
[case testShortCircuitOrWithConditionalAssignment] |
| 596 |
# flags: --platform linux |
| 597 |
import sys |
| 598 |
|
| 599 |
def f(): pass |
| 600 |
PY2 = f() |
| 601 |
if PY2 or sys.platform == 'linux': |
| 602 |
x = 'foo' |
| 603 |
else: |
| 604 |
x = 3 |
| 605 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 606 |
if sys.platform == 'linux' or PY2: |
| 607 |
y = 'foo' |
| 608 |
else: |
| 609 |
y = 3 |
| 610 |
reveal_type(y) # N: Revealed type is "builtins.str" |
| 611 |
[builtins fixtures/ops.pyi] |
| 612 |
|
| 613 |
[case testShortCircuitNoEvaluation] |
| 614 |
# flags: --platform linux --always-false COMPILE_TIME_FALSE |
| 615 |
import sys |
| 616 |
|
| 617 |
if sys.platform == 'darwin': |
| 618 |
mac_only = 'junk' |
| 619 |
|
| 620 |
# `mac_only` should not be evaluated |
| 621 |
if sys.platform == 'darwin' and mac_only: |
| 622 |
pass |
| 623 |
if sys.platform == 'linux' or mac_only: |
| 624 |
pass |
| 625 |
|
| 626 |
COMPILE_TIME_FALSE = 'junk' |
| 627 |
|
| 628 |
if COMPILE_TIME_FALSE: |
| 629 |
compile_time_false_only = 'junk' |
| 630 |
|
| 631 |
# `compile_time_false_only` should not be evaluated |
| 632 |
if COMPILE_TIME_FALSE and compile_time_false_only: |
| 633 |
pass |
| 634 |
if not COMPILE_TIME_FALSE or compile_time_false_only: |
| 635 |
pass |
| 636 |
|
| 637 |
MYPY = False |
| 638 |
|
| 639 |
if not MYPY: |
| 640 |
mypy_only = 'junk' |
| 641 |
|
| 642 |
# `mypy_only` should not be evaluated |
| 643 |
if not MYPY and mypy_only: |
| 644 |
pass |
| 645 |
if MYPY or mypy_only: |
| 646 |
pass |
| 647 |
[builtins fixtures/ops.pyi] |
| 648 |
|
| 649 |
[case testSemanticAnalysisFalseButTypeNarrowingTrue] |
| 650 |
# flags: --always-false COMPILE_TIME_FALSE |
| 651 |
from typing import Literal |
| 652 |
|
| 653 |
indeterminate: str |
| 654 |
COMPILE_TIME_FALSE: Literal[True] # type-narrowing: mapped in 'if' only |
| 655 |
a = COMPILE_TIME_FALSE or indeterminate |
| 656 |
reveal_type(a) # N: Revealed type is "builtins.str" |
| 657 |
b = indeterminate or COMPILE_TIME_FALSE |
| 658 |
reveal_type(b) # N: Revealed type is "builtins.str | Literal[True]" |
| 659 |
[typing fixtures/typing-medium.pyi] |
| 660 |
|
| 661 |
[case testSemanticAnalysisTrueButTypeNarrowingFalse] |
| 662 |
# flags: --always-true COMPILE_TIME_TRUE |
| 663 |
from typing import Literal |
| 664 |
|
| 665 |
indeterminate: str |
| 666 |
COMPILE_TIME_TRUE: Literal[False] # type narrowed to `else` only |
| 667 |
a = COMPILE_TIME_TRUE or indeterminate |
| 668 |
reveal_type(a) # N: Revealed type is "Literal[False]" |
| 669 |
b = indeterminate or COMPILE_TIME_TRUE |
| 670 |
reveal_type(b) # N: Revealed type is "builtins.str | Literal[False]" |
| 671 |
|
| 672 |
[typing fixtures/typing-medium.pyi] |
| 673 |
[case testConditionalAssertWithoutElse] |
| 674 |
import typing |
| 675 |
|
| 676 |
class A: pass |
| 677 |
class B(A): pass |
| 678 |
|
| 679 |
x = A() |
| 680 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 681 |
|
| 682 |
if typing.TYPE_CHECKING: |
| 683 |
assert isinstance(x, B) |
| 684 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 685 |
|
| 686 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 687 |
|
| 688 |
[builtins fixtures/isinstancelist.pyi] |
| 689 |
[typing fixtures/typing-medium.pyi] |
| 690 |
|
| 691 |
[case testUnreachableWhenSuperclassIsAny] |
| 692 |
from typing import Any |
| 693 |
|
| 694 |
# This can happen if we're importing a class from a missing module |
| 695 |
Parent: Any |
| 696 |
class Child(Parent): |
| 697 |
def foo(self) -> int: |
| 698 |
reveal_type(self) # N: Revealed type is "__main__.Child" |
| 699 |
if self is None: |
| 700 |
reveal_type(self) |
| 701 |
return None |
| 702 |
reveal_type(self) # N: Revealed type is "__main__.Child" |
| 703 |
return 3 |
| 704 |
|
| 705 |
def bar(self) -> int: |
| 706 |
if 1: |
| 707 |
self = super(Child, self).something() |
| 708 |
reveal_type(self) # N: Revealed type is "__main__.Child" |
| 709 |
if self is None: |
| 710 |
reveal_type(self) |
| 711 |
return None |
| 712 |
reveal_type(self) # N: Revealed type is "__main__.Child" |
| 713 |
return 3 |
| 714 |
[builtins fixtures/isinstance.pyi] |
| 715 |
|
| 716 |
[case testUnreachableWhenSuperclassIsAnyNoStrictOptional] |
| 717 |
# flags: --no-strict-optional |
| 718 |
from typing import Any |
| 719 |
|
| 720 |
Parent: Any |
| 721 |
class Child(Parent): |
| 722 |
def foo(self) -> int: |
| 723 |
reveal_type(self) # N: Revealed type is "__main__.Child" |
| 724 |
if self is None: |
| 725 |
reveal_type(self) # N: Revealed type is "None" |
| 726 |
return None |
| 727 |
reveal_type(self) # N: Revealed type is "__main__.Child" |
| 728 |
return 3 |
| 729 |
[builtins fixtures/isinstance.pyi] |
| 730 |
|
| 731 |
[case testUnreachableAfterToplevelAssert] |
| 732 |
import sys |
| 733 |
reveal_type(0) # N: Revealed type is "Literal[0]?" |
| 734 |
assert sys.platform == 'lol' |
| 735 |
reveal_type('') # No error here :-) |
| 736 |
[builtins fixtures/ops.pyi] |
| 737 |
|
| 738 |
[case testUnreachableAfterToplevelAssert2] |
| 739 |
import sys |
| 740 |
reveal_type(0) # N: Revealed type is "Literal[0]?" |
| 741 |
assert sys.version_info[0] == 1 |
| 742 |
reveal_type('') # No error here :-) |
| 743 |
[builtins fixtures/ops.pyi] |
| 744 |
|
| 745 |
[case testUnreachableAfterToplevelAssert3] |
| 746 |
reveal_type(0) # N: Revealed type is "Literal[0]?" |
| 747 |
MYPY = False |
| 748 |
assert not MYPY |
| 749 |
reveal_type('') # No error here :-) |
| 750 |
[builtins fixtures/ops.pyi] |
| 751 |
|
| 752 |
[case testUnreachableAfterToplevelAssert4] |
| 753 |
# flags: --always-false NOPE |
| 754 |
reveal_type(0) # N: Revealed type is "Literal[0]?" |
| 755 |
NOPE = False |
| 756 |
assert NOPE |
| 757 |
reveal_type('') # No error here :-) |
| 758 |
[builtins fixtures/ops.pyi] |
| 759 |
|
| 760 |
[case testUnreachableAfterToplevelAssertImport] |
| 761 |
import foo |
| 762 |
foo.bar() # E: "object" has no attribute "bar" |
| 763 |
[file foo.py] |
| 764 |
import sys |
| 765 |
assert sys.platform == 'lol' |
| 766 |
def bar() -> None: pass |
| 767 |
[builtins fixtures/ops.pyi] |
| 768 |
|
| 769 |
[case testUnreachableAfterToplevelAssertImport2] |
| 770 |
# flags: --platform lol |
| 771 |
import foo |
| 772 |
foo.bar() # No error :-) |
| 773 |
[file foo.py] |
| 774 |
import sys |
| 775 |
assert sys.platform == 'lol' |
| 776 |
def bar() -> None: pass |
| 777 |
[builtins fixtures/ops.pyi] |
| 778 |
|
| 779 |
[case testUnreachableAfterToplevelAssertImportThirdParty] |
| 780 |
# flags: --platform unknown |
| 781 |
import sys |
| 782 |
assert sys.platform == 'linux' |
| 783 |
import does_not_exist |
| 784 |
[builtins fixtures/ops.pyi] |
| 785 |
|
| 786 |
[case testUnreachableAfterToplevelAssertImportThirdParty2] |
| 787 |
# flags: --platform unknown |
| 788 |
import sys |
| 789 |
import bad; assert sys.platform == 'linux'; import does_not_exist |
| 790 |
[builtins fixtures/ops.pyi] |
| 791 |
[out] |
| 792 |
main:3: error: Cannot find implementation or library stub for module named "bad" |
| 793 |
main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 794 |
|
| 795 |
[case testUnreachableAfterToplevelAssertNotInsideIf] |
| 796 |
import sys |
| 797 |
if sys.version_info[0] >= 2: |
| 798 |
assert sys.platform == 'lol' |
| 799 |
reveal_type('') # N: Revealed type is "Literal['']?" |
| 800 |
reveal_type('') # N: Revealed type is "Literal['']?" |
| 801 |
[builtins fixtures/ops.pyi] |
| 802 |
|
| 803 |
[case testUnreachableFlagWithBadControlFlow1] |
| 804 |
# flags: --warn-unreachable |
| 805 |
a: int |
| 806 |
if isinstance(a, int): |
| 807 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 808 |
else: |
| 809 |
reveal_type(a) # E: Statement is unreachable |
| 810 |
[builtins fixtures/isinstancelist.pyi] |
| 811 |
|
| 812 |
[case testUnreachableFlagWithBadControlFlow2] |
| 813 |
# flags: --warn-unreachable |
| 814 |
b: int |
| 815 |
while isinstance(b, int): |
| 816 |
reveal_type(b) # N: Revealed type is "builtins.int" |
| 817 |
else: |
| 818 |
reveal_type(b) # E: Statement is unreachable |
| 819 |
[builtins fixtures/isinstancelist.pyi] |
| 820 |
|
| 821 |
[case testUnreachableFlagWithBadControlFlow3] |
| 822 |
# flags: --warn-unreachable |
| 823 |
def foo(c: int) -> None: |
| 824 |
reveal_type(c) # N: Revealed type is "builtins.int" |
| 825 |
assert not isinstance(c, int) |
| 826 |
reveal_type(c) # E: Statement is unreachable |
| 827 |
[builtins fixtures/isinstancelist.pyi] |
| 828 |
|
| 829 |
[case testUnreachableFlagWithBadControlFlow4] |
| 830 |
# flags: --warn-unreachable |
| 831 |
d: int |
| 832 |
if False: |
| 833 |
reveal_type(d) # E: Statement is unreachable |
| 834 |
[builtins fixtures/isinstancelist.pyi] |
| 835 |
|
| 836 |
[case testUnreachableFlagWithBadControlFlow5] |
| 837 |
# flags: --warn-unreachable |
| 838 |
e: int |
| 839 |
if True: |
| 840 |
reveal_type(e) # N: Revealed type is "builtins.int" |
| 841 |
else: |
| 842 |
reveal_type(e) # E: Statement is unreachable |
| 843 |
[builtins fixtures/isinstancelist.pyi] |
| 844 |
|
| 845 |
[case testUnreachableFlagStatementAfterReturn] |
| 846 |
# flags: --warn-unreachable |
| 847 |
def foo(x: int) -> None: |
| 848 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 849 |
return |
| 850 |
reveal_type(x) # E: Statement is unreachable |
| 851 |
|
| 852 |
[case testUnreachableFlagTryBlocks] |
| 853 |
# flags: --warn-unreachable |
| 854 |
|
| 855 |
def foo(x: int) -> int: |
| 856 |
try: |
| 857 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 858 |
return x |
| 859 |
reveal_type(x) # E: Statement is unreachable |
| 860 |
finally: |
| 861 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 862 |
if True: |
| 863 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 864 |
else: |
| 865 |
reveal_type(x) # E: Statement is unreachable |
| 866 |
|
| 867 |
def bar(x: int) -> int: |
| 868 |
try: |
| 869 |
if True: |
| 870 |
raise Exception() |
| 871 |
reveal_type(x) # E: Statement is unreachable |
| 872 |
except: |
| 873 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 874 |
return x |
| 875 |
else: |
| 876 |
reveal_type(x) # E: Statement is unreachable |
| 877 |
|
| 878 |
def baz(x: int) -> int: |
| 879 |
try: |
| 880 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 881 |
except: |
| 882 |
# Mypy assumes all lines could throw an exception |
| 883 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 884 |
return x |
| 885 |
else: |
| 886 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 887 |
return x |
| 888 |
[builtins fixtures/exception.pyi] |
| 889 |
|
| 890 |
[case testUnreachableFlagIgnoresSemanticAnalysisUnreachable_no_native_parse] |
| 891 |
# flags: --warn-unreachable --python-version 3.10 --platform win32 --always-false FOOBAR |
| 892 |
import sys |
| 893 |
from typing import TYPE_CHECKING |
| 894 |
|
| 895 |
def f1(x: int) -> None: |
| 896 |
if TYPE_CHECKING: |
| 897 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 898 |
else: |
| 899 |
reveal_type(x) |
| 900 |
|
| 901 |
def f2(x: int) -> None: |
| 902 |
if not TYPE_CHECKING: |
| 903 |
reveal_type(x) |
| 904 |
else: |
| 905 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 906 |
|
| 907 |
def f3(x: int) -> None: |
| 908 |
if sys.platform == 'darwin': |
| 909 |
reveal_type(x) |
| 910 |
else: |
| 911 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 912 |
|
| 913 |
def f4(x: int) -> None: |
| 914 |
if sys.platform == 'win32': |
| 915 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 916 |
else: |
| 917 |
reveal_type(x) |
| 918 |
|
| 919 |
def f5(x: int) -> None: |
| 920 |
if sys.version_info == (2, 7): |
| 921 |
reveal_type(x) |
| 922 |
else: |
| 923 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 924 |
|
| 925 |
def f6(x: int) -> None: |
| 926 |
if sys.version_info >= (3, 10): |
| 927 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 928 |
else: |
| 929 |
reveal_type(x) |
| 930 |
|
| 931 |
if sys.version_info >= (3, 11): |
| 932 |
reveal_type(x) |
| 933 |
else: |
| 934 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 935 |
|
| 936 |
def f7(x: int) -> None: |
| 937 |
FOOBAR = "" |
| 938 |
if FOOBAR: |
| 939 |
reveal_type(x) |
| 940 |
else: |
| 941 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 942 |
[builtins fixtures/ops.pyi] |
| 943 |
[typing fixtures/typing-medium.pyi] |
| 944 |
|
| 945 |
[case testUnreachableFlagIgnoresSemanticAnalysisExprUnreachable] |
| 946 |
# flags: --warn-unreachable --always-false FOOBAR |
| 947 |
import sys |
| 948 |
from typing import TYPE_CHECKING |
| 949 |
|
| 950 |
FOOBAR = "" |
| 951 |
|
| 952 |
def foo() -> bool: ... |
| 953 |
|
| 954 |
lst = [1, 2, 3] |
| 955 |
|
| 956 |
a = FOOBAR and foo() |
| 957 |
b = (not FOOBAR) or foo() |
| 958 |
c = 1 if FOOBAR else 2 |
| 959 |
d = [x for x in lst if FOOBAR] |
| 960 |
[builtins fixtures/list.pyi] |
| 961 |
[typing fixtures/typing-medium.pyi] |
| 962 |
|
| 963 |
[case testUnreachableFlagOkWithDeadStatements] |
| 964 |
# flags: --warn-unreachable |
| 965 |
from typing import NoReturn |
| 966 |
def assert_never(x: NoReturn) -> NoReturn: |
| 967 |
assert False |
| 968 |
|
| 969 |
def nonthrowing_assert_never(x: NoReturn) -> None: ... |
| 970 |
|
| 971 |
def expect_str(x: str) -> str: pass |
| 972 |
|
| 973 |
x: int |
| 974 |
if False: |
| 975 |
assert False |
| 976 |
reveal_type(x) # E: Statement is unreachable |
| 977 |
|
| 978 |
if False: |
| 979 |
raise Exception() |
| 980 |
reveal_type(x) # E: Statement is unreachable |
| 981 |
|
| 982 |
if False: |
| 983 |
assert_never(x) |
| 984 |
reveal_type(x) # E: Statement is unreachable |
| 985 |
|
| 986 |
if False: |
| 987 |
nonthrowing_assert_never(x) # E: Statement is unreachable |
| 988 |
reveal_type(x) |
| 989 |
|
| 990 |
if False: |
| 991 |
# Ignore obvious type errors |
| 992 |
assert_never(expect_str(x)) |
| 993 |
reveal_type(x) # E: Statement is unreachable |
| 994 |
[builtins fixtures/exception.pyi] |
| 995 |
|
| 996 |
[case testNeverVariants] |
| 997 |
from typing import Never |
| 998 |
from typing_extensions import Never as TENever |
| 999 |
from typing import NoReturn |
| 1000 |
from typing_extensions import NoReturn as TENoReturn |
| 1001 |
from mypy_extensions import NoReturn as MENoReturn |
| 1002 |
|
| 1003 |
bottom1: Never |
| 1004 |
reveal_type(bottom1) # N: Revealed type is "Never" |
| 1005 |
bottom2: TENever |
| 1006 |
reveal_type(bottom2) # N: Revealed type is "Never" |
| 1007 |
bottom3: NoReturn |
| 1008 |
reveal_type(bottom3) # N: Revealed type is "Never" |
| 1009 |
bottom4: TENoReturn |
| 1010 |
reveal_type(bottom4) # N: Revealed type is "Never" |
| 1011 |
bottom5: MENoReturn |
| 1012 |
reveal_type(bottom5) # N: Revealed type is "Never" |
| 1013 |
|
| 1014 |
[builtins fixtures/tuple.pyi] |
| 1015 |
|
| 1016 |
[case testUnreachableFlagExpressions] |
| 1017 |
# flags: --warn-unreachable |
| 1018 |
def foo() -> bool: ... |
| 1019 |
|
| 1020 |
lst = [1, 2, 3, 4] |
| 1021 |
|
| 1022 |
a = True or foo() # E: Right operand of "or" is never evaluated |
| 1023 |
b = 42 or False # E: Right operand of "or" is never evaluated |
| 1024 |
d = False and foo() # E: Right operand of "and" is never evaluated |
| 1025 |
e = True or (True or (True or foo())) # E: Right operand of "or" is never evaluated |
| 1026 |
f = (True or foo()) or (True or foo()) # E: Right operand of "or" is never evaluated |
| 1027 |
|
| 1028 |
k = [x for x in lst if isinstance(x, int) or foo()] # E: Right operand of "or" is never evaluated |
| 1029 |
[builtins fixtures/isinstancelist.pyi] |
| 1030 |
|
| 1031 |
[case testUnreachableFlagMiscTestCaseMissingMethod] |
| 1032 |
# flags: --warn-unreachable |
| 1033 |
|
| 1034 |
class Case1: |
| 1035 |
def test1(self) -> bool: |
| 1036 |
return False and self.missing() # E: Right operand of "and" is never evaluated |
| 1037 |
|
| 1038 |
def test2(self) -> bool: |
| 1039 |
return not self.property_decorator_missing and self.missing() # E: Function "property_decorator_missing" could always be true in boolean context \ |
| 1040 |
# E: Right operand of "and" is never evaluated |
| 1041 |
|
| 1042 |
def property_decorator_missing(self) -> bool: |
| 1043 |
return True |
| 1044 |
[builtins fixtures/bool.pyi] |
| 1045 |
|
| 1046 |
[case testUnreachableFlagWithGenerics] |
| 1047 |
# flags: --warn-unreachable |
| 1048 |
from typing import TypeVar, Generic |
| 1049 |
|
| 1050 |
T1 = TypeVar('T1', bound=int) |
| 1051 |
T2 = TypeVar('T2', int, str) |
| 1052 |
T3 = TypeVar('T3', None, str) |
| 1053 |
|
| 1054 |
def test1(x: T1) -> T1: |
| 1055 |
if isinstance(x, int): |
| 1056 |
reveal_type(x) # N: Revealed type is "T1`-1" |
| 1057 |
else: |
| 1058 |
reveal_type(x) # E: Statement is unreachable |
| 1059 |
return x |
| 1060 |
|
| 1061 |
def test2(x: T2) -> T2: |
| 1062 |
if isinstance(x, int): |
| 1063 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 1064 |
else: |
| 1065 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1066 |
|
| 1067 |
if False: |
| 1068 |
# This is unreachable, but we don't report an error, unfortunately. |
| 1069 |
# The presence of the TypeVar with values unfortunately currently shuts |
| 1070 |
# down type-checking for this entire function. |
| 1071 |
# TODO: Find a way of removing this limitation |
| 1072 |
reveal_type(x) |
| 1073 |
|
| 1074 |
return x |
| 1075 |
|
| 1076 |
class Test3(Generic[T2]): |
| 1077 |
x: T2 |
| 1078 |
|
| 1079 |
def func(self) -> None: |
| 1080 |
if isinstance(self.x, int): |
| 1081 |
reveal_type(self.x) # N: Revealed type is "builtins.int" |
| 1082 |
else: |
| 1083 |
reveal_type(self.x) # N: Revealed type is "builtins.str" |
| 1084 |
|
| 1085 |
if False: |
| 1086 |
# Same issue as above |
| 1087 |
reveal_type(self.x) |
| 1088 |
|
| 1089 |
|
| 1090 |
class Test4(Generic[T3]): |
| 1091 |
def __init__(self, x: T3): |
| 1092 |
# https://github.com/python/mypy/issues/9456 |
| 1093 |
# On TypeVars with value restrictions, we currently have no way |
| 1094 |
# of checking a statement for all the type expansions. |
| 1095 |
# Thus unreachable warnings are disabled |
| 1096 |
if x and False: |
| 1097 |
pass |
| 1098 |
# This test should fail after this limitation is removed. |
| 1099 |
if False and x: |
| 1100 |
pass |
| 1101 |
|
| 1102 |
[builtins fixtures/isinstancelist.pyi] |
| 1103 |
|
| 1104 |
[case testUnreachableBlockStaysUnreachableWithTypeVarConstraints] |
| 1105 |
# flags: --always-false COMPILE_TIME_FALSE |
| 1106 |
from typing import TypeVar |
| 1107 |
COMPILE_TIME_FALSE = False |
| 1108 |
T = TypeVar("T", int, str) |
| 1109 |
def foo(x: T) -> T: |
| 1110 |
if COMPILE_TIME_FALSE: |
| 1111 |
return "bad" |
| 1112 |
return x |
| 1113 |
|
| 1114 |
[case testUnreachableFlagContextManagersNoSuppress] |
| 1115 |
# flags: --warn-unreachable |
| 1116 |
from contextlib import contextmanager |
| 1117 |
from typing import Literal, Optional, Iterator, Any |
| 1118 |
class DoesNotSuppress1: |
| 1119 |
def __enter__(self) -> int: ... |
| 1120 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> Optional[bool]: ... |
| 1121 |
|
| 1122 |
class DoesNotSuppress2: |
| 1123 |
def __enter__(self) -> int: ... |
| 1124 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> Literal[False]: ... |
| 1125 |
|
| 1126 |
class DoesNotSuppress3: |
| 1127 |
def __enter__(self) -> int: ... |
| 1128 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> Any: ... |
| 1129 |
|
| 1130 |
class DoesNotSuppress4: |
| 1131 |
def __enter__(self) -> int: ... |
| 1132 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> None: ... |
| 1133 |
|
| 1134 |
@contextmanager |
| 1135 |
def simple() -> Iterator[int]: |
| 1136 |
yield 3 |
| 1137 |
|
| 1138 |
def cond() -> bool: ... |
| 1139 |
|
| 1140 |
def noop() -> None: ... |
| 1141 |
|
| 1142 |
def f_no_suppress_1a() -> int: |
| 1143 |
with DoesNotSuppress1(): |
| 1144 |
return 3 |
| 1145 |
noop() # E: Statement is unreachable |
| 1146 |
|
| 1147 |
def f_no_suppress_1b() -> int: |
| 1148 |
with DoesNotSuppress1(): |
| 1149 |
if cond(): |
| 1150 |
return 3 |
| 1151 |
else: |
| 1152 |
return 3 |
| 1153 |
noop() # E: Statement is unreachable |
| 1154 |
|
| 1155 |
def f_no_suppress_2() -> int: |
| 1156 |
with DoesNotSuppress2(): |
| 1157 |
return 3 |
| 1158 |
noop() # E: Statement is unreachable |
| 1159 |
|
| 1160 |
def f_no_suppress_3() -> int: |
| 1161 |
with DoesNotSuppress3(): |
| 1162 |
return 3 |
| 1163 |
noop() # E: Statement is unreachable |
| 1164 |
|
| 1165 |
def f_no_suppress_4() -> int: |
| 1166 |
with DoesNotSuppress4(): |
| 1167 |
return 3 |
| 1168 |
noop() # E: Statement is unreachable |
| 1169 |
|
| 1170 |
def f_no_suppress_5() -> int: |
| 1171 |
with simple(): |
| 1172 |
return 3 |
| 1173 |
noop() # E: Statement is unreachable |
| 1174 |
|
| 1175 |
[typing fixtures/typing-medium.pyi] |
| 1176 |
[builtins fixtures/tuple.pyi] |
| 1177 |
|
| 1178 |
[case testUnreachableFlagContextManagersSuppressed] |
| 1179 |
# flags: --warn-unreachable |
| 1180 |
from contextlib import contextmanager |
| 1181 |
from typing import Optional, Iterator, Literal, Any |
| 1182 |
|
| 1183 |
class DoesNotSuppress: |
| 1184 |
def __enter__(self) -> int: ... |
| 1185 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> Optional[bool]: ... |
| 1186 |
|
| 1187 |
class Suppresses1: |
| 1188 |
def __enter__(self) -> int: ... |
| 1189 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> bool: ... |
| 1190 |
|
| 1191 |
class Suppresses2: |
| 1192 |
def __enter__(self) -> int: ... |
| 1193 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> Literal[True]: ... |
| 1194 |
|
| 1195 |
def cond() -> bool: ... |
| 1196 |
|
| 1197 |
def noop() -> None: ... |
| 1198 |
|
| 1199 |
def f_suppress_1a() -> int: # E: Missing return statement |
| 1200 |
with Suppresses1(): |
| 1201 |
return 3 |
| 1202 |
noop() |
| 1203 |
|
| 1204 |
def f_suppress_1b() -> int: # E: Missing return statement |
| 1205 |
with Suppresses1(): |
| 1206 |
if cond(): |
| 1207 |
return 3 |
| 1208 |
else: |
| 1209 |
return 3 |
| 1210 |
noop() |
| 1211 |
|
| 1212 |
def f_suppress_2() -> int: # E: Missing return statement |
| 1213 |
with Suppresses2(): |
| 1214 |
return 3 |
| 1215 |
noop() |
| 1216 |
|
| 1217 |
def f_mix() -> int: # E: Missing return statement |
| 1218 |
with DoesNotSuppress(), Suppresses1(), DoesNotSuppress(): |
| 1219 |
return 3 |
| 1220 |
noop() |
| 1221 |
[typing fixtures/typing-medium.pyi] |
| 1222 |
[builtins fixtures/tuple.pyi] |
| 1223 |
|
| 1224 |
[case testUnreachableFlagContextManagersSuppressedNoStrictOptional] |
| 1225 |
# flags: --warn-unreachable --no-strict-optional |
| 1226 |
from contextlib import contextmanager |
| 1227 |
from typing import Optional, Iterator, Literal, Any |
| 1228 |
|
| 1229 |
class DoesNotSuppress1: |
| 1230 |
def __enter__(self) -> int: ... |
| 1231 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> Optional[bool]: ... |
| 1232 |
|
| 1233 |
# Normally, this should suppress. But when strict-optional mode is disabled, we can't |
| 1234 |
# necessarily distinguish between bool and Optional[bool]. So we default to assuming |
| 1235 |
# no suppression, since that's what most context managers will do. |
| 1236 |
class DoesNotSuppress2: |
| 1237 |
def __enter__(self) -> int: ... |
| 1238 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> bool: ... |
| 1239 |
|
| 1240 |
# But if we see Literal[True], it's pretty unlikely the return type is actually meant to |
| 1241 |
# be 'Optional[Literal[True]]'. So, we optimistically assume this is meant to be suppressing. |
| 1242 |
class Suppresses: |
| 1243 |
def __enter__(self) -> int: ... |
| 1244 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> Literal[True]: ... |
| 1245 |
|
| 1246 |
def noop() -> None: ... |
| 1247 |
|
| 1248 |
def f_no_suppress_1() -> int: |
| 1249 |
with DoesNotSuppress1(): |
| 1250 |
return 3 |
| 1251 |
noop() # E: Statement is unreachable |
| 1252 |
|
| 1253 |
def f_no_suppress_2() -> int: |
| 1254 |
with DoesNotSuppress1(): |
| 1255 |
return 3 |
| 1256 |
noop() # E: Statement is unreachable |
| 1257 |
|
| 1258 |
def f_suppress() -> int: # E: Missing return statement |
| 1259 |
with Suppresses(): |
| 1260 |
return 3 |
| 1261 |
noop() |
| 1262 |
[typing fixtures/typing-medium.pyi] |
| 1263 |
[builtins fixtures/tuple.pyi] |
| 1264 |
|
| 1265 |
[case testUnreachableFlagContextAsyncManagersNoSuppress] |
| 1266 |
# flags: --warn-unreachable |
| 1267 |
from contextlib import asynccontextmanager |
| 1268 |
from typing import Optional, AsyncIterator, Literal, Any |
| 1269 |
|
| 1270 |
class DoesNotSuppress1: |
| 1271 |
async def __aenter__(self) -> int: ... |
| 1272 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> Optional[bool]: ... |
| 1273 |
|
| 1274 |
class DoesNotSuppress2: |
| 1275 |
async def __aenter__(self) -> int: ... |
| 1276 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> Literal[False]: ... |
| 1277 |
|
| 1278 |
class DoesNotSuppress3: |
| 1279 |
async def __aenter__(self) -> int: ... |
| 1280 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> Any: ... |
| 1281 |
|
| 1282 |
class DoesNotSuppress4: |
| 1283 |
async def __aenter__(self) -> int: ... |
| 1284 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> None: ... |
| 1285 |
|
| 1286 |
@asynccontextmanager |
| 1287 |
async def simple() -> AsyncIterator[int]: |
| 1288 |
yield 3 |
| 1289 |
|
| 1290 |
def cond() -> bool: ... |
| 1291 |
|
| 1292 |
def noop() -> None: ... |
| 1293 |
|
| 1294 |
async def f_no_suppress_1a() -> int: |
| 1295 |
async with DoesNotSuppress1(): |
| 1296 |
return 3 |
| 1297 |
noop() # E: Statement is unreachable |
| 1298 |
|
| 1299 |
async def f_no_suppress_1b() -> int: |
| 1300 |
async with DoesNotSuppress1(): |
| 1301 |
if cond(): |
| 1302 |
return 3 |
| 1303 |
else: |
| 1304 |
return 3 |
| 1305 |
noop() # E: Statement is unreachable |
| 1306 |
|
| 1307 |
async def f_no_suppress_2() -> int: |
| 1308 |
async with DoesNotSuppress2(): |
| 1309 |
return 3 |
| 1310 |
noop() # E: Statement is unreachable |
| 1311 |
|
| 1312 |
async def f_no_suppress_3() -> int: |
| 1313 |
async with DoesNotSuppress3(): |
| 1314 |
return 3 |
| 1315 |
noop() # E: Statement is unreachable |
| 1316 |
|
| 1317 |
async def f_no_suppress_4() -> int: |
| 1318 |
async with DoesNotSuppress4(): |
| 1319 |
return 3 |
| 1320 |
noop() # E: Statement is unreachable |
| 1321 |
|
| 1322 |
async def f_no_suppress_5() -> int: |
| 1323 |
async with simple(): |
| 1324 |
return 3 |
| 1325 |
noop() # E: Statement is unreachable |
| 1326 |
|
| 1327 |
[typing fixtures/typing-full.pyi] |
| 1328 |
[builtins fixtures/tuple.pyi] |
| 1329 |
|
| 1330 |
[case testUnreachableFlagContextAsyncManagersSuppressed] |
| 1331 |
# flags: --warn-unreachable |
| 1332 |
from contextlib import asynccontextmanager |
| 1333 |
from typing import Optional, AsyncIterator, Literal, Any |
| 1334 |
|
| 1335 |
class DoesNotSuppress: |
| 1336 |
async def __aenter__(self) -> int: ... |
| 1337 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> Optional[bool]: ... |
| 1338 |
|
| 1339 |
class Suppresses1: |
| 1340 |
async def __aenter__(self) -> int: ... |
| 1341 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> bool: ... |
| 1342 |
|
| 1343 |
class Suppresses2: |
| 1344 |
async def __aenter__(self) -> int: ... |
| 1345 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> Literal[True]: ... |
| 1346 |
|
| 1347 |
def cond() -> bool: ... |
| 1348 |
|
| 1349 |
def noop() -> None: ... |
| 1350 |
|
| 1351 |
async def f_suppress_1() -> int: # E: Missing return statement |
| 1352 |
async with Suppresses1(): |
| 1353 |
return 3 |
| 1354 |
noop() |
| 1355 |
|
| 1356 |
async def f_suppress_2() -> int: # E: Missing return statement |
| 1357 |
async with Suppresses1(): |
| 1358 |
if cond(): |
| 1359 |
return 3 |
| 1360 |
else: |
| 1361 |
return 3 |
| 1362 |
noop() |
| 1363 |
|
| 1364 |
async def f_suppress_3() -> int: # E: Missing return statement |
| 1365 |
async with Suppresses2(): |
| 1366 |
return 3 |
| 1367 |
noop() |
| 1368 |
|
| 1369 |
async def f_mix() -> int: # E: Missing return statement |
| 1370 |
async with DoesNotSuppress(), Suppresses1(), DoesNotSuppress(): |
| 1371 |
return 3 |
| 1372 |
noop() |
| 1373 |
[typing fixtures/typing-full.pyi] |
| 1374 |
[builtins fixtures/tuple.pyi] |
| 1375 |
|
| 1376 |
[case testUnreachableFlagContextAsyncManagersAbnormal] |
| 1377 |
# flags: --warn-unreachable |
| 1378 |
from contextlib import asynccontextmanager |
| 1379 |
from typing import Optional, AsyncIterator, Literal, Any |
| 1380 |
|
| 1381 |
class RegularManager: |
| 1382 |
def __enter__(self) -> int: ... |
| 1383 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> bool: ... |
| 1384 |
|
| 1385 |
class AsyncManager: |
| 1386 |
async def __aenter__(self) -> int: ... |
| 1387 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> bool: ... |
| 1388 |
|
| 1389 |
def noop() -> None: ... |
| 1390 |
|
| 1391 |
async def f_bad_1() -> int: |
| 1392 |
async with RegularManager(): # E: "RegularManager" has no attribute "__aenter__"; maybe "__enter__"? \ |
| 1393 |
# E: "RegularManager" has no attribute "__aexit__"; maybe "__exit__"? |
| 1394 |
return 3 |
| 1395 |
noop() # E: Statement is unreachable |
| 1396 |
|
| 1397 |
def f_bad_2() -> int: |
| 1398 |
with AsyncManager(): # E: "AsyncManager" has no attribute "__enter__"; maybe "__aenter__"? \ |
| 1399 |
# E: "AsyncManager" has no attribute "__exit__"; maybe "__aexit__"? |
| 1400 |
return 3 |
| 1401 |
noop() # E: Statement is unreachable |
| 1402 |
|
| 1403 |
# TODO: We should consider reporting an error when the user tries using |
| 1404 |
# context manager with malformed signatures instead of silently continuing. |
| 1405 |
|
| 1406 |
class RegularManagerMalformedSignature: |
| 1407 |
def __enter__(self) -> int: ... |
| 1408 |
def __exit__(self, exctype: object, excvalue: object, traceback: object) -> object: ... |
| 1409 |
|
| 1410 |
class AsyncManagerMalformedSignature: |
| 1411 |
async def __aenter__(self) -> int: ... |
| 1412 |
async def __aexit__(self, exctype: object, excvalue: object, traceback: object) -> object: ... |
| 1413 |
|
| 1414 |
def f_malformed_1() -> int: |
| 1415 |
with RegularManagerMalformedSignature(): |
| 1416 |
return 3 |
| 1417 |
noop() # E: Statement is unreachable |
| 1418 |
|
| 1419 |
async def f_malformed_2() -> int: |
| 1420 |
async with AsyncManagerMalformedSignature(): |
| 1421 |
return 3 |
| 1422 |
noop() # E: Statement is unreachable |
| 1423 |
|
| 1424 |
[typing fixtures/typing-full.pyi] |
| 1425 |
[builtins fixtures/tuple.pyi] |
| 1426 |
|
| 1427 |
[case testUnreachableUntypedFunction] |
| 1428 |
# flags: --warn-unreachable |
| 1429 |
|
| 1430 |
def test_untyped_fn(obj): |
| 1431 |
assert obj.prop is True |
| 1432 |
|
| 1433 |
obj.update(prop=False) |
| 1434 |
obj.reload() |
| 1435 |
|
| 1436 |
assert obj.prop is False |
| 1437 |
reveal_type(obj.prop) |
| 1438 |
|
| 1439 |
def test_typed_fn(obj) -> None: |
| 1440 |
assert obj.prop is True |
| 1441 |
|
| 1442 |
obj.update(prop=False) |
| 1443 |
obj.reload() |
| 1444 |
|
| 1445 |
assert obj.prop is False |
| 1446 |
reveal_type(obj.prop) # E: Statement is unreachable |
| 1447 |
|
| 1448 |
[case testUnreachableCheckedUntypedFunction] |
| 1449 |
# flags: --warn-unreachable --check-untyped-defs |
| 1450 |
|
| 1451 |
def test_untyped_fn(obj): |
| 1452 |
assert obj.prop is True |
| 1453 |
|
| 1454 |
obj.update(prop=False) |
| 1455 |
obj.reload() |
| 1456 |
|
| 1457 |
assert obj.prop is False |
| 1458 |
reveal_type(obj.prop) # E: Statement is unreachable |
| 1459 |
|
| 1460 |
[case testConditionalTypeVarException] |
| 1461 |
# every part of this test case was necessary to trigger the crash |
| 1462 |
import sys |
| 1463 |
from typing import TypeVar |
| 1464 |
|
| 1465 |
T = TypeVar("T", int, str) |
| 1466 |
|
| 1467 |
def f(t: T) -> None: |
| 1468 |
if sys.platform == "lol": |
| 1469 |
try: |
| 1470 |
pass |
| 1471 |
except BaseException as e: |
| 1472 |
pass |
| 1473 |
[builtins fixtures/dict.pyi] |
| 1474 |
|
| 1475 |
|
| 1476 |
[case testUnreachableLiteral] |
| 1477 |
# flags: --warn-unreachable |
| 1478 |
from typing import Literal |
| 1479 |
|
| 1480 |
def nope() -> Literal[False]: ... |
| 1481 |
|
| 1482 |
def f() -> None: |
| 1483 |
if nope(): |
| 1484 |
x = 1 # E: Statement is unreachable |
| 1485 |
[builtins fixtures/dict.pyi] |
| 1486 |
|
| 1487 |
[case testUnreachableLiteralFrom__bool__] |
| 1488 |
# flags: --warn-unreachable |
| 1489 |
from typing import Literal |
| 1490 |
|
| 1491 |
class Truth: |
| 1492 |
def __bool__(self) -> Literal[True]: ... |
| 1493 |
|
| 1494 |
class Lie: |
| 1495 |
def __bool__(self) -> Literal[False]: ... |
| 1496 |
|
| 1497 |
class Maybe: |
| 1498 |
def __bool__(self) -> Literal[True | False]: ... |
| 1499 |
|
| 1500 |
t = Truth() |
| 1501 |
if t: |
| 1502 |
x = 1 |
| 1503 |
else: |
| 1504 |
x = 2 # E: Statement is unreachable |
| 1505 |
|
| 1506 |
if Lie(): |
| 1507 |
x = 3 # E: Statement is unreachable |
| 1508 |
|
| 1509 |
if Maybe(): |
| 1510 |
x = 4 |
| 1511 |
|
| 1512 |
|
| 1513 |
def foo() -> bool: ... |
| 1514 |
|
| 1515 |
y = Truth() or foo() # E: Right operand of "or" is never evaluated |
| 1516 |
z = Lie() and foo() # E: Right operand of "and" is never evaluated |
| 1517 |
[builtins fixtures/dict.pyi] |
| 1518 |
|
| 1519 |
[case testUnreachableModuleBody1] |
| 1520 |
# flags: --warn-unreachable |
| 1521 |
from typing import NoReturn |
| 1522 |
def foo() -> NoReturn: |
| 1523 |
raise Exception("foo") |
| 1524 |
foo() |
| 1525 |
x = 1 # E: Statement is unreachable |
| 1526 |
[builtins fixtures/exception.pyi] |
| 1527 |
|
| 1528 |
[case testUnreachableModuleBody2] |
| 1529 |
# flags: --warn-unreachable |
| 1530 |
raise Exception |
| 1531 |
x = 1 # E: Statement is unreachable |
| 1532 |
[builtins fixtures/exception.pyi] |
| 1533 |
|
| 1534 |
[case testUnreachableNoReturnBinaryOps] |
| 1535 |
# flags: --warn-unreachable |
| 1536 |
from typing import NoReturn |
| 1537 |
|
| 1538 |
a: NoReturn |
| 1539 |
a and 1 # E: Right operand of "and" is never evaluated |
| 1540 |
a or 1 # E: Right operand of "or" is never evaluated |
| 1541 |
a or a # E: Right operand of "or" is never evaluated |
| 1542 |
1 and a and 1 # E: Right operand of "and" is never evaluated |
| 1543 |
a and a # E: Right operand of "and" is never evaluated |
| 1544 |
[builtins fixtures/exception.pyi] |
| 1545 |
|
| 1546 |
[case testUnreachableFlagWithTerminalBranchInDeferredNode] |
| 1547 |
# flags: --warn-unreachable |
| 1548 |
from typing import NoReturn |
| 1549 |
|
| 1550 |
def assert_never(x: NoReturn) -> NoReturn: ... |
| 1551 |
|
| 1552 |
def force_forward_ref() -> int: |
| 1553 |
return 4 |
| 1554 |
|
| 1555 |
def f(value: None) -> None: |
| 1556 |
x |
| 1557 |
if value is not None: |
| 1558 |
assert_never(value) |
| 1559 |
|
| 1560 |
x = force_forward_ref() |
| 1561 |
[builtins fixtures/exception.pyi] |
| 1562 |
|
| 1563 |
[case testSetitemNoReturn] |
| 1564 |
# flags: --warn-unreachable |
| 1565 |
from typing import NoReturn |
| 1566 |
class Foo: |
| 1567 |
def __setitem__(self, key: str, value: str) -> NoReturn: |
| 1568 |
raise Exception |
| 1569 |
Foo()['a'] = 'a' |
| 1570 |
x = 0 # E: Statement is unreachable |
| 1571 |
[builtins fixtures/exception.pyi] |
| 1572 |
|
| 1573 |
[case TestNoImplicNoReturnFromError] |
| 1574 |
# flags: --warn-unreachable |
| 1575 |
from typing import TypeVar |
| 1576 |
|
| 1577 |
T = TypeVar("T") |
| 1578 |
class Foo: |
| 1579 |
def __setitem__(self, key: str, value: str) -> T: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar |
| 1580 |
raise Exception |
| 1581 |
|
| 1582 |
def f() -> None: |
| 1583 |
Foo()['a'] = 'a' |
| 1584 |
x = 0 # This should not be reported as unreachable |
| 1585 |
[builtins fixtures/exception.pyi] |
| 1586 |
|
| 1587 |
[case testIntentionallyEmptyGeneratorFunction] |
| 1588 |
# flags: --warn-unreachable |
| 1589 |
from typing import Generator |
| 1590 |
|
| 1591 |
def f() -> Generator[None, None, None]: |
| 1592 |
return |
| 1593 |
yield |
| 1594 |
|
| 1595 |
[case testIntentionallyEmptyGeneratorFunction_None] |
| 1596 |
# flags: --warn-unreachable |
| 1597 |
from typing import Generator |
| 1598 |
|
| 1599 |
def f() -> Generator[None, None, None]: |
| 1600 |
return None |
| 1601 |
yield None |
| 1602 |
|
| 1603 |
[case testLambdaNoReturn] |
| 1604 |
# flags: --warn-unreachable |
| 1605 |
from typing import Callable, NoReturn |
| 1606 |
|
| 1607 |
def foo() -> NoReturn: |
| 1608 |
raise |
| 1609 |
|
| 1610 |
f1 = lambda: foo() |
| 1611 |
x = 0 # not unreachable |
| 1612 |
|
| 1613 |
f2: Callable[[], NoReturn] = lambda: foo() |
| 1614 |
x = 0 # not unreachable |
| 1615 |
|
| 1616 |
[case testAttributeNoReturn] |
| 1617 |
# flags: --warn-unreachable |
| 1618 |
from typing import Optional, NoReturn, TypeVar |
| 1619 |
|
| 1620 |
def foo() -> NoReturn: |
| 1621 |
raise |
| 1622 |
|
| 1623 |
T = TypeVar("T") |
| 1624 |
def bar(x: Optional[list[T]] = None) -> T: |
| 1625 |
... |
| 1626 |
|
| 1627 |
reveal_type(bar().attr) # N: Revealed type is "Never" |
| 1628 |
1 # not unreachable |
| 1629 |
reveal_type(foo().attr) # N: Revealed type is "Never" |
| 1630 |
1 # E: Statement is unreachable |
| 1631 |
|
| 1632 |
[case testIgnoreReturningNotImplemented] |
| 1633 |
# flags: --warn-unreachable |
| 1634 |
|
| 1635 |
class C: |
| 1636 |
def __add__(self, o: C) -> C: |
| 1637 |
if not isinstance(o, C): |
| 1638 |
return NotImplemented |
| 1639 |
return C() |
| 1640 |
def __sub__(self, o: C) -> C: |
| 1641 |
if isinstance(o, C): |
| 1642 |
return C() |
| 1643 |
return NotImplemented |
| 1644 |
def __mul__(self, o: C) -> C: |
| 1645 |
if isinstance(o, C): |
| 1646 |
return C() |
| 1647 |
else: |
| 1648 |
return NotImplemented |
| 1649 |
[builtins fixtures/isinstance.pyi] |
| 1650 |
|
| 1651 |
[case testIgnoreReturningNotImplementedSimple] |
| 1652 |
# flags: --warn-unreachable |
| 1653 |
-- This behavior may or may not actually be desirable. |
| 1654 |
-- This test just serves to document mypy's current behavior. |
| 1655 |
-- And mypy's handling of NotImplemented has long been known |
| 1656 |
-- to be suboptimal, eg https://github.com/python/mypy/issues/363. |
| 1657 |
-- If you change this behavior, please change the corresponding docs |
| 1658 |
-- https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-warn-unreachable |
| 1659 |
def foo() -> None: |
| 1660 |
return None |
| 1661 |
return NotImplemented #no error |
| 1662 |
return None # E: Statement is unreachable |
| 1663 |
[builtins fixtures/isinstance.pyi] |
| 1664 |
|
| 1665 |
[case testPassAndEllipsisUnreachable] |
| 1666 |
# flags: --warn-unreachable |
| 1667 |
def f() -> None: |
| 1668 |
return |
| 1669 |
pass # E: Statement is unreachable |
| 1670 |
|
| 1671 |
def g() -> None: |
| 1672 |
return |
| 1673 |
... # E: Statement is unreachable |
| 1674 |
[builtins fixtures/isinstance.pyi] |
| 1675 |
|
| 1676 |
[case testUnreachableStatementPrettyHighlighting] |
| 1677 |
# flags: --warn-unreachable --pretty |
| 1678 |
def x() -> None: |
| 1679 |
assert False |
| 1680 |
if 5: |
| 1681 |
pass |
| 1682 |
[out] |
| 1683 |
main:4: error: Statement is unreachable |
| 1684 |
if 5: |
| 1685 |
^~~~~ |
| 1686 |
|
| 1687 |
[case testReachableEqualityNarrowingAny] |
| 1688 |
# flags: --warn-unreachable |
| 1689 |
# https://github.com/python/mypy/issues/20532 |
| 1690 |
from __future__ import annotations |
| 1691 |
from typing import Any |
| 1692 |
|
| 1693 |
def print(s: str): pass |
| 1694 |
|
| 1695 |
def main(contents: Any, commit: str | None) -> None: |
| 1696 |
if ( |
| 1697 |
contents.get("commit") == commit |
| 1698 |
and (commit is not None or print("can_be_reached")) |
| 1699 |
): |
| 1700 |
pass |
| 1701 |
|
| 1702 |
main({"commit": None}, None) |
| 1703 |
[builtins fixtures/tuple.pyi] |
| 1704 |
|
| 1705 |
[case testUnreachableFunctionAfterTopLevelAssert] |
| 1706 |
def foo() -> int: |
| 1707 |
... |
| 1708 |
|
| 1709 |
raise Exception |
| 1710 |
|
| 1711 |
x = foo() |
| 1712 |
|
| 1713 |
def test() -> None: |
| 1714 |
# It is questionable to allow this, this test exists to check 1:1 |
| 1715 |
# behavior match of parallel checking with sequential checking. |
| 1716 |
x + "hm..." |
| 1717 |
|
| 1718 |
class C: |
| 1719 |
def test(self) -> None: |
| 1720 |
x + "same here..." |
| 1721 |
[builtins fixtures/exception.pyi] |
| 1722 |
|
| 1723 |
[case testUnreachableFunctionAfterClassLevelAssert] |
| 1724 |
def foo() -> int: |
| 1725 |
... |
| 1726 |
|
| 1727 |
class C: |
| 1728 |
x = foo() |
| 1729 |
raise Exception |
| 1730 |
def test(self) -> None: |
| 1731 |
# It is questionable to allow this, this test exists to check 1:1 |
| 1732 |
# behavior match of parallel checking with sequential checking. |
| 1733 |
C.x + "hm..." |
| 1734 |
|
| 1735 |
def test_outer() -> None: |
| 1736 |
C.x + "no way" # E: Unsupported left operand type for + ("int") |
| 1737 |
[builtins fixtures/exception.pyi] |
| 1738 |
|
| 1739 |
[case testIgnoreInPlatformSpecificCodeUsed] |
| 1740 |
# flags: --warn-unused-ignores --platform unknown |
| 1741 |
|
| 1742 |
import sys |
| 1743 |
assert sys.platform == "win32" |
| 1744 |
|
| 1745 |
42 + "no way" # type: ignore[operator] |
| 1746 |
[builtins fixtures/isinstancelist.pyi] |