Skip to content

Commit 54b4d7b

Browse files
committed
Add additional version or platform conformance tests.
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
1 parent b8ff527 commit 54b4d7b

5 files changed

Lines changed: 520 additions & 2 deletions
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
"""
2+
Tests implementation-name equality and membership checks.
3+
4+
These tests use the suite's Python 3.12 target and default CPython implementation.
5+
"""
6+
7+
# Specification: https://typing.python.org/en/latest/spec/directives.html#version-and-platform-checks
8+
9+
import sys
10+
from typing import assert_type
11+
12+
13+
def test_name_equality(a: int, b: str) -> None:
14+
# > Default value: ``"cpython"``, unless configured otherwise.
15+
error: int = 0
16+
equal: int | str
17+
if sys.implementation.name == "cpython":
18+
error = "reachable" # E
19+
equal = a
20+
else:
21+
error = "unreachable"
22+
equal = b
23+
assert_type(equal, int)
24+
25+
unequal: int | str
26+
if sys.implementation.name != "cpython":
27+
error = "unreachable"
28+
unequal = b
29+
else:
30+
error = "reachable" # E
31+
unequal = a
32+
assert_type(unequal, int)
33+
34+
other: int | str
35+
if sys.implementation.name == "micropython":
36+
error = "unreachable"
37+
other = b
38+
elif sys.implementation.name != "micropython":
39+
error = "reachable" # E
40+
other = a
41+
else:
42+
error = "unreachable"
43+
other = b
44+
assert_type(other, int)
45+
46+
47+
def test_name_tuple_membership(a: int, b: str) -> None:
48+
error: int = 0
49+
contained: int | str
50+
if sys.implementation.name in ("cpython", "pypy"):
51+
error = "reachable" # E
52+
contained = a
53+
else:
54+
error = "unreachable"
55+
contained = b
56+
assert_type(contained, int)
57+
58+
excluded: int | str
59+
if sys.implementation.name not in ("cpython",):
60+
error = "unreachable"
61+
excluded = b
62+
else:
63+
error = "reachable" # E
64+
excluded = a
65+
assert_type(excluded, int)
66+
67+
if sys.implementation.name in ("micropython",):
68+
error = "unreachable"
69+
else:
70+
error = "reachable" # E
71+
if sys.implementation.name not in ("micropython", "pypy"):
72+
error = "reachable" # E
73+
else:
74+
error = "unreachable"
75+
76+
77+
def test_name_set_membership(a: int, b: str) -> None:
78+
error: int = 0
79+
contained: int | str
80+
if sys.implementation.name in {"cpython", "pypy"}:
81+
error = "reachable" # E
82+
contained = a
83+
else:
84+
error = "unreachable"
85+
contained = b
86+
assert_type(contained, int)
87+
88+
excluded: int | str
89+
if sys.implementation.name not in {"cpython"}:
90+
error = "unreachable"
91+
excluded = b
92+
else:
93+
error = "reachable" # E
94+
excluded = a
95+
assert_type(excluded, int)
96+
97+
if sys.implementation.name in {"micropython"}:
98+
error = "unreachable"
99+
else:
100+
error = "reachable" # E
101+
if sys.implementation.name not in {"micropython", "pypy"}:
102+
error = "reachable" # E
103+
else:
104+
error = "unreachable"
105+
106+
107+
def test_name_list_membership(a: int, b: str) -> None:
108+
error: int = 0
109+
contained: int | str
110+
if sys.implementation.name in ["cpython", "pypy"]:
111+
error = "reachable" # E
112+
contained = a
113+
else:
114+
error = "unreachable"
115+
contained = b
116+
assert_type(contained, int)
117+
118+
excluded: int | str
119+
if sys.implementation.name not in ["cpython"]:
120+
error = "unreachable"
121+
excluded = b
122+
else:
123+
error = "reachable" # E
124+
excluded = a
125+
assert_type(excluded, int)
126+
127+
if sys.implementation.name in ["micropython"]:
128+
error = "unreachable"
129+
else:
130+
error = "reachable" # E
131+
if sys.implementation.name not in ["micropython", "pypy"]:
132+
error = "reachable" # E
133+
else:
134+
error = "unreachable"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Tests the CPython implementation-version default.
3+
4+
These tests use the suite's Python 3.12 target and default CPython implementation.
5+
"""
6+
7+
# Specification: https://typing.python.org/en/latest/spec/directives.html#version-and-platform-checks
8+
9+
import sys
10+
from typing import assert_type
11+
12+
13+
def test_implementation_version(a: int, b: str) -> None:
14+
# For the default CPython target, use the configured language version.
15+
error: int = 0
16+
at_boundary: int | str
17+
if sys.implementation.version >= (3, 12):
18+
error = "reachable" # E
19+
at_boundary = a
20+
else:
21+
error = "unreachable"
22+
at_boundary = b
23+
assert_type(at_boundary, int)
24+
25+
below_boundary: int | str
26+
if sys.implementation.version < (3, 12):
27+
error = "unreachable"
28+
below_boundary = b
29+
else:
30+
error = "reachable" # E
31+
below_boundary = a
32+
assert_type(below_boundary, int)
33+
34+
if sys.implementation.version < (3, 100):
35+
error = "reachable" # E
36+
else:
37+
error = "unreachable"
38+
if sys.implementation.version >= (3, 100):
39+
error = "unreachable"
40+
else:
41+
error = "reachable" # E
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""
2+
Tests platform comparisons, literal containment, and prefix checks.
3+
"""
4+
5+
# Specification: https://typing.python.org/en/latest/spec/directives.html#version-and-platform-checks
6+
7+
import sys
8+
from typing import assert_type
9+
10+
11+
def test_equality(a: int, b: str) -> None:
12+
error: int = 0
13+
equal: int | str
14+
if sys.platform == "bogus_platform":
15+
error = "unreachable"
16+
equal = b
17+
else:
18+
error = "reachable" # E
19+
equal = a
20+
assert_type(equal, int)
21+
22+
unequal: int | str
23+
if sys.platform != "bogus_platform":
24+
error = "reachable" # E
25+
unequal = a
26+
else:
27+
error = "unreachable"
28+
unequal = b
29+
assert_type(unequal, int)
30+
31+
32+
def test_startswith(a: int, b: str) -> None:
33+
error: int = 0
34+
empty_prefix: int | str
35+
if sys.platform.startswith(""):
36+
error = "reachable" # E: Every platform starts with the empty prefix.
37+
empty_prefix = a
38+
else:
39+
error = "unreachable"
40+
empty_prefix = b
41+
assert_type(empty_prefix, int)
42+
43+
unknown_prefix: int | str
44+
if sys.platform.startswith("bogus_platform"):
45+
error = "unreachable"
46+
unknown_prefix = b
47+
else:
48+
error = "reachable" # E
49+
unknown_prefix = a
50+
assert_type(unknown_prefix, int)
51+
52+
53+
def test_versioned_platform_prefix() -> None:
54+
error: int = 0
55+
if sys.platform == "freebsd13":
56+
if sys.platform.startswith("freebsd"):
57+
error = "reachable" # E[freebsd_match]
58+
else:
59+
error = "unreachable"
60+
if sys.platform.startswith("linux"):
61+
error = "unreachable"
62+
else:
63+
error = "reachable" # E[freebsd_nonmatch]
64+
else:
65+
error = "reachable" # E[freebsd_match]
66+
error = "reachable" # E[freebsd_nonmatch]
67+
68+
69+
def test_tuple_membership(a: int, b: str) -> None:
70+
# > The membership checks ``in`` and ``not in`` only support simple containment
71+
# > testing with a literal tuple, literal set, or literal list of literal strings.
72+
error: int = 0
73+
contained: int | str
74+
if sys.platform in ("bogus_platform",):
75+
error = "unreachable"
76+
contained = b
77+
else:
78+
error = "reachable" # E
79+
contained = a
80+
assert_type(contained, int)
81+
82+
excluded: int | str
83+
if sys.platform not in ("bogus_platform", "another_bogus_platform"):
84+
error = "reachable" # E
85+
excluded = a
86+
else:
87+
error = "unreachable"
88+
excluded = b
89+
assert_type(excluded, int)
90+
91+
# Positive matches are guarded so this file also works on non-Linux targets.
92+
if sys.platform == "linux":
93+
if sys.platform in ("linux", "darwin"):
94+
error = "reachable" # E[tuple_match]
95+
else:
96+
error = "unreachable"
97+
if sys.platform not in ("linux",):
98+
error = "unreachable"
99+
else:
100+
error = "reachable" # E[tuple_nonmatch]
101+
else:
102+
error = "reachable" # E[tuple_match]
103+
error = "reachable" # E[tuple_nonmatch]
104+
105+
106+
def test_set_membership(a: int, b: str) -> None:
107+
error: int = 0
108+
contained: int | str
109+
if sys.platform in {"bogus_platform"}:
110+
error = "unreachable"
111+
contained = b
112+
else:
113+
error = "reachable" # E
114+
contained = a
115+
assert_type(contained, int)
116+
117+
excluded: int | str
118+
if sys.platform not in {"bogus_platform", "another_bogus_platform"}:
119+
error = "reachable" # E
120+
excluded = a
121+
else:
122+
error = "unreachable"
123+
excluded = b
124+
assert_type(excluded, int)
125+
126+
if sys.platform == "linux":
127+
if sys.platform in {"linux", "darwin"}:
128+
error = "reachable" # E[set_match]
129+
else:
130+
error = "unreachable"
131+
if sys.platform not in {"linux"}:
132+
error = "unreachable"
133+
else:
134+
error = "reachable" # E[set_nonmatch]
135+
else:
136+
error = "reachable" # E[set_match]
137+
error = "reachable" # E[set_nonmatch]
138+
139+
140+
def test_list_membership(a: int, b: str) -> None:
141+
error: int = 0
142+
contained: int | str
143+
if sys.platform in ["bogus_platform"]:
144+
error = "unreachable"
145+
contained = b
146+
else:
147+
error = "reachable" # E
148+
contained = a
149+
assert_type(contained, int)
150+
151+
excluded: int | str
152+
if sys.platform not in ["bogus_platform", "another_bogus_platform"]:
153+
error = "reachable" # E
154+
excluded = a
155+
else:
156+
error = "unreachable"
157+
excluded = b
158+
assert_type(excluded, int)
159+
160+
if sys.platform == "linux":
161+
if sys.platform in ["linux", "darwin"]:
162+
error = "reachable" # E[list_match]
163+
else:
164+
error = "unreachable"
165+
if sys.platform not in ["linux"]:
166+
error = "unreachable"
167+
else:
168+
error = "reachable" # E[list_nonmatch]
169+
else:
170+
error = "reachable" # E[list_match]
171+
error = "reachable" # E[list_nonmatch]

0 commit comments

Comments
 (0)