Skip to content

Commit

Permalink
Guard against unreachability
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed May 23, 2024
1 parent 6155dbd commit a442bfe
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 17 deletions.
10 changes: 7 additions & 3 deletions conformance/results/mypy/specialtypes_promotions.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
conformant = "Pass"
conformant = "Partial"
notes = """
Does not narrow from float to int after isinstance() check
"""
output = """
specialtypes_promotions.py:17: error: "float" has no attribute "numerator" [attr-defined]
specialtypes_promotions.py:29: error: Incompatible return value type (got "complex", expected "float") [return-value]
specialtypes_promotions.py:33: error: Incompatible return value type (got "complex", expected "float") [return-value]
"""
conformance_automated = "Pass"
conformance_automated = "Fail"
errors_diff = """
Line 26: Expected 1 errors
"""
2 changes: 1 addition & 1 deletion conformance/results/mypy/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "mypy 1.10.0"
test_duration = 6.2
test_duration = 1.5
4 changes: 3 additions & 1 deletion conformance/results/pyre/specialtypes_promotions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
conformant = "Partial"
notes = """
Does not reject use of attribute that is compatible only with float.
Does not narrow from float to int after isinstance() check
"""
output = """
specialtypes_promotions.py:29:8 Incompatible return type [7]: Expected `float` but got `complex`.
specialtypes_promotions.py:33:8 Incompatible return type [7]: Expected `float` but got `complex`.
"""
conformance_automated = "Fail"
errors_diff = """
Line 17: Expected 1 errors
Line 26: Expected 1 errors
"""
2 changes: 1 addition & 1 deletion conformance/results/pyre/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyre 0.9.21"
test_duration = 2.6
test_duration = 2.9
4 changes: 3 additions & 1 deletion conformance/results/pyright/specialtypes_promotions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ conformant = "Pass"
output = """
specialtypes_promotions.py:17:7 - error: Cannot access attribute "numerator" for class "float"
  Attribute "numerator" is unknown (reportAttributeAccessIssue)
specialtypes_promotions.py:29:16 - error: Expression of type "complex" is incompatible with return type "float"
specialtypes_promotions.py:26:16 - error: Expression of type "Literal['x']" is incompatible with return type "int"
  "Literal['x']" is incompatible with "int" (reportReturnType)
specialtypes_promotions.py:33:16 - error: Expression of type "complex" is incompatible with return type "float"
  "complex" is incompatible with "float" (reportReturnType)
"""
conformance_automated = "Pass"
Expand Down
2 changes: 1 addition & 1 deletion conformance/results/pyright/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyright 1.1.364"
test_duration = 1.8
test_duration = 1.6
10 changes: 7 additions & 3 deletions conformance/results/pytype/specialtypes_promotions.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
conformant = "Pass"
conformant = "Partial"
notes = """
Does not narrow from float to int after isinstance() check
"""
output = """
File "specialtypes_promotions.py", line 17, in func1: No attribute 'numerator' on float [attribute-error]
File "specialtypes_promotions.py", line 29, in func2: bad return type [bad-return-type]
File "specialtypes_promotions.py", line 33, in func2: bad return type [bad-return-type]
"""
conformance_automated = "Pass"
conformance_automated = "Fail"
errors_diff = """
Line 26: Expected 1 errors
"""
2 changes: 1 addition & 1 deletion conformance/results/pytype/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pytype 2024.04.11"
test_duration = 54.5
test_duration = 49.8
8 changes: 4 additions & 4 deletions conformance/results/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ <h3>Python Type System Conformance Test Results</h3>
<div class="table_container"><table><tbody>
<tr><th class="col1">&nbsp;</th>
<th class='tc-header'><div class='tc-name'>mypy 1.10.0</div>
<div class='tc-time'>6.2sec</div>
<div class='tc-time'>1.5sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyright 1.1.364</div>
<div class='tc-time'>1.8sec</div>
<div class='tc-time'>1.6sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyre 0.9.21</div>
<div class='tc-time'>2.6sec</div>
<div class='tc-time'>2.9sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pytype 2024.04.11</div>
<div class='tc-time'>54.5sec</div>
<div class='tc-time'>49.8sec</div>
</th>
</tr>
<tr><th class="column" colspan="5">
Expand Down
6 changes: 5 additions & 1 deletion conformance/tests/specialtypes_promotions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
v4 = 1


def func1(f: float):
def func1(f: float) -> int:
f.numerator # E: attribute exists on int but not float

if isinstance(f, float):
f.hex() # OK (attribute exists on float but not int)
return 1
else:
assert_type(f, int)
# Make sure type checkers don't treat this branch as unreachable
# and skip checking it.
return "x" # E


def func2(x: int) -> float:
Expand Down

0 comments on commit a442bfe

Please sign in to comment.