-
-
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
Changes from 1 commit
e6fd81e
c874a35
a87ce4f
2a91928
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -1313,15 +1313,15 @@ def f(x): pass | |
|
||
a: Any | ||
# Any causes ambiguity | ||
reveal_type(f(a, 1, '')) # E: Revealed type is 'Any' | ||
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 +1333,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 +1343,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 +1352,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 | ||
|
@@ -1365,7 +1365,7 @@ def f(x): pass | |
|
||
a: Any | ||
# TODO: We could infer 'int' here | ||
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. Hm, this TODO looks interesting. Would it be hard to fix? |
||
reveal_type(f(1, *a)) # E: Revealed type is 'Any' | ||
reveal_type(f(1, *a)) # E: Revealed type is 'builtins.object' | ||
|
||
[case testOverloadWithOverlappingItemsAndAnyArgument8] | ||
from typing import overload, Any | ||
|
@@ -1381,6 +1381,26 @@ 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 testOverloadOnOverloadWithType] | ||
from typing import Any, Type, TypeVar, overload | ||
from mod import MyInt | ||
|
@@ -1723,6 +1743,105 @@ 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: ... | ||
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. I would add a test with overload like this: @overload
def f(x: int) -> Tuple[int]: ...
@overload
def f(x: int, y: int) -> Tuple[int, int]: ...
@overload
def f(*xs: int) -> Tuple[int, ...]: ... And check how it various calls with and without star. |
||
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 testOverloadWithPartiallyOverlappingUnions] | ||
from typing import overload, Union | ||
|
||
|
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.
This comment is now less clear, I would rather remove it or clarify.