Skip to content

Commit f5bee93

Browse files
committed
add tests for bool expansion (no checker does this)
1 parent 02f0652 commit f5bee93

File tree

9 files changed

+81
-18
lines changed

9 files changed

+81
-18
lines changed

conformance/results/mypy/overloads_evaluation.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
conformant = "Partial"
22
notes = """
33
Does not pick a winning overload based on arity, prior to considering argument types.
4+
Does not expand boolean arguments to Literal[True] and Literal[False].
45
"""
56
conformance_automated = "Fail"
67
errors_diff = """
78
Line 35: Unexpected errors ['overloads_evaluation.py:35: error: Expression is of type "Any", not "int" [assert-type]']
89
Line 41: Unexpected errors ['overloads_evaluation.py:41: error: Expression is of type "Any", not "str" [assert-type]']
10+
Line 102: Unexpected errors ['overloads_evaluation.py:102: error: No overload variant of "expand_bool" matches argument type "bool" [call-overload]']
11+
Line 103: Unexpected errors ['overloads_evaluation.py:103: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]']
912
"""
1013
output = """
1114
overloads_evaluation.py:26: error: All overload variants of "example1" require at least one argument [call-overload]
@@ -24,4 +27,9 @@ overloads_evaluation.py:40: note: def example1(x: str) -> str
2427
overloads_evaluation.py:41: error: Expression is of type "Any", not "str" [assert-type]
2528
overloads_evaluation.py:85: error: Argument 1 to "example2" has incompatible type "int | str"; expected "int" [arg-type]
2629
overloads_evaluation.py:85: error: Argument 2 to "example2" has incompatible type "int | str"; expected "str" [arg-type]
30+
overloads_evaluation.py:102: error: No overload variant of "expand_bool" matches argument type "bool" [call-overload]
31+
overloads_evaluation.py:102: note: Possible overload variants:
32+
overloads_evaluation.py:102: note: def expand_bool(x: Literal[False]) -> Literal[0]
33+
overloads_evaluation.py:102: note: def expand_bool(x: Literal[True]) -> Literal[1]
34+
overloads_evaluation.py:103: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]
2735
"""
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
conformant = "Pass"
2-
conformance_automated = "Pass"
1+
conformant = "Partial"
2+
notes = """
3+
Does not expand boolean arguments to Literal[True] and Literal[False].
4+
"""
5+
conformance_automated = "Fail"
36
errors_diff = """
7+
Line 85: Expected 1 errors
8+
Line 102: Unexpected errors ['overloads_evaluation.py:102:23 Incompatible parameter type [6]: In call `expand_bool`, for 1st positional argument, expected `typing_extensions.Literal[False]` but got `bool`.']
9+
Line 103: Unexpected errors ['overloads_evaluation.py:103:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.']
410
"""
511
output = """
612
overloads_evaluation.py:26:0 Missing argument [20]: Call `example1` expects argument `x`.
713
overloads_evaluation.py:34:19 Incompatible parameter type [6]: In call `example1`, for 2nd positional argument, expected `str` but got `int`.
814
overloads_evaluation.py:40:16 Incompatible parameter type [6]: In call `example1`, for 1st positional argument, expected `str` but got `int`.
9-
overloads_evaluation.py:85:13 Incompatible parameter type [6]: In call `example2`, for 1st positional argument, expected `int` but got `Union[int, str]`.
10-
overloads_evaluation.py:85:16 Incompatible parameter type [6]: In call `example2`, for 2nd positional argument, expected `str` but got `Union[int, str]`.
15+
overloads_evaluation.py:102:23 Incompatible parameter type [6]: In call `expand_bool`, for 1st positional argument, expected `typing_extensions.Literal[False]` but got `bool`.
16+
overloads_evaluation.py:103:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.
1117
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pyre 0.9.23"
2-
test_duration = 6.1
2+
test_duration = 6.7

conformance/results/pyright/overloads_evaluation.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
conformant = "Pass"
2-
conformance_automated = "Pass"
1+
conformant = "Partial"
2+
notes = """
3+
Does not expand boolean arguments to Literal[True] and Literal[False].
4+
"""
5+
conformance_automated = "Fail"
36
errors_diff = """
7+
Line 102: Unexpected errors ['overloads_evaluation.py:102:12 - error: No overloads for "expand_bool" match the provided arguments (reportCallIssue)', 'overloads_evaluation.py:102:24 - error: Argument of type "bool" cannot be assigned to parameter "x" of type "Literal[True]" in function "expand_bool"']
8+
Line 103: Unexpected errors ['overloads_evaluation.py:103:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)']
49
"""
510
output = """
611
overloads_evaluation.py:26:1 - error: No overloads for "example1" match the provided arguments
@@ -16,4 +21,8 @@ overloads_evaluation.py:85:14 - error: Argument of type "int | str" cannot be as
1621
overloads_evaluation.py:85:17 - error: Argument of type "int | str" cannot be assigned to parameter "y" of type "int" in function "example2"
1722
  Type "int | str" is not assignable to type "int"
1823
    "str" is not assignable to "int" (reportArgumentType)
24+
overloads_evaluation.py:102:12 - error: No overloads for "expand_bool" match the provided arguments (reportCallIssue)
25+
overloads_evaluation.py:102:24 - error: Argument of type "bool" cannot be assigned to parameter "x" of type "Literal[True]" in function "expand_bool"
26+
  "bool" is not assignable to type "Literal[True]" (reportArgumentType)
27+
overloads_evaluation.py:103:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)
1928
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pyright 1.1.391"
2-
test_duration = 1.6
2+
test_duration = 1.5

