← Back to index

directives_version_platform.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 3
FP: 0
FN: 0
Optional: 2 / 6
1"""
2Tests checking the version or platform.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/directives.html#version-and-platform-checking
6
7import os
8import sys
9from typing import assert_type
11def test(a: int, b: str):
12 val1: int | str
13 if sys.version_info >= (3, 8):
14 val1 = a
15 else:
16 val1 = b
18 assert_type(val1, int)
20 val2: int | str
21 if sys.version_info >= (3, 8, 0):
22 val2 = a
23 else:
24 val2 = b
26 assert_type(val2, int) # E?: May not generate an error (support for three-element sys.version is optional)
28 if sys.version_info < (3, 8):
29 val3 = ""
30 else:
31 val4 = ""
33 this_raises = val3 # E: `val3` is undefined
[unresolved-reference] Name `val3` used when not defined
34 does_not_raise = val4 # should not error
36 val5: int | str
37 if sys.version_info < (3, 100, 0):
38 val5 = a
39 else:
40 val5 = b
42 assert_type(val5, int) # E?: May not generate an error (support for three-element sys.version is optional)
45 if sys.platform == "bogus_platform":
46 val6 = ""
47 else:
48 val7 = ""
50 this_raises = val6 # E: `val6` is undefined
[unresolved-reference] Name `val6` used when not defined
51 does_not_raise = val7 # should not error
53 if sys.platform != "bogus_platform":
54 val8 = ""
55 else:
56 val9 = ""
58 does_not_raise = val8 # should not error
59 this_raises = val9 # E: `val9` is undefined
[unresolved-reference] Name `val9` used when not defined
61 if os.name == "bogus_os":
62 val10 = ""
63 else:
64 val11 = ""
66 this_raises = val10 # E?: `val10` is undefined, but support for `os.name` is optional
[unresolved-reference] Name `val10` used when not defined
67 does_not_raise = val11 # E? should not error if `os.name` control flow is supported, but might be flagged as possibly undefined otherwise
69 if os.name != "bogus_os":
70 val12 = ""
71 else:
72 val13 = ""
74 does_not_raise = val12 # E?: should not error if `os.name` control flow is supported, but might be flagged as possibly undefined otherwise
75 this_raises = val13 # E?: `val13` is undefined, but support for `os.name` is optional
[unresolved-reference] Name `val13` used when not defined