Skip to content

Commit

Permalink
add test for type[A | B] expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Jan 11, 2025
1 parent 169fa58 commit c041484
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
8 changes: 4 additions & 4 deletions conformance/results/mypy/overloads_evaluation.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Line 103: Unexpected errors ['overloads_evaluation.py:103: error: No overload va
Line 104: Unexpected errors ['overloads_evaluation.py:104: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]']
Line 126: Unexpected errors ['overloads_evaluation.py:126: error: No overload variant of "expand_enum" matches argument type "Color" [call-overload]']
Line 127: Unexpected errors ['overloads_evaluation.py:127: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]']
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]']
Line 148: Unexpected errors ['overloads_evaluation.py:148: error: Expression is of type "int", not "int | str" [assert-type]']
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]']
Line 167: Unexpected errors ['overloads_evaluation.py:167: error: Expression is of type "int", not "int | str" [assert-type]']
"""
output = """
overloads_evaluation.py:27: error: All overload variants of "example1" require at least one argument [call-overload]
Expand Down Expand Up @@ -43,6 +43,6 @@ overloads_evaluation.py:126: note: Possible overload variants:
overloads_evaluation.py:126: note: def expand_enum(x: Literal[Color.RED]) -> Literal[0]
overloads_evaluation.py:126: note: def expand_enum(x: Literal[Color.BLUE]) -> Literal[1]
overloads_evaluation.py:127: error: Expression is of type "Any", not "Literal[0, 1]" [assert-type]
overloads_evaluation.py:147: error: Argument 1 to "expand_tuple" has incompatible type "tuple[int, int | str]"; expected "tuple[int, int]" [arg-type]
overloads_evaluation.py:148: error: Expression is of type "int", not "int | str" [assert-type]
overloads_evaluation.py:166: error: Argument 1 to "expand_tuple" has incompatible type "tuple[int, int | str]"; expected "tuple[int, int]" [arg-type]
overloads_evaluation.py:167: error: Expression is of type "int", not "int | str" [assert-type]
"""
13 changes: 9 additions & 4 deletions conformance/results/pyre/overloads_evaluation.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ conformant = "Partial"
notes = """
Does not expand boolean arguments to Literal[True] and Literal[False].
Does not expand enum arguments to literal variants.
Does not expand type[A | B] to type[A] and type[B].
Does not expand tuple arguments to possible combinations.
"""
conformance_automated = "Fail"
Expand All @@ -12,8 +13,10 @@ Line 103: Unexpected errors ['overloads_evaluation.py:103:23 Incompatible parame
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]`.']
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`.']
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]`.']
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]]`.']
Line 148: Unexpected errors ['overloads_evaluation.py:148:4 Assert type [70]: Expected `Union[int, str]` but got `int`.']
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]]`.']
Line 145: Unexpected errors ['overloads_evaluation.py:145:4 Assert type [70]: Expected `Union[int, str]` but got `int`.']
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]]`.']
Line 167: Unexpected errors ['overloads_evaluation.py:167:4 Assert type [70]: Expected `Union[int, str]` but got `int`.']
"""
output = """
overloads_evaluation.py:27:0 Missing argument [20]: Call `example1` expects argument `x`.
Expand All @@ -27,6 +30,8 @@ overloads_evaluation.py:103:23 Incompatible parameter type [6]: In call `expand_
overloads_evaluation.py:104:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.
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`.
overloads_evaluation.py:127:4 Assert type [70]: Expected `Union[typing_extensions.Literal[0], typing_extensions.Literal[1]]` but got `typing_extensions.Literal[0]`.
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]]`.
overloads_evaluation.py:148:4 Assert type [70]: Expected `Union[int, str]` but got `int`.
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]]`.
overloads_evaluation.py:145:4 Assert type [70]: Expected `Union[int, str]` but got `int`.
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]]`.
overloads_evaluation.py:167:4 Assert type [70]: Expected `Union[int, str]` but got `int`.
"""
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.23"
test_duration = 6.1
test_duration = 5.9
10 changes: 5 additions & 5 deletions conformance/results/pyright/overloads_evaluation.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Line 103: Unexpected errors ['overloads_evaluation.py:103:12 - error: No overloa
Line 104: Unexpected errors ['overloads_evaluation.py:104:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)']
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"']
Line 127: Unexpected errors ['overloads_evaluation.py:127:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)']
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"']
Line 148: Unexpected errors ['overloads_evaluation.py:148:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)']
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"']
Line 167: Unexpected errors ['overloads_evaluation.py:167:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)']
"""
output = """
overloads_evaluation.py:27:1 - error: No overloads for "example1" match the provided arguments
Expand All @@ -35,9 +35,9 @@ overloads_evaluation.py:126:12 - error: No overloads for "expand_enum" match the
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"
  "Color" is not assignable to type "Literal[Color.BLUE]" (reportArgumentType)
overloads_evaluation.py:127:17 - error: "assert_type" mismatch: expected "Literal[0, 1]" but received "Unknown" (reportAssertTypeFailure)
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"
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"
  Type "int | str" is not assignable to type "str"
    "int" is not assignable to "str" (reportArgumentType)
overloads_evaluation.py:148:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)
overloads_evaluation.py:167:17 - error: "assert_type" mismatch: expected "int | str" but received "Unknown" (reportAssertTypeFailure)
"""
20 changes: 16 additions & 4 deletions conformance/results/pytype/overloads_evaluation.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ Line 104: Unexpected errors ['overloads_evaluation.py:104:5: \\x1b[1m\\x1b[31mer
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]']
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]']
Line 127: Unexpected errors ['overloads_evaluation.py:127:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_enum: Any [assert-type]']
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]']
Line 148: Unexpected errors ['overloads_evaluation.py:148:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_tuple: int [assert-type]']
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]']
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]']
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]']
Line 167: Unexpected errors ['overloads_evaluation.py:167:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in check_expand_tuple: int [assert-type]']
"""
output = """
overloads_evaluation.py:23:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in example1: bad return type [bad-return-type]
Expand Down Expand Up @@ -98,12 +100,22 @@ overloads_evaluation.py:127:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in c
assert_type(ret1, Literal[0, 1])
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
overloads_evaluation.py:144:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_tuple: bad return type [bad-return-type]
overloads_evaluation.py:141:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_type_union: bad return type [bad-return-type]
return 1
\u001b[1m\u001b[31m~\u001b[39m\u001b[0m
overloads_evaluation.py:148:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in check_expand_tuple: int [assert-type]
overloads_evaluation.py:145:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in check_expand_type_union: int [assert-type]
assert_type(ret1, int | str)
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
overloads_evaluation.py:163:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in expand_tuple: bad return type [bad-return-type]
return 1
\u001b[1m\u001b[31m~\u001b[39m\u001b[0m
overloads_evaluation.py:167:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in check_expand_tuple: int [assert-type]
assert_type(ret1, int | str)
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
Expand Down
8 changes: 4 additions & 4 deletions conformance/results/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ <h3>Python Type System Conformance Test Results</h3>
<div class='tc-time'>1.5sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyre 0.9.23</div>
<div class='tc-time'>6.1sec</div>
<div class='tc-time'>5.9sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pytype 2024.10.11</div>
<div class='tc-time'>31.0sec</div>
Expand Down Expand Up @@ -691,9 +691,9 @@ <h3>Python Type System Conformance Test Results</h3>
<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>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overloads_evaluation</th>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overloads_overlap</th>
Expand Down
19 changes: 19 additions & 0 deletions conformance/tests/overloads_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ def check_expand_enum(v: Color) -> None:
ret1 = expand_enum(v)
assert_type(ret1, Literal[0, 1])


# > 4. ``type[A | B]`` should be expanded into ``type[A]`` and ``type[B]``.

@overload
def expand_type_union(x: type[int]) -> int:
...

@overload
def expand_type_union(x: type[str]) -> str:
...

def expand_type_union(x: type[int] | type[str]) -> int | str:
return 1

def check_expand_type_union(v: type[int | str]) -> None:
ret1 = expand_type_union(v)
assert_type(ret1, int | str)


# > 5. Tuples of known length that contain expandable types should be expanded
# > into all possible combinations of their element types. For example, the type
# > ``tuple[int | str, bool]`` should be expanded into ``(int, Literal[True])``,
Expand Down

0 comments on commit c041484

Please sign in to comment.