conformance/results/pytype/overloads_evaluation.toml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
conformant = "Partial"
22
notes = """
33
Does not pick a winning overload based on arity, prior to considering argument types.
4-
Does not perform argument expansion on unions and union return types of all matching overloads.
4+
Does not perform argument expansion and union return types of all matching overloads.
55
"""
66
conformance_automated = "Fail"
77
errors_diff = """
@@ -11,6 +11,9 @@ Line 35: Unexpected errors ['overloads_evaluation.py:35:1: \\x1b[1m\\x1b[31merro
1111
Line 41: Unexpected errors ['overloads_evaluation.py:41:1: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in <module>: Any [assert-type]']
1212
Line 59: Unexpected errors ['overloads_evaluation.py:59:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in example2: bad return type [bad-return-type]']
1313
Line 78: Unexpected errors ['overloads_evaluation.py:78:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in _: str [assert-type]']
14+
Line 99: Unexpected errors ['overloads_evaluation.py:99:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in expand_bool: bad return type [bad-return-type]', 'overloads_evaluation.py:99:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in expand_bool: bad return type [bad-return-type]']
15+
Line 102: Unexpected errors ['overloads_evaluation.py:102:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in _: Function expand_bool was called with the wrong arguments [wrong-arg-types]']
16+
Line 103: Unexpected errors ['overloads_evaluation.py:103:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in _: Any [assert-type]']
1417
"""
1518
output = """
1619
overloads_evaluation.py:22:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in example1: bad return type [bad-return-type]
@@ -55,4 +58,24 @@ overloads_evaluation.py:78:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in _:
5558
assert_type(ret1, int | str)
5659
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
5760
61+
overloads_evaluation.py:99:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_bool: bad return type [bad-return-type]
62+
63+
return int(x)
64+
\u001b[1m\u001b[31m~~~~~~~~~~~~~\u001b[39m\u001b[0m
65+
66+
overloads_evaluation.py:99:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_bool: bad return type [bad-return-type]
67+
68+
return int(x)
69+
\u001b[1m\u001b[31m~~~~~~~~~~~~~\u001b[39m\u001b[0m
70+
71+
overloads_evaluation.py:102:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in _: Function expand_bool was called with the wrong arguments [wrong-arg-types]
72+
73+
ret1 = expand_bool(v)
74+
\u001b[1m\u001b[31m~~~~~~~~~~~~~~\u001b[39m\u001b[0m
75+
76+
overloads_evaluation.py:103:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in _: Any [assert-type]
77+
78+
assert_type(ret1, Literal[0, 1])
79+
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
80+
5881
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pytype 2024.10.11"
2-
test_duration = 31.5
2+
test_duration = 31.2

conformance/results/results.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ <h3>Python Type System Conformance Test Results</h3>
162162
<div class='tc-time'>1.7sec</div>
163163
</th>
164164
<th class='tc-header'><div class='tc-name'>pyright 1.1.391</div>
165-
<div class='tc-time'>1.6sec</div>
165+
<div class='tc-time'>1.5sec</div>
166166
</th>
167167
<th class='tc-header'><div class='tc-name'>pyre 0.9.23</div>
168-
<div class='tc-time'>6.1sec</div>
168+
<div class='tc-time'>6.7sec</div>
169169
</th>
170170
<th class='tc-header'><div class='tc-name'>pytype 2024.10.11</div>
171-
<div class='tc-time'>31.5sec</div>
171+
<div class='tc-time'>31.2sec</div>
172172
</th>
173173
</tr>
174174
<tr><th class="column" colspan="5">
@@ -691,10 +691,10 @@ <h3>Python Type System Conformance Test Results</h3>
691691
<th class="column col2 not-conformant"><div class="hover-text">Fail<span class="tooltip-text" id="bottom"><p>Does not enforce any of the specified rules regarding overload definitions.</p></span></div></th>
692692
</tr>
693693
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overloads_evaluation</th>
694-
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not pick a winning overload based on arity, prior to considering argument types.</p></span></div></th>
695-
<th class="column col2 conformant">Pass</th>
696-
<th class="column col2 conformant">Pass</th>
697-
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not pick a winning overload based on arity, prior to considering argument types.</p><p>Does not perform argument expansion on unions and union return types of all matching overloads.</p></span></div></th>
694+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not pick a winning overload based on arity, prior to considering argument types.</p><p>Does not expand boolean arguments to Literal[True] and Literal[False].</p></span></div></th>
695+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not expand boolean arguments to Literal[True] and Literal[False].</p></span></div></th>
696+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not expand boolean arguments to Literal[True] and Literal[False].</p></span></div></th>
697+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not pick a winning overload based on arity, prior to considering argument types.</p><p>Does not perform argument expansion and union return types of all matching overloads.</p></span></div></th>
698698
</tr>
699699
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overloads_overlap</th>
700700
<th class="column col2 conformant">Pass</th>

conformance/tests/overloads_evaluation.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Tests for evaluation of calls to overloaded functions.
33
"""
44

5-
from typing import assert_type, overload
5+
from typing import assert_type, Literal, overload
66

77

88
# > Step 1: Examine the argument list to determine the number of
@@ -84,3 +84,20 @@ def _(v: int | str) -> None:
8484
def _(v: int | str) -> None:
8585
example2(v, v, 1) # E: no overload matches (str, ..., ...)
8686

87+
88+
# > 2. ``bool`` should be expanded into ``Literal[True]`` and ``Literal[False]``.
89+
90+
@overload
91+
def expand_bool(x: Literal[False]) -> Literal[0]:
92+
...
93+
94+
@overload
95+
def expand_bool(x: Literal[True]) -> Literal[1]:
96+
...
97+
98+
def expand_bool(x: bool) -> int:
99+
return int(x)
100+
101+
def _(v: bool) -> None:
102+
ret1 = expand_bool(v)
103+
assert_type(ret1, Literal[0, 1])

0 commit comments

Comments
 (0)