-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Fix bug with inferring bad arguments to overloads #5660
Changes from all commits
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 |
---|---|---|
|
@@ -4873,3 +4873,76 @@ def g(name: str) -> int: | |
|
||
reveal_type(f) # E: Revealed type is 'def (name: builtins.str) -> builtins.int' | ||
reveal_type(g) # E: Revealed type is 'def (name: builtins.str) -> builtins.int' | ||
|
||
[case testOverloadBadArgumentsInferredToAny1] | ||
from typing import Union, Any, overload | ||
|
||
def bar(x: int) -> Union[int, Any]: ... | ||
|
||
@overload | ||
def foo(x: str) -> None: ... | ||
@overload | ||
def foo(x: int) -> None: ... | ||
def foo(x) -> None: pass | ||
|
||
foo(bar('lol')) # E: Argument 1 to "bar" has incompatible type "str"; expected "int" | ||
|
||
[case testOverloadBadArgumentsInferredToAny2] | ||
from typing import Union, Iterable, Tuple, TypeVar, Generic, overload, Any | ||
|
||
class A: | ||
def foo(self) -> Iterable[int]: pass | ||
|
||
def bar(x: int) -> Union[A, int]: ... | ||
|
||
_T = TypeVar('_T') | ||
|
||
@overload | ||
def foo() -> None: ... | ||
@overload | ||
def foo(iterable: Iterable[_T]) -> None: ... | ||
def foo(iterable = None) -> None: pass | ||
|
||
foo(bar('lol').foo()) # E: Argument 1 to "bar" has incompatible type "str"; expected "int" \ | ||
# E: Item "int" of "Union[A, int]" has no attribute "foo" | ||
|
||
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 few tests about situations where absence of context can cause troubles (so that this will not regress). For example something like this: def g(x: Optional[T] = None) -> List[T]: ...
@overload
def f(x: int) -> int: ...
@overload
def f(x: List[int]) -> List[int]: ...
def f(x):
pass
reveal_type(f(g())) I would also add something like @overload
def g(x: List[str]) -> List[str]: ...
@overload
def g(x: List[int]) -> List[int]: ...
def g(x):
pass
@overload
def f(x: int) -> int: ...
@overload
def f(x: List[int]) -> List[int]: ...
def f(x):
pass
reveal_type(f(g([]))) but this one unfortunately causes an error both on master and with this PR. Probably this is something we should fix, but I am afraid this may depend on #4872 (but this is just a guess). |
||
[case testOverloadInferringArgumentsUsingContext1] | ||
from typing import Optional, List, overload, TypeVar | ||
T = TypeVar('T') | ||
|
||
def g(x: Optional[T] = None) -> List[T]: ... | ||
|
||
@overload | ||
def f(x: int) -> int: ... | ||
@overload | ||
def f(x: List[int]) -> List[int]: ... | ||
def f(x): pass | ||
|
||
reveal_type(f(g())) # E: Revealed type is 'builtins.list[builtins.int]' | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testOverloadInferringArgumentsUsingContext2-skip] | ||
# This test case ought to work, but is maybe blocked by | ||
# https://github.com/python/mypy/issues/4872? | ||
# | ||
# See https://github.com/python/mypy/pull/5660#discussion_r219669409 for | ||
# more context. | ||
|
||
from typing import Optional, List, overload, TypeVar | ||
T = TypeVar('T') | ||
@overload | ||
def g(x: List[str]) -> List[str]: ... | ||
@overload | ||
def g(x: List[int]) -> List[int]: ... | ||
def g(x): | ||
pass | ||
|
||
@overload | ||
def f(x: int) -> int: ... | ||
@overload | ||
def f(x: List[int]) -> List[int]: ... | ||
def f(x): | ||
pass | ||
|
||
reveal_type(f(g([]))) # E: Revealed type is 'builtins.list[builtins.int]' | ||
[builtins fixtures/list.pyi] |
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.
At first I was sceptical about this, but after some thinking and playing it seems to me this is actually OK. Because under the hood all relevant possibilities (even the union math) go through
infer_overload_return_type
, that callscheck_call
with aCallableType
item that, in turn, will triggerinfer_arg_types_in_context
using current matching overload item arg type as context. So it should be fine.