Skip to content

Commit 4f272ef

Browse files
committed
Update ambiguity rule.
1 parent 93eec95 commit 4f272ef

8 files changed

Lines changed: 14 additions & 14 deletions

File tree

conformance/results/mypy/overloads_evaluation.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Line 268: Unexpected errors ['overloads_evaluation.py:268: error: Expression is
2424
Line 284: Unexpected errors ['overloads_evaluation.py:284: error: Expression is of type "list[Any]", not "Any" [assert-type]']
2525
Line 306: Unexpected errors ['overloads_evaluation.py:306: error: Expression is of type "Any", not "float" [assert-type]']
2626
Line 350: Unexpected errors ['overloads_evaluation.py:350: error: Expression is of type "list[Any]", not "Any" [assert-type]']
27-
Line 395: Unexpected errors ['overloads_evaluation.py:395: error: Expression is of type "Any", not "int" [assert-type]']
2827
Line 439: Unexpected errors ['overloads_evaluation.py:439: error: Expression is of type "Any", not "bool" [assert-type]']
2928
"""
3029
output = """
@@ -58,6 +57,5 @@ overloads_evaluation.py:268: error: Expression is of type "list[Any]", not "Any"
5857
overloads_evaluation.py:284: error: Expression is of type "list[Any]", not "Any" [assert-type]
5958
overloads_evaluation.py:306: error: Expression is of type "Any", not "float" [assert-type]
6059
overloads_evaluation.py:350: error: Expression is of type "list[Any]", not "Any" [assert-type]
61-
overloads_evaluation.py:395: error: Expression is of type "Any", not "int" [assert-type]
6260
overloads_evaluation.py:439: error: Expression is of type "Any", not "bool" [assert-type]
6361
"""

conformance/results/pycroscope/overloads_evaluation.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
conformance_automated = "Fail"
22
errors_diff = """
33
Line 371: Unexpected errors ['./overloads_evaluation.py:371:16: Any[multiple_overload_matches] is not equivalent to bool']
4-
Line 395: Unexpected errors ['./overloads_evaluation.py:395:16: Any[multiple_overload_matches] is not equivalent to int']
54
Line 400: Unexpected errors ['./overloads_evaluation.py:400:16: Any[multiple_overload_matches] is not equivalent to bool']
65
Line 420: Unexpected errors ['./overloads_evaluation.py:420:16: Any[multiple_overload_matches] is not equivalent to bool']
76
Line 439: Unexpected errors ['./overloads_evaluation.py:439:16: Any[multiple_overload_matches] is not equivalent to bool']
@@ -14,7 +13,6 @@ output = """
1413
./overloads_evaluation.py:51:0: Cannot call overloaded function [incompatible_argument]
1514
./overloads_evaluation.py:116:4: Cannot call overloaded function [incompatible_argument]
1615
./overloads_evaluation.py:371:16: Any[multiple_overload_matches] is not equivalent to bool
17-
./overloads_evaluation.py:395:16: Any[multiple_overload_matches] is not equivalent to int
1816
./overloads_evaluation.py:400:16: Any[multiple_overload_matches] is not equivalent to bool
1917
./overloads_evaluation.py:420:16: Any[multiple_overload_matches] is not equivalent to bool
2018
./overloads_evaluation.py:439:16: Any[multiple_overload_matches] is not equivalent to bool
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
conformant = "Pass"
2-
conformance_automated = "Pass"
2+
conformance_automated = "Fail"
33
errors_diff = """
4+
Line 395: Unexpected errors ['assert_type(int, Any) failed [assert-type]']
45
"""
56
output = """
67
ERROR overloads_evaluation.py:38:11-13: No matching overload found for function `example1_1` called with arguments: () [no-matching-overload]
78
ERROR overloads_evaluation.py:46:15-16: Argument `Literal[1]` is not assignable to parameter `y` with type `str` in function `example1_1` [bad-argument-type]
89
ERROR overloads_evaluation.py:51:12-13: Argument `Literal[1]` is not assignable to parameter `x` with type `str` in function `example1_1` [bad-argument-type]
910
ERROR overloads_evaluation.py:116:13-22: No matching overload found for function `example2` called with arguments: (int | str, int | str, Literal[1]) [no-matching-overload]
11+
ERROR overloads_evaluation.py:395:16-27: assert_type(int, Any) failed [assert-type]
1012
"""

conformance/results/pyright/overloads_evaluation.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Does not evaluate `Any` in some cases where overload is ambiguous.
1010
conformance_automated = "Fail"
1111
errors_diff = """
1212
Line 284: Unexpected errors ['overloads_evaluation.py:284:17 - error: "assert_type" mismatch: expected "Any" but received "list[int]" (reportAssertTypeFailure)']
13+
Line 395: Unexpected errors ['overloads_evaluation.py:395:17 - error: "assert_type" mismatch: expected "Any" but received "int" (reportAssertTypeFailure)']
1314
Line 468: Unexpected errors ['overloads_evaluation.py:468:17 - error: "assert_type" mismatch: expected "A[Any]" but received "A[None]" (reportAssertTypeFailure)']
1415
"""
1516
output = """
@@ -27,5 +28,6 @@ overloads_evaluation.py:116:17 - error: Argument of type "int | str" cannot be a
2728
  Type "int | str" is not assignable to type "int"
2829
    "str" is not assignable to "int" (reportArgumentType)
2930
overloads_evaluation.py:284:17 - error: "assert_type" mismatch: expected "Any" but received "list[int]" (reportAssertTypeFailure)
31+
overloads_evaluation.py:395:17 - error: "assert_type" mismatch: expected "Any" but received "int" (reportAssertTypeFailure)
3032
overloads_evaluation.py:468:17 - error: "assert_type" mismatch: expected "A[Any]" but received "A[None]" (reportAssertTypeFailure)
3133
"""

conformance/results/ty/overloads_evaluation.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ Returns Any instead of most general return type for ambiguous calls.
44
"""
55
conformance_automated = "Fail"
66
errors_diff = """
7-
Line 395: Unexpected errors ['overloads_evaluation.py:395:5: error[type-assertion-failure] Type `Unknown` does not match asserted type `int`']
87
Line 468: Unexpected errors ['overloads_evaluation.py:468:5: error[type-assertion-failure] Type `Unknown` does not match asserted type `A[Any]`']
98
"""
109
output = """
1110
overloads_evaluation.py:38:1: error[no-matching-overload] No overload of function `example1_1` matches arguments
1211
overloads_evaluation.py:46:15: error[invalid-argument-type] Argument to function `example1_1` is incorrect: Expected `str`, found `Literal[1]`
1312
overloads_evaluation.py:51:12: error[invalid-argument-type] Argument to function `example1_1` is incorrect: Expected `str`, found `Literal[1]`
1413
overloads_evaluation.py:116:5: error[no-matching-overload] No overload of function `example2` matches arguments
15-
overloads_evaluation.py:395:5: error[type-assertion-failure] Type `Unknown` does not match asserted type `int`
1614
overloads_evaluation.py:468:5: error[type-assertion-failure] Type `Unknown` does not match asserted type `A[Any]`
1715
"""

conformance/results/zuban/overloads_evaluation.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Returns Any instead of most general return type for ambiguous calls.
55
"""
66
conformance_automated = "Fail"
77
errors_diff = """
8-
Line 395: Unexpected errors ['overloads_evaluation.py:395: error: Expression is of type "Any", not "int" [misc]']
98
Line 439: Unexpected errors ['overloads_evaluation.py:439: error: Expression is of type "Any", not "bool" [misc]']
109
Line 468: Unexpected errors ['overloads_evaluation.py:468: error: Expression is of type "Any", not "A[Any]" [misc]']
1110
"""
@@ -24,7 +23,6 @@ overloads_evaluation.py:51: note: def example1_1(x: int, y: str) -> int
2423
overloads_evaluation.py:51: note: def example1_1(x: str) -> str
2524
overloads_evaluation.py:116: error: Argument 1 to "example2" has incompatible type "int | str"; expected "int" [arg-type]
2625
overloads_evaluation.py:116: error: Argument 2 to "example2" has incompatible type "int | str"; expected "str" [arg-type]
27-
overloads_evaluation.py:395: error: Expression is of type "Any", not "int" [misc]
2826
overloads_evaluation.py:439: error: Expression is of type "Any", not "bool" [misc]
2927
overloads_evaluation.py:468: error: Expression is of type "Any", not "A[Any]" [misc]
3028
"""

conformance/tests/overloads_evaluation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ def example9(x: str | bytes, y: str) -> bool | int:
390390
def check_example9(x: Any):
391391
# All three overloads are candidates. The parameter types corresponding to
392392
# argument `x` are `str` and `bytes`, which are not equivalent, so none of
393-
# the overloads can be eliminated. We pick the most general return type.
393+
# the overloads can be eliminated. We fall back to `Any`.
394394
ret1 = example9(x, 'o1')
395-
assert_type(ret1, int)
395+
assert_type(ret1, Any)
396396
# The second and third overload are candidates. The parameter type
397397
# corresponding to argument `x` is `bytes` in both candidates, so we can
398398
# eliminate the third overload.

docs/spec/overload.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,13 @@ caller eliminates types that include ``Any``.
315315
Once this filtering process is applied for all arguments, examine the return
316316
types of the remaining overloads. If these return types include type variables
317317
that are scoped to the called function, they should be replaced with their
318-
solved types. Eliminate every overload for which there exists a
319-
:term:`materialization <materialize>` of another overload's return type that is
320-
not assignable to this overload's return type.
318+
solved types.
319+
320+
Next, keep only the overloads for which both of the following are true:
321+
* All :term:`materializations <materialize>` of every other candidate
322+
overload's return type are assignable to this overload's return type, and
323+
* This overload's return type is assignable to every other candidate overload's
324+
return type.
321325

322326
This rule picks the most general return type, if one exists.
323327

0 commit comments

Comments
 (0)