Skip to content

Commit c041484

Browse files
committed
add test for type[A | B] expansion
1 parent 169fa58 commit c041484

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

conformance/results/mypy/overloads_evaluation.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Line 103: Unexpected errors ['overloads_evaluation.py:103: error: No overload va
1313
Line 104: Unexpected errors ['overloads_evaluation.py:104: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]']
1414
Line 126: Unexpected errors ['overloads_evaluation.py:126: error: No overload variant of "expand_enum" matches argument type "Color" [call-overload]']
1515
Line 127: Unexpected errors ['overloads_evaluation.py:127: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]']
16-
Line 147: Unexpected errors ['overloads_evaluation.py:147: error: Argument 1 to "expand_tuple" has incompatible type "tuple[int, int | str]"; expected "tuple[int, int]" [arg-type]']
17-
Line 148: Unexpected errors ['overloads_evaluation.py:148: error: Expression is of type "int", not "int | str" [assert-type]']
16+
Line 166: Unexpected errors ['overloads_evaluation.py:166: error: Argument 1 to "expand_tuple" has incompatible type "tuple[int, int | str]"; expected "tuple[int, int]" [arg-type]']
17+
Line 167: Unexpected errors ['overloads_evaluation.py:167: error: Expression is of type "int", not "int | str" [assert-type]']
1818
"""
1919
output = """
2020
overloads_evaluation.py:27: error: All overload variants of "example1" require at least one argument [call-overload]
@@ -43,6 +43,6 @@ overloads_evaluation.py:126: note: Possible overload variants:
4343
overloads_evaluation.py:126: note: def expand_enum(x: Literal[Color.RED]) -> Literal[0]
4444
overloads_evaluation.py:126: note: def expand_enum(x: Literal[Color.BLUE]) -> Literal[1]
4545
overloads_evaluation.py:127: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]
46-
overloads_evaluation.py:147: error: Argument 1 to "expand_tuple" has incompatible type "tuple[int, int | str]"; expected "tuple[int, int]" [arg-type]
47-
overloads_evaluation.py:148: error: Expression is of type "int", not "int | str" [assert-type]
46+
overloads_evaluation.py:166: error: Argument 1 to "expand_tuple" has incompatible type "tuple[int, int | str]"; expected "tuple[int, int]" [arg-type]
47+
overloads_evaluation.py:167: error: Expression is of type "int", not "int | str" [assert-type]
4848
"""

conformance/results/pyre/overloads_evaluation.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ conformant = "Partial"
22
notes = """
33
Does not expand boolean arguments to Literal[True] and Literal[False].
44
Does not expand enum arguments to literal variants.
5+
Does not expand type[A | B] to type[A] and type[B].
56
Does not expand tuple arguments to possible combinations.
67
"""
78
conformance_automated = "Fail"
@@ -12,8 +13,10 @@ Line 103: Unexpected errors ['overloads_evaluation.py:103:23 Incompatible parame
1213
Line 104: Unexpected errors ['overloads_evaluation.py:104:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.']
1314
Line 126: Unexpected errors ['overloads_evaluation.py:126:23 Incompatible parameter type [6]: In call `expand_enum`, for 1st positional argument, expected `typing_extensions.Literal[Color.RED]` but got `Color`.']
1415
Line 127: Unexpected errors ['overloads_evaluation.py:127:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.']
15-
Line 147: Unexpected errors ['overloads_evaluation.py:147:24 Incompatible parameter type [6]: In call `expand_tuple`, for 1st positional argument, expected `Tuple[int, int]` but got `Tuple[int, Union[int, str]]`.']
16-
Line 148: Unexpected errors ['overloads_evaluation.py:148:4 Assert type [70]: Expected `Union[int, str]` but got `int`.']
16+
Line 144: Unexpected errors ['overloads_evaluation.py:144:29 Incompatible parameter type [6]: In call `expand_type_union`, for 1st positional argument, expected `Type[int]` but got `Type[Union[int, str]]`.']
17+
Line 145: Unexpected errors ['overloads_evaluation.py:145:4 Assert type [70]: Expected `Union[int, str]` but got `int`.']
18+
Line 166: Unexpected errors ['overloads_evaluation.py:166:24 Incompatible parameter type [6]: In call `expand_tuple`, for 1st positional argument, expected `Tuple[int, int]` but got `Tuple[int, Union[int, str]]`.']
19+
Line 167: Unexpected errors ['overloads_evaluation.py:167:4 Assert type [70]: Expected `Union[int, str]` but got `int`.']
1720
"""
1821
output = """
1922
overloads_evaluation.py:27:0 Missing argument [20]: Call `example1` expects argument `x`.
@@ -27,6 +30,8 @@ overloads_evaluation.py:103:23 Incompatible parameter type [6]: In call `expand_
2730
overloads_evaluation.py:104:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.
2831
overloads_evaluation.py:126:23 Incompatible parameter type [6]: In call `expand_enum`, for 1st positional argument, expected `typing_extensions.Literal[Color.RED]` but got `Color`.
2932
overloads_evaluation.py:127:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.
30-
overloads_evaluation.py:147:24 Incompatible parameter type [6]: In call `expand_tuple`, for 1st positional argument, expected `Tuple[int, int]` but got `Tuple[int, Union[int, str]]`.
31-
overloads_evaluation.py:148:4 Assert type [70]: Expected `Union[int, str]` but got `int`.
33+
overloads_evaluation.py:144:29 Incompatible parameter type [6]: In call `expand_type_union`, for 1st positional argument, expected `Type[int]` but got `Type[Union[int, str]]`.
34+
overloads_evaluation.py:145:4 Assert type [70]: Expected `Union[int, str]` but got `int`.
35+
overloads_evaluation.py:166:24 Incompatible parameter type [6]: In call `expand_tuple`, for 1st positional argument, expected `Tuple[int, int]` but got `Tuple[int, Union[int, str]]`.
36+
overloads_evaluation.py:167:4 Assert type [70]: Expected `Union[int, str]` but got `int`.
3237
"""
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 = 5.9

