| 1 |
-- Test cases for import returned by the native parser using a side channel |
| 2 |
-- In particular, test import flags related to reachability and priority when ordering SCCs |
| 3 |
|
| 4 |
[case testNoImport] |
| 5 |
# No imports |
| 6 |
print() |
| 7 |
[out] |
| 8 |
|
| 9 |
[case testSimpleImport] |
| 10 |
import foo |
| 11 |
[out] |
| 12 |
1: import foo |
| 13 |
|
| 14 |
[case testMultipleImports] |
| 15 |
import foo |
| 16 |
import bar |
| 17 |
import baz |
| 18 |
[out] |
| 19 |
1: import foo |
| 20 |
2: import bar |
| 21 |
3: import baz |
| 22 |
|
| 23 |
[case testImportAs] |
| 24 |
import foo as f |
| 25 |
import bar as b |
| 26 |
[out] |
| 27 |
1: import foo as f |
| 28 |
2: import bar as b |
| 29 |
|
| 30 |
[case testFromImport] |
| 31 |
from foo import bar |
| 32 |
[out] |
| 33 |
1: from foo import bar |
| 34 |
|
| 35 |
[case testFromImportMultiple] |
| 36 |
from foo import bar, baz, qux |
| 37 |
[out] |
| 38 |
1: from foo import bar, baz, qux |
| 39 |
|
| 40 |
[case testFromImportAs] |
| 41 |
from foo import bar as b, baz as z |
| 42 |
[out] |
| 43 |
1: from foo import bar as b, baz as z |
| 44 |
|
| 45 |
[case testRelativeImport] |
| 46 |
from . import foo |
| 47 |
from .. import bar |
| 48 |
from ...baz import qux |
| 49 |
[out] |
| 50 |
1: from . import foo |
| 51 |
2: from .. import bar |
| 52 |
3: from ...baz import qux |
| 53 |
|
| 54 |
[case testNonTopLevelImport] |
| 55 |
def f(): |
| 56 |
import foo |
| 57 |
from bar import baz |
| 58 |
[out] |
| 59 |
2: import foo [not top_level] |
| 60 |
3: from bar import baz [not top_level] |
| 61 |
|
| 62 |
[case testImportInClass] |
| 63 |
class Foo: |
| 64 |
import bar |
| 65 |
[out] |
| 66 |
2: import bar |
| 67 |
[case testUnreachableImportPY2] |
| 68 |
# Imports in unreachable blocks should not appear |
| 69 |
if PY2: |
| 70 |
import unreachable_module |
| 71 |
import reachable_module |
| 72 |
[out] |
| 73 |
4: import reachable_module |
| 74 |
|
| 75 |
[case testReachableImportPY3] |
| 76 |
# Imports in reachable if PY3 blocks should appear |
| 77 |
if PY3: |
| 78 |
import reachable_module |
| 79 |
[out] |
| 80 |
3: import reachable_module |
| 81 |
|
| 82 |
[case testUnreachableElseBlock] |
| 83 |
# Imports in unreachable else block should not appear |
| 84 |
if PY3: |
| 85 |
import reachable |
| 86 |
else: |
| 87 |
import unreachable |
| 88 |
[out] |
| 89 |
3: import reachable |
| 90 |
|
| 91 |
[case testMixedReachableUnreachable] |
| 92 |
# Mix of reachable and unreachable imports |
| 93 |
import before |
| 94 |
if PY2: |
| 95 |
import unreachable_in_if |
| 96 |
else: |
| 97 |
import reachable_in_else |
| 98 |
import after |
| 99 |
[out] |
| 100 |
2: import before |
| 101 |
6: import reachable_in_else |
| 102 |
7: import after |
| 103 |
|
| 104 |
[case testNestedUnreachable] |
| 105 |
# Nested unreachable blocks |
| 106 |
if PY2: |
| 107 |
import outer_unreachable |
| 108 |
if True: |
| 109 |
import inner_unreachable |
| 110 |
import reachable |
| 111 |
[out] |
| 112 |
6: import reachable |
| 113 |
|
| 114 |
[case testUnreachableWithTopLevel] |
| 115 |
# Unreachable imports together with top_level flag handling |
| 116 |
def f(): |
| 117 |
if PY2: |
| 118 |
import unreachable_func |
| 119 |
import reachable_func |
| 120 |
if PY2: |
| 121 |
import unreachable_top |
| 122 |
import reachable_top |
| 123 |
[out] |
| 124 |
5: import reachable_func [not top_level] |
| 125 |
8: import reachable_top |
| 126 |
|
| 127 |
[case testVersionCheckUnreachable] |
| 128 |
# sys.version_info check makes else unreachable |
| 129 |
import sys |
| 130 |
if sys.version_info >= (3, 8): |
| 131 |
import reachable_version_check |
| 132 |
else: |
| 133 |
import unreachable_old_version |
| 134 |
[out] |
| 135 |
2: import sys |
| 136 |
4: import reachable_version_check |
| 137 |
|
| 138 |
[case testMultipeBranchesUnreachable] |
| 139 |
if PY2: |
| 140 |
import unreachable_if |
| 141 |
elif PY2: |
| 142 |
import unreachable_elif |
| 143 |
else: |
| 144 |
import reachable_else |
| 145 |
[out] |
| 146 |
6: import reachable_else |
| 147 |
|
| 148 |
[case testMypyOnlyImport] |
| 149 |
# Imports in TYPE_CHECKING blocks are mypy_only |
| 150 |
from typing import TYPE_CHECKING |
| 151 |
if TYPE_CHECKING: |
| 152 |
import mypy_only_module |
| 153 |
import regular_module |
| 154 |
[out] |
| 155 |
2: from typing import TYPE_CHECKING |
| 156 |
4: import mypy_only_module [mypy_only] |
| 157 |
5: import regular_module |
| 158 |
|
| 159 |
[case testMypyNameAlias] |
| 160 |
# MYPY is also recognized as mypy_only |
| 161 |
if MYPY: |
| 162 |
import only_for_mypy |
| 163 |
import for_everyone |
| 164 |
[out] |
| 165 |
3: import only_for_mypy [mypy_only] |
| 166 |
4: import for_everyone |
| 167 |
|
| 168 |
[case testMypyOnlyElseBranch] |
| 169 |
# Else branch of TYPE_CHECKING is unreachable (runtime-only, not analyzed by mypy) |
| 170 |
from typing import TYPE_CHECKING |
| 171 |
if TYPE_CHECKING: |
| 172 |
import for_mypy |
| 173 |
else: |
| 174 |
import for_runtime |
| 175 |
[out] |
| 176 |
2: from typing import TYPE_CHECKING |
| 177 |
4: import for_mypy [mypy_only] |
| 178 |
|
| 179 |
[case testNestedMypyOnly] |
| 180 |
# Nested blocks inside TYPE_CHECKING are also mypy_only |
| 181 |
from typing import TYPE_CHECKING |
| 182 |
if TYPE_CHECKING: |
| 183 |
if True: |
| 184 |
import deeply_nested |
| 185 |
import also_mypy_only |
| 186 |
[out] |
| 187 |
2: from typing import TYPE_CHECKING |
| 188 |
5: import deeply_nested [mypy_only] |
| 189 |
6: import also_mypy_only [mypy_only] |
| 190 |
|
| 191 |
[case testMypyOnlyInFunction] |
| 192 |
# Mypy-only imports in functions |
| 193 |
from typing import TYPE_CHECKING |
| 194 |
def f(): |
| 195 |
if TYPE_CHECKING: |
| 196 |
import mypy_func_import |
| 197 |
import regular_func_import |
| 198 |
[out] |
| 199 |
2: from typing import TYPE_CHECKING |
| 200 |
5: import mypy_func_import [not top_level, mypy_only] |
| 201 |
6: import regular_func_import [not top_level] |
| 202 |
|
| 203 |
[case testMypyOnlyElifBranch] |
| 204 |
# Elif after TYPE_CHECKING is unreachable (runtime-only) |
| 205 |
from typing import TYPE_CHECKING |
| 206 |
if TYPE_CHECKING: |
| 207 |
import for_mypy |
| 208 |
elif True: |
| 209 |
import for_runtime_elif |
| 210 |
[out] |
| 211 |
2: from typing import TYPE_CHECKING |
| 212 |
4: import for_mypy [mypy_only] |
| 213 |
|
| 214 |
[case testNotMypyFalseBranch] |
| 215 |
# Not TYPE_CHECKING makes body unreachable, else is mypy_only |
| 216 |
from typing import TYPE_CHECKING |
| 217 |
if not TYPE_CHECKING: |
| 218 |
import runtime_only |
| 219 |
else: |
| 220 |
import mypy_only |
| 221 |
[out] |
| 222 |
2: from typing import TYPE_CHECKING |
| 223 |
6: import mypy_only [mypy_only] |
| 224 |
|
| 225 |
[case testMixedMypyOnlyAndUnreachable] |
| 226 |
# Combination of mypy_only and unreachable |
| 227 |
from typing import TYPE_CHECKING |
| 228 |
if PY2: |
| 229 |
import unreachable_py2 |
| 230 |
if TYPE_CHECKING: |
| 231 |
import mypy_only_import |
| 232 |
if PY3: |
| 233 |
import always_reachable |
| 234 |
[out] |
| 235 |
2: from typing import TYPE_CHECKING |
| 236 |
6: import mypy_only_import [mypy_only] |
| 237 |
8: import always_reachable |
| 238 |
|
| 239 |
[case testTypingExtensionsTypeChecking] |
| 240 |
# typing_extensions.TYPE_CHECKING is also recognized |
| 241 |
from typing_extensions import TYPE_CHECKING |
| 242 |
if TYPE_CHECKING: |
| 243 |
import mypy_import |
| 244 |
[out] |
| 245 |
2: from typing_extensions import TYPE_CHECKING |
| 246 |
4: import mypy_import [mypy_only] |
| 247 |
|
| 248 |
[case testUnknownCondition] |
| 249 |
# Unknown conditions - all branches reachable |
| 250 |
if x: |
| 251 |
import in_if |
| 252 |
else: |
| 253 |
import in_else |
| 254 |
[out] |
| 255 |
3: import in_if |
| 256 |
5: import in_else |
| 257 |
|
| 258 |
[case testUnknownConditionWithElif] |
| 259 |
# Unknown conditions with elif |
| 260 |
if unknown_var: |
| 261 |
import first |
| 262 |
elif another_var: |
| 263 |
import second |
| 264 |
else: |
| 265 |
import third |
| 266 |
[out] |
| 267 |
3: import first |
| 268 |
5: import second |
| 269 |
7: import third |
| 270 |
|
| 271 |
[case testMixedKnownUnknown] |
| 272 |
# Mix of known and unknown conditions |
| 273 |
if TYPE_CHECKING: |
| 274 |
import mypy_only |
| 275 |
if some_var: |
| 276 |
import maybe_reachable |
| 277 |
if PY2: |
| 278 |
import unreachable |
| 279 |
[out] |
| 280 |
3: import mypy_only [mypy_only] |
| 281 |
5: import maybe_reachable |
| 282 |
|
| 283 |
[case testNestedUnknownInMypyOnly] |
| 284 |
# Unknown condition nested in mypy-only block |
| 285 |
from typing import TYPE_CHECKING |
| 286 |
if TYPE_CHECKING: |
| 287 |
if condition: |
| 288 |
import nested_mypy |
| 289 |
import also_mypy |
| 290 |
[out] |
| 291 |
2: from typing import TYPE_CHECKING |
| 292 |
5: import nested_mypy [mypy_only] |
| 293 |
6: import also_mypy [mypy_only] |
| 294 |
|
| 295 |
[case testUnknownAfterMypyTrue] |
| 296 |
# Unknown elif after TYPE_CHECKING is unreachable |
| 297 |
from typing import TYPE_CHECKING |
| 298 |
if TYPE_CHECKING: |
| 299 |
import for_mypy |
| 300 |
elif x: |
| 301 |
import unreachable_elif |
| 302 |
[out] |
| 303 |
2: from typing import TYPE_CHECKING |
| 304 |
4: import for_mypy [mypy_only] |
| 305 |
|
| 306 |
[case testStarImport] |
| 307 |
from foo import * |
| 308 |
[out] |
| 309 |
1: from foo import * |
| 310 |
|
| 311 |
[case testStarImportInFunction] |
| 312 |
def f(): |
| 313 |
from foo import * |
| 314 |
[out] |
| 315 |
2: from foo import * [not top_level] |
| 316 |
|
| 317 |
[case testMypyOnlyStarImport] |
| 318 |
from typing import TYPE_CHECKING |
| 319 |
if TYPE_CHECKING: |
| 320 |
from mypy_types import * |
| 321 |
from regular import * |
| 322 |
[out] |
| 323 |
1: from typing import TYPE_CHECKING |
| 324 |
3: from mypy_types import * [mypy_only] |
| 325 |
4: from regular import * |
| 326 |
|
| 327 |
[case testUnreachableStarImport] |
| 328 |
if PY2: |
| 329 |
from unreachable import * |
| 330 |
from reachable import * |
| 331 |
[out] |
| 332 |
3: from reachable import * |
| 333 |
|
| 334 |
[case testNotPY2] |
| 335 |
# not PY2 should be always true |
| 336 |
if not PY2: |
| 337 |
import always_reachable |
| 338 |
[out] |
| 339 |
3: import always_reachable |
| 340 |
|
| 341 |
[case testNotPY3] |
| 342 |
# not PY3 should be always false |
| 343 |
if not PY3: |
| 344 |
import unreachable |
| 345 |
import reachable |
| 346 |
[out] |
| 347 |
4: import reachable |
| 348 |
|
| 349 |
[case testNotPY3Else] |
| 350 |
# not PY3 else branch should be reachable |
| 351 |
if not PY3: |
| 352 |
import unreachable_if |
| 353 |
else: |
| 354 |
import reachable_else |
| 355 |
[out] |
| 356 |
5: import reachable_else |
| 357 |
|
| 358 |
[case testBoolAndAlwaysTrueWithMypyTrue] |
| 359 |
# PY3 and TYPE_CHECKING - both true, result is mypy_true |
| 360 |
from typing import TYPE_CHECKING |
| 361 |
if PY3 and TYPE_CHECKING: |
| 362 |
import mypy_only_import |
| 363 |
[out] |
| 364 |
2: from typing import TYPE_CHECKING |
| 365 |
4: import mypy_only_import [mypy_only] |
| 366 |
|
| 367 |
[case testBoolAndAlwaysTrueWithUnknown] |
| 368 |
# PY3 and unknown - result is unknown, both branches reachable |
| 369 |
if PY3 and some_var: |
| 370 |
import maybe_reachable |
| 371 |
else: |
| 372 |
import also_maybe_reachable |
| 373 |
[out] |
| 374 |
3: import maybe_reachable |
| 375 |
5: import also_maybe_reachable |
| 376 |
|
| 377 |
[case testBoolAndAlwaysFalseWithAnything] |
| 378 |
# PY2 and anything - result is always false |
| 379 |
if PY2 and some_var: |
| 380 |
import unreachable |
| 381 |
import reachable |
| 382 |
[out] |
| 383 |
4: import reachable |
| 384 |
|
| 385 |
[case testBoolAndMypyFalseWithAlwaysTrue] |
| 386 |
# not TYPE_CHECKING and PY3 - result is mypy_false, else is mypy_only |
| 387 |
from typing import TYPE_CHECKING |
| 388 |
if not TYPE_CHECKING and PY3: |
| 389 |
import runtime_only |
| 390 |
else: |
| 391 |
import mypy_only_import |
| 392 |
[out] |
| 393 |
2: from typing import TYPE_CHECKING |
| 394 |
6: import mypy_only_import [mypy_only] |
| 395 |
|
| 396 |
[case testBoolAndMypyTrueOnly] |
| 397 |
# MYPY and TYPE_CHECKING - both mypy_true, result is mypy_true |
| 398 |
from typing import TYPE_CHECKING |
| 399 |
if MYPY and TYPE_CHECKING: |
| 400 |
import mypy_only_import |
| 401 |
[out] |
| 402 |
2: from typing import TYPE_CHECKING |
| 403 |
4: import mypy_only_import [mypy_only] |
| 404 |
|
| 405 |
[case testBoolOrAlwaysTrueWithAnything] |
| 406 |
# PY3 or anything - result is always true |
| 407 |
if PY3 or some_var: |
| 408 |
import always_reachable |
| 409 |
else: |
| 410 |
import unreachable_else |
| 411 |
[out] |
| 412 |
3: import always_reachable |
| 413 |
|
| 414 |
[case testBoolOrMypyTrueWithAlwaysFalse] |
| 415 |
# TYPE_CHECKING or PY2 - result is mypy_true |
| 416 |
from typing import TYPE_CHECKING |
| 417 |
if TYPE_CHECKING or PY2: |
| 418 |
import mypy_only_import |
| 419 |
[out] |
| 420 |
2: from typing import TYPE_CHECKING |
| 421 |
4: import mypy_only_import [mypy_only] |
| 422 |
|
| 423 |
[case testBoolOrMypyFalseOnly] |
| 424 |
# not MYPY or not TYPE_CHECKING - all mypy_false, result is mypy_false, else is mypy_only |
| 425 |
from typing import TYPE_CHECKING |
| 426 |
if not MYPY or not TYPE_CHECKING: |
| 427 |
import runtime_only |
| 428 |
else: |
| 429 |
import mypy_only_import |
| 430 |
[out] |
| 431 |
2: from typing import TYPE_CHECKING |
| 432 |
6: import mypy_only_import [mypy_only] |
| 433 |
|
| 434 |
[case testBoolOrAlwaysFalseWithUnknown] |
| 435 |
# PY2 or unknown - result is unknown, both branches reachable |
| 436 |
if PY2 or some_var: |
| 437 |
import maybe_reachable |
| 438 |
else: |
| 439 |
import also_maybe_reachable |
| 440 |
[out] |
| 441 |
3: import maybe_reachable |
| 442 |
5: import also_maybe_reachable |
| 443 |
|
| 444 |
[case testBoolOrAllFalse] |
| 445 |
# PY2 or not TYPE_CHECKING - all false values, result is always_false, else is unreachable |
| 446 |
from typing import TYPE_CHECKING |
| 447 |
if PY2 or not TYPE_CHECKING: |
| 448 |
import runtime_only |
| 449 |
else: |
| 450 |
import mypy_only_import |
| 451 |
[out] |
| 452 |
2: from typing import TYPE_CHECKING |
| 453 |
6: import mypy_only_import |
| 454 |
|
| 455 |
[case testBoolComplexAndOr] |
| 456 |
# Complex: (PY3 and TYPE_CHECKING) or PY2 - evaluates to mypy_true |
| 457 |
from typing import TYPE_CHECKING |
| 458 |
if (PY3 and TYPE_CHECKING) or PY2: |
| 459 |
import mypy_only_import |
| 460 |
[out] |
| 461 |
2: from typing import TYPE_CHECKING |
| 462 |
4: import mypy_only_import [mypy_only] |
| 463 |
|
| 464 |
[case testBoolMultipleOr] |
| 465 |
# Multiple or: PY2 or PY2 or TYPE_CHECKING - result is mypy_true |
| 466 |
from typing import TYPE_CHECKING |
| 467 |
if PY2 or PY2 or TYPE_CHECKING: |
| 468 |
import mypy_only_import |
| 469 |
[out] |
| 470 |
2: from typing import TYPE_CHECKING |
| 471 |
4: import mypy_only_import [mypy_only] |
| 472 |
|
| 473 |
[case testBoolMultipleAnd] |
| 474 |
# Multiple and: PY3 and PY3 and PY3 - result is always_true |
| 475 |
if PY3 and PY3 and PY3: |
| 476 |
import always_reachable |
| 477 |
else: |
| 478 |
import unreachable_else |
| 479 |
[out] |
| 480 |
3: import always_reachable |
| 481 |
|
| 482 |
[case testVersionInfoIndexMajor] |
| 483 |
# sys.version_info[0] == 3 should be always true for Python 3.10 |
| 484 |
import sys |
| 485 |
if sys.version_info[0] == 3: |
| 486 |
import reachable_py3 |
| 487 |
else: |
| 488 |
import unreachable_not_py3 |
| 489 |
[out] |
| 490 |
2: import sys |
| 491 |
4: import reachable_py3 |
| 492 |
|
| 493 |
[case testVersionInfoIndexMajorNotEqual] |
| 494 |
# sys.version_info[0] == 2 should be always false for Python 3.10 |
| 495 |
import sys |
| 496 |
if sys.version_info[0] == 2: |
| 497 |
import unreachable_py2 |
| 498 |
import reachable |
| 499 |
[out] |
| 500 |
2: import sys |
| 501 |
5: import reachable |
| 502 |
|
| 503 |
[case testVersionInfoIndexMinor] |
| 504 |
# sys.version_info[1] >= 10 should be always true for Python 3.10 |
| 505 |
import sys |
| 506 |
if sys.version_info[1] >= 10: |
| 507 |
import reachable_310_plus |
| 508 |
else: |
| 509 |
import unreachable_pre_310 |
| 510 |
[out] |
| 511 |
2: import sys |
| 512 |
4: import reachable_310_plus |
| 513 |
|
| 514 |
[case testVersionInfoIndexMinorLess] |
| 515 |
# sys.version_info[1] < 10 should be always false for Python 3.10 |
| 516 |
import sys |
| 517 |
if sys.version_info[1] < 10: |
| 518 |
import unreachable_pre_310 |
| 519 |
import reachable |
| 520 |
[out] |
| 521 |
2: import sys |
| 522 |
5: import reachable |
| 523 |
|
| 524 |
[case testVersionInfoIndexMinorGreater] |
| 525 |
# sys.version_info[1] > 9 should be always true for Python 3.10 |
| 526 |
import sys |
| 527 |
if sys.version_info[1] > 9: |
| 528 |
import reachable_310_plus |
| 529 |
[out] |
| 530 |
2: import sys |
| 531 |
4: import reachable_310_plus |
| 532 |
|
| 533 |
[case testVersionInfoIndexLessOrEqual] |
| 534 |
# sys.version_info[0] <= 3 should be always true for Python 3.10 |
| 535 |
import sys |
| 536 |
if sys.version_info[0] <= 3: |
| 537 |
import reachable_py3_or_less |
| 538 |
[out] |
| 539 |
2: import sys |
| 540 |
4: import reachable_py3_or_less |
| 541 |
|
| 542 |
[case testVersionInfoIndexNotEqual] |
| 543 |
# sys.version_info[0] != 2 should be always true for Python 3.10 |
| 544 |
import sys |
| 545 |
if sys.version_info[0] != 2: |
| 546 |
import reachable_not_py2 |
| 547 |
[out] |
| 548 |
2: import sys |
| 549 |
4: import reachable_not_py2 |
| 550 |
|
| 551 |
[case testVersionInfoSliceExplicit] |
| 552 |
# sys.version_info[:2] >= (3, 10) should be always true for Python 3.10 |
| 553 |
import sys |
| 554 |
if sys.version_info[:2] >= (3, 10): |
| 555 |
import reachable_310_plus |
| 556 |
else: |
| 557 |
import unreachable_pre_310 |
| 558 |
[out] |
| 559 |
2: import sys |
| 560 |
4: import reachable_310_plus |
| 561 |
|
| 562 |
[case testVersionInfoSliceExplicitBothBounds] |
| 563 |
# sys.version_info[0:2] >= (3, 10) should be always true for Python 3.10 |
| 564 |
import sys |
| 565 |
if sys.version_info[0:2] >= (3, 10): |
| 566 |
import reachable_310_plus |
| 567 |
[out] |
| 568 |
2: import sys |
| 569 |
4: import reachable_310_plus |
| 570 |
|
| 571 |
[case testVersionInfoSliceExplicitLess] |
| 572 |
# sys.version_info[:2] < (3, 10) should be always false for Python 3.10 |
| 573 |
import sys |
| 574 |
if sys.version_info[:2] < (3, 10): |
| 575 |
import unreachable_pre_310 |
| 576 |
import reachable |
| 577 |
[out] |
| 578 |
2: import sys |
| 579 |
5: import reachable |
| 580 |
|
| 581 |
[case testVersionInfoSliceSingleElement] |
| 582 |
# sys.version_info[:1] >= (3,) should be always true for Python 3.10 |
| 583 |
import sys |
| 584 |
if sys.version_info[:1] >= (3,): |
| 585 |
import reachable_py3 |
| 586 |
[out] |
| 587 |
2: import sys |
| 588 |
4: import reachable_py3 |
| 589 |
|
| 590 |
[case testVersionInfoSliceSingleElementLess] |
| 591 |
# sys.version_info[:1] < (3,) should be always false for Python 3.10 |
| 592 |
import sys |
| 593 |
if sys.version_info[:1] < (3,): |
| 594 |
import unreachable_py2 |
| 595 |
import reachable |
| 596 |
[out] |
| 597 |
2: import sys |
| 598 |
5: import reachable |
| 599 |
|
| 600 |
[case testVersionInfoReversedOperands] |
| 601 |
# (3, 8) <= sys.version_info should be always true for Python 3.10 |
| 602 |
import sys |
| 603 |
if (3, 8) <= sys.version_info: |
| 604 |
import reachable_38_plus |
| 605 |
[out] |
| 606 |
2: import sys |
| 607 |
4: import reachable_38_plus |
| 608 |
|
| 609 |
[case testVersionInfoReversedGreater] |
| 610 |
# (3, 11) > sys.version_info should be always true for Python 3.10 |
| 611 |
import sys |
| 612 |
if (3, 11) > sys.version_info: |
| 613 |
import reachable_pre_311 |
| 614 |
[out] |
| 615 |
2: import sys |
| 616 |
4: import reachable_pre_311 |
| 617 |
|
| 618 |
[case testVersionInfoReversedEquals] |
| 619 |
# (3, 10) == sys.version_info should be always true for Python 3.10 |
| 620 |
import sys |
| 621 |
if (3, 10) == sys.version_info: |
| 622 |
import reachable_exactly_310 |
| 623 |
[out] |
| 624 |
2: import sys |
| 625 |
4: import reachable_exactly_310 |
| 626 |
|
| 627 |
[case testVersionInfoEquals] |
| 628 |
# sys.version_info == (3, 10) should be always true for Python 3.10 |
| 629 |
import sys |
| 630 |
if sys.version_info == (3, 10): |
| 631 |
import reachable_exactly_310 |
| 632 |
else: |
| 633 |
import unreachable_not_310 |
| 634 |
[out] |
| 635 |
2: import sys |
| 636 |
4: import reachable_exactly_310 |
| 637 |
|
| 638 |
[case testVersionInfoNotEquals] |
| 639 |
# sys.version_info != (3, 11) should be always true for Python 3.10 |
| 640 |
import sys |
| 641 |
if sys.version_info != (3, 11): |
| 642 |
import reachable_not_311 |
| 643 |
[out] |
| 644 |
2: import sys |
| 645 |
4: import reachable_not_311 |
| 646 |
|
| 647 |
[case testVersionInfoNotEqualsExact] |
| 648 |
# sys.version_info != (3, 10) should be always false for Python 3.10 |
| 649 |
import sys |
| 650 |
if sys.version_info != (3, 10): |
| 651 |
import unreachable_not_310 |
| 652 |
import reachable |
| 653 |
[out] |
| 654 |
2: import sys |
| 655 |
5: import reachable |
| 656 |
|
| 657 |
[case testVersionInfoSliceMinorOnly] |
| 658 |
# sys.version_info[1:2] >= (10,) should be always true for Python 3.10 |
| 659 |
import sys |
| 660 |
if sys.version_info[1:2] >= (10,): |
| 661 |
import reachable_minor_10_plus |
| 662 |
[out] |
| 663 |
2: import sys |
| 664 |
4: import reachable_minor_10_plus |
| 665 |
|
| 666 |
[case testAttributeTypeChecking] |
| 667 |
# typing.TYPE_CHECKING should be recognized as mypy_true |
| 668 |
import typing |
| 669 |
if typing.TYPE_CHECKING: |
| 670 |
import mypy_only_import |
| 671 |
[out] |
| 672 |
2: import typing |
| 673 |
4: import mypy_only_import [mypy_only] |
| 674 |
|
| 675 |
[case testAttributeMYPY] |
| 676 |
# foo.MYPY should be recognized as mypy_true |
| 677 |
import foo |
| 678 |
if foo.MYPY: |
| 679 |
import mypy_only_import |
| 680 |
[out] |
| 681 |
2: import foo |
| 682 |
4: import mypy_only_import [mypy_only] |
| 683 |
|
| 684 |
[case testAttributePY2] |
| 685 |
# bar.PY2 should be recognized as always_false |
| 686 |
import bar |
| 687 |
if bar.PY2: |
| 688 |
import unreachable |
| 689 |
import reachable |
| 690 |
[out] |
| 691 |
2: import bar |
| 692 |
5: import reachable |
| 693 |
|
| 694 |
[case testAttributePY3] |
| 695 |
# baz.PY3 should be recognized as always_true |
| 696 |
import baz |
| 697 |
if baz.PY3: |
| 698 |
import always_reachable |
| 699 |
[out] |
| 700 |
2: import baz |
| 701 |
4: import always_reachable |
| 702 |
|
| 703 |
[case testVersionInfoWithBoolAnd] |
| 704 |
# Combining version check with TYPE_CHECKING using and |
| 705 |
import sys |
| 706 |
from typing import TYPE_CHECKING |
| 707 |
if sys.version_info >= (3, 8) and TYPE_CHECKING: |
| 708 |
import mypy_only_import |
| 709 |
[out] |
| 710 |
2: import sys |
| 711 |
3: from typing import TYPE_CHECKING |
| 712 |
5: import mypy_only_import [mypy_only] |
| 713 |
|
| 714 |
[case testVersionInfoWithBoolOr] |
| 715 |
# Combining version check with PY2 using or - should be always_true |
| 716 |
import sys |
| 717 |
if sys.version_info >= (3, 8) or PY2: |
| 718 |
import always_reachable |
| 719 |
else: |
| 720 |
import unreachable_else |
| 721 |
[out] |
| 722 |
2: import sys |
| 723 |
4: import always_reachable |
| 724 |
|
| 725 |
[case testComplexBoolVersionMypyUnknown] |
| 726 |
# Complex: (MYPY or unknown) evaluates to MYPY_TRUE, and version_info >= (3,8) is ALWAYS_TRUE |
| 727 |
# ALWAYS_TRUE and MYPY_TRUE evaluates to MYPY_TRUE |
| 728 |
import sys |
| 729 |
if sys.version_info >= (3, 8) and (MYPY or unknown_var): |
| 730 |
import mypy_only_import |
| 731 |
[out] |
| 732 |
3: import sys |
| 733 |
5: import mypy_only_import [mypy_only] |