-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Refine how overload selection handles *args, **kwargs, and Any #5166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ilevkivskyi
merged 4 commits into
python:master
from
Michael0x2a:overloads-refine-handling-of-starargs-and-any
Jun 13, 2018
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6fd81e
Refine how overload selection handles *args, **kwargs, and Any
Michael0x2a c874a35
Respond to code review
Michael0x2a a87ce4f
Refactor Any ambiguity check
Michael0x2a 2a91928
Refine how overloads handle Tuples and other shaped types
Michael0x2a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1276,7 +1276,7 @@ def f(x: object) -> object: ... | |
def f(x): pass | ||
|
||
a: Any | ||
reveal_type(f(a)) # E: Revealed type is 'Any' | ||
reveal_type(f(a)) # E: Revealed type is 'builtins.object' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument2] | ||
from typing import overload, Any | ||
|
@@ -1288,7 +1288,7 @@ def f(x: float) -> float: ... | |
def f(x): pass | ||
|
||
a: Any | ||
reveal_type(f(a)) # E: Revealed type is 'Any' | ||
reveal_type(f(a)) # E: Revealed type is 'builtins.float' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument3] | ||
from typing import overload, Any | ||
|
@@ -1312,16 +1312,17 @@ def f(x: object, y: int, z: str) -> object: ... | |
def f(x): pass | ||
|
||
a: Any | ||
# Any causes ambiguity | ||
reveal_type(f(a, 1, '')) # E: Revealed type is 'Any' | ||
# Any causes ambiguity; we fall back to returning object since it's a | ||
# supertype of int | ||
reveal_type(f(a, 1, '')) # E: Revealed type is 'builtins.object' | ||
# Any causes no ambiguity | ||
reveal_type(f(1, a, a)) # E: Revealed type is 'builtins.int' | ||
reveal_type(f('', a, a)) # E: Revealed type is 'builtins.object' | ||
# Like above, but use keyword arguments. | ||
reveal_type(f(y=1, z='', x=a)) # E: Revealed type is 'Any' | ||
reveal_type(f(y=1, z='', x=a)) # E: Revealed type is 'builtins.object' | ||
reveal_type(f(y=a, z='', x=1)) # E: Revealed type is 'builtins.int' | ||
reveal_type(f(z='', x=1, y=a)) # E: Revealed type is 'builtins.int' | ||
reveal_type(f(z='', x=a, y=1)) # E: Revealed type is 'Any' | ||
reveal_type(f(z='', x=a, y=1)) # E: Revealed type is 'builtins.object' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument5] | ||
from typing import overload, Any, Union | ||
|
@@ -1333,7 +1334,7 @@ def f(x: Union[int, float]) -> float: ... | |
def f(x): pass | ||
|
||
a: Any | ||
reveal_type(f(a)) # E: Revealed type is 'Any' | ||
reveal_type(f(a)) # E: Revealed type is 'builtins.float' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument6] | ||
from typing import overload, Any | ||
|
@@ -1343,7 +1344,7 @@ def f(x: int, y: int) -> int: ... | |
@overload | ||
def f(x: float, y: int, z: str) -> float: ... | ||
@overload | ||
def f(x: object, y: int, z: str, a: None) -> object: ... | ||
def f(x: object, y: int, z: str, a: None) -> str: ... | ||
def f(x): pass | ||
|
||
a: Any | ||
|
@@ -1352,7 +1353,7 @@ reveal_type(f(*a)) # E: Revealed type is 'Any' | |
reveal_type(f(a, *a)) # E: Revealed type is 'Any' | ||
reveal_type(f(1, *a)) # E: Revealed type is 'Any' | ||
reveal_type(f(1.1, *a)) # E: Revealed type is 'Any' | ||
reveal_type(f('', *a)) # E: Revealed type is 'builtins.object' | ||
reveal_type(f('', *a)) # E: Revealed type is 'builtins.str' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument7] | ||
from typing import overload, Any | ||
|
@@ -1363,9 +1364,15 @@ def f(x: int, y: int, z: int) -> int: ... | |
def f(x: object, y: int, z: int) -> object: ... | ||
def f(x): pass | ||
|
||
@overload | ||
def g(x: int, y: int, z: int) -> int: ... | ||
@overload | ||
def g(x: object, y: int, z: str) -> object: ... | ||
def g(x): pass | ||
|
||
a: Any | ||
# TODO: We could infer 'int' here | ||
reveal_type(f(1, *a)) # E: Revealed type is 'Any' | ||
reveal_type(f(1, *a)) # E: Revealed type is 'builtins.int' | ||
reveal_type(g(1, *a)) # E: Revealed type is 'builtins.object' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument8] | ||
from typing import overload, Any | ||
|
@@ -1381,6 +1388,58 @@ a: Any | |
reveal_type(f(a, 1, 1)) # E: Revealed type is 'builtins.str' | ||
reveal_type(f(1, *a)) # E: Revealed type is 'builtins.str' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument9] | ||
from typing import overload, Any, List | ||
|
||
@overload | ||
def f(x: List[int]) -> List[int]: ... | ||
@overload | ||
def f(x: List[Any]) -> List[Any]: ... | ||
def f(x): pass | ||
|
||
a: Any | ||
b: List[Any] | ||
c: List[str] | ||
d: List[int] | ||
reveal_type(f(a)) # E: Revealed type is 'builtins.list[Any]' | ||
reveal_type(f(b)) # E: Revealed type is 'builtins.list[Any]' | ||
reveal_type(f(c)) # E: Revealed type is 'builtins.list[Any]' | ||
reveal_type(f(d)) # E: Revealed type is 'builtins.list[builtins.int]' | ||
|
||
[builtins fixtures/list.pyi] | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument10] | ||
from typing import overload, Any | ||
|
||
@overload | ||
def f(*, x: int = 3, y: int = 3) -> int: ... | ||
@overload | ||
def f(**kwargs: str) -> str: ... | ||
def f(*args, **kwargs): pass | ||
|
||
a: Any | ||
i: int | ||
reveal_type(f(x=a, y=i)) # E: Revealed type is 'builtins.int' | ||
reveal_type(f(y=a)) # E: Revealed type is 'Any' | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument11] | ||
from typing import overload, Any, Dict | ||
|
||
@overload | ||
def f(x: int = 3, **kwargs: int) -> int: ... | ||
@overload | ||
def f(**kwargs: str) -> str: ... | ||
def f(*args, **kwargs): pass | ||
|
||
a: Dict[str, Any] | ||
i: int | ||
reveal_type(f(x=i, **a)) # E: Revealed type is 'builtins.int' | ||
reveal_type(f(**a)) # E: Revealed type is 'Any' | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testOverloadOnOverloadWithType] | ||
from typing import Any, Type, TypeVar, overload | ||
from mod import MyInt | ||
|
@@ -1723,6 +1782,127 @@ def foo2(**kwargs: int) -> str: ... | |
def foo2(*args: int) -> int: ... # E: Overloaded function signature 2 will never be matched: function 1's parameter type(s) are the same or broader | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testOverloadVarargInputAndVarargDefinition] | ||
from typing import overload, List | ||
|
||
class A: ... | ||
class B: ... | ||
class C: ... | ||
|
||
@overload | ||
def foo(x: int) -> A: ... | ||
@overload | ||
def foo(x: int, y: int) -> B: ... | ||
@overload | ||
def foo(x: int, y: int, z: int, *args: int) -> C: ... | ||
def foo(*args): pass | ||
|
||
reveal_type(foo(1)) # E: Revealed type is '__main__.A' | ||
reveal_type(foo(1, 2)) # E: Revealed type is '__main__.B' | ||
reveal_type(foo(1, 2, 3)) # E: Revealed type is '__main__.C' | ||
|
||
reveal_type(foo(*[1])) # E: Revealed type is '__main__.C' | ||
reveal_type(foo(*[1, 2])) # E: Revealed type is '__main__.C' | ||
reveal_type(foo(*[1, 2, 3])) # E: Revealed type is '__main__.C' | ||
|
||
x: List[int] | ||
reveal_type(foo(*x)) # E: Revealed type is '__main__.C' | ||
|
||
y: List[str] | ||
foo(*y) # E: No overload variant of "foo" matches argument type "List[str]" | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testOverloadMultipleVarargDefinition] | ||
from typing import overload, List, Any | ||
|
||
class A: ... | ||
class B: ... | ||
class C: ... | ||
class D: ... | ||
|
||
@overload | ||
def foo(x: int) -> A: ... | ||
@overload | ||
def foo(x: int, y: int) -> B: ... | ||
@overload | ||
def foo(x: int, y: int, z: int, *args: int) -> C: ... | ||
@overload | ||
def foo(*x: str) -> D: ... | ||
def foo(*args): pass | ||
|
||
reveal_type(foo(*[1, 2])) # E: Revealed type is '__main__.C' | ||
reveal_type(foo(*["a", "b"])) # E: Revealed type is '__main__.D' | ||
|
||
x: List[Any] | ||
reveal_type(foo(*x)) # E: Revealed type is 'Any' | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testOverloadMultipleVarargDefinitionComplex] | ||
from typing import TypeVar, overload, Any, Callable | ||
|
||
T1 = TypeVar('T1') | ||
T2 = TypeVar('T2') | ||
T3 = TypeVar('T3') | ||
|
||
@overload | ||
def chain_call(input_value: T1, | ||
f1: Callable[[T1], T2]) -> T2: ... | ||
@overload | ||
def chain_call(input_value: T1, | ||
f1: Callable[[T1], T2], | ||
f2: Callable[[T2], T3]) -> T3: ... | ||
@overload | ||
def chain_call(input_value: T1, | ||
*f_rest: Callable[[T1], T1]) -> T1: ... | ||
@overload | ||
def chain_call(input_value: T1, | ||
f1: Callable[[T1], T2], | ||
f2: Callable[[T2], T3], | ||
f3: Callable[[T3], Any], | ||
*f_rest: Callable[[Any], Any]) -> Any: ... | ||
def chain_call(input_value, *f_rest): | ||
for function in f_rest: | ||
input_value = function(input_value) | ||
return input_value | ||
|
||
|
||
class A: ... | ||
class B: ... | ||
class C: ... | ||
class D: ... | ||
|
||
def f(x: A) -> A: ... | ||
def f1(x: A) -> B: ... | ||
def f2(x: B) -> C: ... | ||
def f3(x: C) -> D: ... | ||
|
||
reveal_type(chain_call(A(), f1, f2)) # E: Revealed type is '__main__.C*' | ||
reveal_type(chain_call(A(), f1, f2, f3)) # E: Revealed type is 'Any' | ||
reveal_type(chain_call(A(), f, f, f, f)) # E: Revealed type is '__main__.A' | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testOverloadVarargsSelection] | ||
from typing import overload, Tuple | ||
@overload | ||
def f(x: int) -> Tuple[int]: ... | ||
@overload | ||
def f(x: int, y: int) -> Tuple[int, int]: ... | ||
@overload | ||
def f(*xs: int) -> Tuple[int, ...]: ... | ||
def f(*args): pass | ||
|
||
i: int | ||
reveal_type(f(i)) # E: Revealed type is 'Tuple[builtins.int]' | ||
reveal_type(f(i, i)) # E: Revealed type is 'Tuple[builtins.int, builtins.int]' | ||
reveal_type(f(i, i, i)) # E: Revealed type is 'builtins.tuple[builtins.int]' | ||
|
||
reveal_type(f(*[])) # E: Revealed type is 'builtins.tuple[builtins.int]' | ||
reveal_type(f(*[i])) # E: Revealed type is 'builtins.tuple[builtins.int]' | ||
reveal_type(f(*[i, i])) # E: Revealed type is 'builtins.tuple[builtins.int]' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if there is a tuple instead of list? Ideally |
||
reveal_type(f(*[i, i, i])) # E: Revealed type is 'builtins.tuple[builtins.int]' | ||
|
||
[builtins fixtures/list.pyi] | ||
|
||
[case testOverloadWithPartiallyOverlappingUnions] | ||
from typing import overload, Union | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a test with overload like this:
And check how it various calls with and without star.