Skip to content

Commit 15d40a6

Browse files
committed
Conformance suite: rewrite directives_version_platform.py to not assume that type checkers will ignore all errors in unreachable code
1 parent 6b1a64c commit 15d40a6

File tree

5 files changed

+78
-36
lines changed

5 files changed

+78
-36
lines changed

conformance/results/mypy/directives_version_platform.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ Does not understand three-element form of sys.version checks.
44
Does not understand os.name checks.
55
"""
66
output = """
7-
directives_version_platform.py:19: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
8-
directives_version_platform.py:27: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
9-
directives_version_platform.py:40: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
10-
directives_version_platform.py:45: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
7+
directives_version_platform.py:26: error: Expression is of type "int | str", not "int" [assert-type]
8+
directives_version_platform.py:33: error: Name "val3" is not defined [name-defined]
9+
directives_version_platform.py:42: error: Expression is of type "int | str", not "int" [assert-type]
10+
directives_version_platform.py:50: error: Name "val6" is not defined [name-defined]
11+
directives_version_platform.py:59: error: Name "val9" is not defined [name-defined]
1112
"""
1213
conformance_automated = "Pass"
1314
errors_diff = """

conformance/results/pyrefly/directives_version_platform.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ conformance_automated = "Pass"
33
errors_diff = """
44
"""
55
output = """
6-
ERROR directives_version_platform.py:40:17-19: `Literal['']` is not assignable to `int` [bad-assignment]
7-
ERROR directives_version_platform.py:45:17-19: `Literal['']` is not assignable to `int` [bad-assignment]
6+
ERROR directives_version_platform.py:33:19-23: Could not find name `val3` [unknown-name]
7+
ERROR directives_version_platform.py:50:19-23: Could not find name `val6` [unknown-name]
8+
ERROR directives_version_platform.py:59:19-23: Could not find name `val9` [unknown-name]
9+
ERROR directives_version_platform.py:66:19-24: `val10` may be uninitialized [unbound-name]
10+
ERROR directives_version_platform.py:67:22-27: `val11` may be uninitialized [unbound-name]
11+
ERROR directives_version_platform.py:74:22-27: `val12` may be uninitialized [unbound-name]
12+
ERROR directives_version_platform.py:75:19-24: `val13` may be uninitialized [unbound-name]
813
"""

conformance/results/pyright/directives_version_platform.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
conformant = "Pass"
22
output = """
3+
directives_version_platform.py:33:19 - error: "val3" is not defined (reportUndefinedVariable)
4+
directives_version_platform.py:50:19 - error: "val6" is not defined (reportUndefinedVariable)
5+
directives_version_platform.py:59:19 - error: "val9" is not defined (reportUndefinedVariable)
6+
directives_version_platform.py:66:19 - error: "val10" is not defined (reportUndefinedVariable)
7+
directives_version_platform.py:75:19 - error: "val13" is not defined (reportUndefinedVariable)
38
"""
49
conformance_automated = "Pass"
510
errors_diff = """

conformance/results/zuban/directives_version_platform.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ conformance_automated = "Pass"
22
errors_diff = """
33
"""
44
output = """
5-
directives_version_platform.py:19: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
6-
directives_version_platform.py:27: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
7-
directives_version_platform.py:40: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
8-
directives_version_platform.py:45: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
5+
directives_version_platform.py:26: error: Expression is of type "int | str", not "int" [misc]
6+
directives_version_platform.py:33: error: Name "val3" is not defined [name-defined]
7+
directives_version_platform.py:42: error: Expression is of type "int | str", not "int" [misc]
8+
directives_version_platform.py:50: error: Name "val6" is not defined [name-defined]
9+
directives_version_platform.py:59: error: Name "val9" is not defined [name-defined]
910
"""

conformance/tests/directives_version_platform.py

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,70 @@
66

77
import os
88
import sys
9+
from typing import assert_type
910

11+
def test(a: int, b: str):
12+
val1: int | str
13+
if sys.version_info >= (3, 8):
14+
val1 = a
15+
else:
16+
val1 = b
1017

11-
if sys.version_info >= (3, 8):
12-
pass
13-
else:
14-
val1: int = "" # Should not generate an error
18+
assert_type(val1, int)
1519

16-
if sys.version_info >= (3, 8, 0):
17-
pass
18-
else:
19-
val2: int = "" # E?: May not generate an error (support for three-element sys.version is optional)
20+
val2: int | str
21+
if sys.version_info >= (3, 8, 0):
22+
val2 = a
23+
else:
24+
val2 = b
2025

21-
if sys.version_info < (3, 8):
22-
val3: int = "" # Should not generate an error
26+
assert_type(val2, int) # E?: May not generate an error (support for three-element sys.version is optional)
2327

24-
if sys.version_info < (3, 100, 0):
25-
pass
26-
else:
27-
val3: 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 = ""
2832

33+
this_raises = val3 # E: `val3` is undefined
34+
does_not_raise = val4 # should not error
2935

30-
if sys.platform == "bogus_platform":
31-
val5: int = "" # Should not generate an error
36+
val5: int | str
37+
if sys.version_info < (3, 100, 0):
38+
val5 = a
39+
else:
40+
val5 = b
3241

33-
if sys.platform != "bogus_platform":
34-
pass
35-
else:
36-
val6: int = "" # Should not generate an error
42+
assert_type(val5, int) # E?: May not generate an error (support for three-element sys.version is optional)
3743

3844

39-
if os.name == "bogus_os":
40-
val7: int = "" # E?: May not generate an error (support for os.name is optional)
45+
if sys.platform == "bogus_platform":
46+
val6 = ""
47+
else:
48+
val7 = ""
4149

42-
if os.name != "bogus_platform":
43-
pass
44-
else:
45-
val8: int = "" # E?: May not generate an error (support for os.name is optional)
50+
this_raises = val6 # E: `val6` is undefined
51+
does_not_raise = val7 # should not error
52+
53+
if sys.platform != "bogus_platform":
54+
val8 = ""
55+
else:
56+
val9 = ""
57+
58+
does_not_raise = val8 # should not error
59+
this_raises = val9 # E: `val9` is undefined
60+
61+
if os.name == "bogus_os":
62+
val10 = ""
63+
else:
64+
val11 = ""
65+
66+
this_raises = val10 # E?: `val10` is undefined, but support for `os.name` is optional
67+
does_not_raise = val11 # E? should not error if `os.name` control flow is supported, but might be flagged as possibly undefined otherwise
68+
69+
if os.name != "bogus_platform":
70+
val12 = ""
71+
else:
72+
val13 = ""
73+
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

0 commit comments

Comments
 (0)