Skip to content

Commit d2a3bdf

Browse files
committed
Proposed clarification of spec for int/float/complex promotion
1 parent 2e70529 commit d2a3bdf

File tree

11 files changed

+54
-20
lines changed

11 files changed

+54
-20
lines changed

conformance/results/mypy/specialtypes_promotions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
conformant = "Pass"
22
output = """
3-
specialtypes_promotions.py:13: error: "float" has no attribute "numerator" [attr-defined]
3+
specialtypes_promotions.py:15: error: "float" has no attribute "numerator" [attr-defined]
4+
specialtypes_promotions.py:27: error: Incompatible return value type (got "complex", expected "float") [return-value]
45
"""
56
conformance_automated = "Pass"
67
errors_diff = """
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "mypy 1.10.0"
2-
test_duration = 1.4
2+
test_duration = 1.2

conformance/results/pyre/specialtypes_promotions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ notes = """
33
Does not reject use of attribute that is compatible only with float.
44
"""
55
output = """
6+
specialtypes_promotions.py:27:8 Incompatible return type [7]: Expected `float` but got `complex`.
67
"""
78
conformance_automated = "Fail"
89
errors_diff = """
9-
Line 13: Expected 1 errors
10+
Line 15: Expected 1 errors
1011
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pyre 0.9.21"
2-
test_duration = 3.4
2+
test_duration = 1.7

conformance/results/pyright/specialtypes_promotions.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
conformant = "Pass"
22
output = """
3-
specialtypes_promotions.py:13:7 - error: Cannot access attribute "numerator" for class "float"
3+
specialtypes_promotions.py:15:7 - error: Cannot access attribute "numerator" for class "float"
44
  Attribute "numerator" is unknown (reportAttributeAccessIssue)
5+
specialtypes_promotions.py:27:16 - error: Expression of type "complex" is incompatible with return type "float"
6+
  "complex" is incompatible with "float" (reportReturnType)
57
"""
68
conformance_automated = "Pass"
79
errors_diff = """
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pyright 1.1.364"
2-
test_duration = 1.4
2+
test_duration = 1.3

conformance/results/pytype/specialtypes_promotions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
conformant = "Pass"
22
output = """
3-
File "specialtypes_promotions.py", line 13, in func1: No attribute 'numerator' on float [attribute-error]
3+
File "specialtypes_promotions.py", line 15, in func1: No attribute 'numerator' on float [attribute-error]
4+
File "specialtypes_promotions.py", line 27, in func2: bad return type [bad-return-type]
45
"""
56
conformance_automated = "Pass"
67
errors_diff = """
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pytype 2024.04.11"
2-
test_duration = 30.1
2+
test_duration = 27.6

conformance/results/results.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,16 @@ <h3>Python Type System Conformance Test Results</h3>
159159
<div class="table_container"><table><tbody>
160160
<tr><th class="col1">&nbsp;</th>
161161
<th class='tc-header'><div class='tc-name'>mypy 1.10.0</div>
162-
<div class='tc-time'>1.4sec</div>
162+
<div class='tc-time'>1.2sec</div>
163163
</th>
164164
<th class='tc-header'><div class='tc-name'>pyright 1.1.364</div>
165-
<div class='tc-time'>1.4sec</div>
165+
<div class='tc-time'>1.3sec</div>
166166
</th>
167167
<th class='tc-header'><div class='tc-name'>pyre 0.9.21</div>
168-
<div class='tc-time'>3.4sec</div>
168+
<div class='tc-time'>1.7sec</div>
169169
</th>
170170
<th class='tc-header'><div class='tc-name'>pytype 2024.04.11</div>
171-
<div class='tc-time'>30.1sec</div>
171+
<div class='tc-time'>27.6sec</div>
172172
</th>
173173
</tr>
174174
<tr><th class="column" colspan="5">

conformance/tests/specialtypes_promotions.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Tests "type promotions" for float and complex when they appear in annotations.
33
"""
44

5+
from typing import assert_type
6+
57
# Specification: https://typing.readthedocs.io/en/latest/spec/special-types.html#special-cases-for-float-and-complex
68

79
v1: float = 1
@@ -10,7 +12,20 @@
1012

1113

1214
def func1(f: float):
13-
f.numerator # E
15+
f.numerator # E: attribute exists on int but not float
16+
17+
if isinstance(f, float):
18+
assert_type(f, float)
19+
else:
20+
assert_type(f, int)
21+
1422

15-
if not isinstance(f, float):
16-
f.numerator # OK
23+
def func2(x: int) -> float:
24+
if x == 0:
25+
return 1
26+
elif x == 1:
27+
return 1j # E
28+
elif x > 10:
29+
return x
30+
else:
31+
return 1.0

0 commit comments

Comments
 (0)