conformance/results/pyright/overloads_evaluation.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Line 103: Unexpected errors ['overloads_evaluation.py:103:12 - error: No overloa
1010
Line 104: Unexpected errors ['overloads_evaluation.py:104:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)']
1111
Line 126: Unexpected errors ['overloads_evaluation.py:126:12 - error: No overloads for "expand_enum" match the provided arguments (reportCallIssue)', 'overloads_evaluation.py:126:24 - error: Argument of type "Color" cannot be assigned to parameter "x" of type "Literal[Color.BLUE]" in function "expand_enum"']
1212
Line 127: Unexpected errors ['overloads_evaluation.py:127:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)']
13-
Line 147: Unexpected errors ['overloads_evaluation.py:147:12 - error: No overloads for "expand_tuple" match the provided arguments (reportCallIssue)', 'overloads_evaluation.py:147:29 - error: Argument of type "tuple[Literal[1], int | str]" cannot be assigned to parameter "x" of type "tuple[int, str]" in function "expand_tuple"']
14-
Line 148: Unexpected errors ['overloads_evaluation.py:148:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)']
13+
Line 166: Unexpected errors ['overloads_evaluation.py:166:12 - error: No overloads for "expand_tuple" match the provided arguments (reportCallIssue)', 'overloads_evaluation.py:166:29 - error: Argument of type "tuple[Literal[1], int | str]" cannot be assigned to parameter "x" of type "tuple[int, str]" in function "expand_tuple"']
14+
Line 167: Unexpected errors ['overloads_evaluation.py:167:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)']
1515
"""
1616
output = """
1717
overloads_evaluation.py:27:1 - error: No overloads for "example1" match the provided arguments
@@ -35,9 +35,9 @@ overloads_evaluation.py:126:12 - error: No overloads for "expand_enum" match the
3535
overloads_evaluation.py:126:24 - error: Argument of type "Color" cannot be assigned to parameter "x" of type "Literal[Color.BLUE]" in function "expand_enum"
3636
  "Color" is not assignable to type "Literal[Color.BLUE]" (reportArgumentType)
3737
overloads_evaluation.py:127:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)
38-
overloads_evaluation.py:147:12 - error: No overloads for "expand_tuple" match the provided arguments (reportCallIssue)
39-
overloads_evaluation.py:147:29 - error: Argument of type "tuple[Literal[1], int | str]" cannot be assigned to parameter "x" of type "tuple[int, str]" in function "expand_tuple"
38+
overloads_evaluation.py:166:12 - error: No overloads for "expand_tuple" match the provided arguments (reportCallIssue)
39+
overloads_evaluation.py:166:29 - error: Argument of type "tuple[Literal[1], int | str]" cannot be assigned to parameter "x" of type "tuple[int, str]" in function "expand_tuple"
4040
  Type "int | str" is not assignable to type "str"
4141
    "int" is not assignable to "str" (reportArgumentType)
