From c041484145c4be36ec8ea5aa58013c90275e85aa Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Fri, 10 Jan 2025 19:18:46 -0800 Subject: [PATCH] add test for type[A | B] expansion --- .../results/mypy/overloads_evaluation.toml | 8 ++++---- .../results/pyre/overloads_evaluation.toml | 13 ++++++++---- conformance/results/pyre/version.toml | 2 +- .../results/pyright/overloads_evaluation.toml | 10 +++++----- .../results/pytype/overloads_evaluation.toml | 20 +++++++++++++++---- conformance/results/results.html | 8 ++++---- conformance/tests/overloads_evaluation.py | 19 ++++++++++++++++++ 7 files changed, 58 insertions(+), 22 deletions(-) diff --git a/conformance/results/mypy/overloads_evaluation.toml b/conformance/results/mypy/overloads_evaluation.toml index 2b911de2..23d82075 100644 --- a/conformance/results/mypy/overloads_evaluation.toml +++ b/conformance/results/mypy/overloads_evaluation.toml @@ -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] @@ -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] """ diff --git a/conformance/results/pyre/overloads_evaluation.toml b/conformance/results/pyre/overloads_evaluation.toml index 49bfb38c..79dfb530 100644 --- a/conformance/results/pyre/overloads_evaluation.toml +++ b/conformance/results/pyre/overloads_evaluation.toml @@ -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" @@ -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`. @@ -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`. """ diff --git a/conformance/results/pyre/version.toml b/conformance/results/pyre/version.toml index 439cd1ec..e22b34fd 100644 --- a/conformance/results/pyre/version.toml +++ b/conformance/results/pyre/version.toml @@ -1,2 +1,2 @@ version = "pyre 0.9.23" -test_duration = 6.1 +test_duration = 5.9 diff --git a/conformance/results/pyright/overloads_evaluation.toml b/conformance/results/pyright/overloads_evaluation.toml index 5b7de3cc..d38b282c 100644 --- a/conformance/results/pyright/overloads_evaluation.toml +++ b/conformance/results/pyright/overloads_evaluation.toml @@ -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 @@ -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) """ diff --git a/conformance/results/pytype/overloads_evaluation.toml b/conformance/results/pytype/overloads_evaluation.toml index 915cf401..10f0f163 100644 --- a/conformance/results/pytype/overloads_evaluation.toml +++ b/conformance/results/pytype/overloads_evaluation.toml @@ -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] @@ -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 diff --git a/conformance/results/results.html b/conformance/results/results.html index 8d7f3d17..d1d5a037 100644 --- a/conformance/results/results.html +++ b/conformance/results/results.html @@ -165,7 +165,7 @@

Python Type System Conformance Test Results

1.5sec
pyre 0.9.23
-
6.1sec
+
5.9sec
pytype 2024.10.11
31.0sec
@@ -691,9 +691,9 @@

Python Type System Conformance Test Results

Fail

Does not enforce any of the specified rules regarding overload definitions.

     overloads_evaluation -
Partial

Does not pick a winning overload based on arity, prior to considering argument types.

Does not expand boolean arguments to Literal[True] and Literal[False].

Does not expand enum arguments to literal variants.

-
Partial

Does not expand boolean arguments to Literal[True] and Literal[False].

Does not expand enum arguments to literal variants.

-
Partial

Does not expand boolean arguments to Literal[True] and Literal[False].

Does not expand enum arguments to literal variants.

+
Partial

Does not pick a winning overload based on arity, prior to considering argument types.

Does not expand boolean arguments to Literal[True] and Literal[False].

Does not expand enum arguments to literal variants.

Does not expand tuple arguments to possible combinations.

+
Partial

Does not expand boolean arguments to Literal[True] and Literal[False].

Does not expand enum arguments to literal variants.

Does not expand tuple arguments to possible combinations.

+
Partial

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.

Partial

Does not pick a winning overload based on arity, prior to considering argument types.

Does not perform argument expansion (on any types) when matching overloads.

     overloads_overlap diff --git a/conformance/tests/overloads_evaluation.py b/conformance/tests/overloads_evaluation.py index f20831c2..f73c27fd 100644 --- a/conformance/tests/overloads_evaluation.py +++ b/conformance/tests/overloads_evaluation.py @@ -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])``,