42-
overloads_evaluation.py:148:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)
42+
overloads_evaluation.py:167:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)
4343
"""

conformance/results/pytype/overloads_evaluation.toml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ Line 104: Unexpected errors ['overloads_evaluation.py:104:5: \\x1b[1m\\x1b[31mer
1717
Line 123: Unexpected errors ['overloads_evaluation.py:123:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in expand_enum: bad return type [bad-return-type]']
1818
Line 126: Unexpected errors ['overloads_evaluation.py:126:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_enum: Function expand_enum was called with the wrong arguments [wrong-arg-types]']
1919
Line 127: Unexpected errors ['overloads_evaluation.py:127:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_enum: Any [assert-type]']
20-
Line 144: Unexpected errors ['overloads_evaluation.py:144:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in expand_tuple: bad return type [bad-return-type]']
21-
Line 148: Unexpected errors ['overloads_evaluation.py:148:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_tuple: int [assert-type]']
20+
Line 141: Unexpected errors ['overloads_evaluation.py:141:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in expand_type_union: bad return type [bad-return-type]']
21+
Line 145: Unexpected errors ['overloads_evaluation.py:145:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_type_union: int [assert-type]']
22+
Line 163: Unexpected errors ['overloads_evaluation.py:163:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in expand_tuple: bad return type [bad-return-type]']
23+
Line 167: Unexpected errors ['overloads_evaluation.py:167:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_tuple: int [assert-type]']
2224
"""
2325
output = """
2426
overloads_evaluation.py:23:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in example1: bad return type [bad-return-type]
@@ -98,12 +100,22 @@ overloads_evaluation.py:127:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in c
98100
assert_type(ret1, Literal[0, 1])
99101
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
100102
101-
overloads_evaluation.py:144:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_tuple: bad return type [bad-return-type]
103+
overloads_evaluation.py:141:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_type_union: bad return type [bad-return-type]
102104
103105
return 1
104106
\u001b[1m\u001b[31m~\u001b[39m\u001b[0m
105107
106-
overloads_evaluation.py:148:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in check_expand_tuple: int [assert-type]
108+
overloads_evaluation.py:145:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in check_expand_type_union: int [assert-type]
109+
110+
assert_type(ret1, int | str)
111+
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
112+
113+
overloads_evaluation.py:163:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_tuple: bad return type [bad-return-type]
114+
115+
return 1
116+
\u001b[1m\u001b[31m~\u001b[39m\u001b[0m
117+
118+
overloads_evaluation.py:167:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in check_expand_tuple: int [assert-type]
107119
108120
assert_type(ret1, int | str)
109121
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m

conformance/results/results.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ <h3>Python Type System Conformance Test Results</h3>
165165
<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'>5.9sec</div>
169169
</th>
170170
<th class='tc-header'><div class='tc-name'>pytype 2024.10.11</div>
171171
<div class='tc-time'>31.0sec</div>
@@ -691,9 +691,9 @@ <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><p>Does not expand boolean arguments to Literal[True] and Literal[False].</p><p>Does not expand enum arguments to literal variants.</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><p>Does not expand enum arguments to literal variants.</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><p>Does not expand enum arguments to literal variants.</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><p>Does not expand enum arguments to literal variants.</p><p>Does not expand tuple arguments to possible combinations.</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><p>Does not expand enum arguments to literal variants.</p><p>Does not expand tuple arguments to possible combinations.</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><p>Does not expand enum arguments to literal variants.</p><p>Does not expand type[A | B] to type[A] and type[B].</p><p>Does not expand tuple arguments to possible combinations.</p></span></div></th>
697697
<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 any types) when matching overloads.</p></span></div></th>
698698
</tr>
699699
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overloads_overlap</th>

conformance/tests/overloads_evaluation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ def check_expand_enum(v: Color) -> None:
126126
ret1 = expand_enum(v)
127127
assert_type(ret1, Literal[0, 1])
128128

129+
130+
# > 4. ``type[A | B]`` should be expanded into ``type[A]`` and ``type[B]``.
131+
132+
@overload
133+
def expand_type_union(x: type[int]) -> int:
134+
...
135+
136+
@overload
137+
def expand_type_union(x: type[str]) -> str:
138+
...
139+
140+
def expand_type_union(x: type[int] | type[str]) -> int | str:
141+
return 1
142+
143+
def check_expand_type_union(v: type[int | str]) -> None:
144+
ret1 = expand_type_union(v)
145+
assert_type(ret1, int | str)
146+
147+
129148
# > 5. Tuples of known length that contain expandable types should be expanded
130149
# > into all possible combinations of their element types. For example, the type
131150
# > ``tuple[int | str, bool]`` should be expanded into ``(int, Literal[True])``,

0 commit comments

Comments
 (0)