-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Callables like operator.call
produce arg-type
error with generic or overloaded functions
#14337
Comments
It works when the definition of T = TypeVar("T")
U = TypeVar("U")
def call2(cb: Callable[[T], T], arg0: T, /) -> T:
return cb(arg0)
def ident(x: U) -> U:
return x
assert_type(call2(ident, "foo"), str) So, I guess mypy has trouble realizing it should assign |
yeah, the problem seems to be that Mypy isn't binding |
overloads aren't getting resolved either: from operator import call
from typing import Literal
from typing import overload
@overload
def foo(*, flag: Literal[True], extra_if_flag: int = ...) -> None:
...
@overload
def foo(*, flag: Literal[False] = ...) -> None:
...
def foo(*, flag: bool = False, extra_if_flag: int = 0) -> None:
...
foo()
call(foo) # error: Missing named argument "flag" for "call" [call-arg]
foo(flag=False)
call(foo, flag=False) # error: Argument "flag" to "call" has incompatible type "Literal[False]"; expected "Literal[True]" [arg-type]
foo(flag=True)
call(foo, flag=True)
foo(flag=True, extra_if_flag=0)
call(foo, flag=True, extra_if_flag=0) (https://mypy-play.net/?mypy=1.1.1&python=3.11&gist=590d1fbbfdc8afd03a82d7877bbea61b) |
operator.call
produce arg-type
error with generic functionsoperator.call
produce arg-type
error with generic or overloaded functions
The original example (with generics) passes on current master with
One can argue that mypy is overly strict here (the error goes away if you use regular position-or-name arguments in definition of I recommend opening a specific separate issue about this remaining edge case if you think it is important. (cc @JukkaL this is precisely the corner case I mentioned in #15896) |
Bug Report
operator.call
and similarly-typed callables produce anarg-type
error when used with many generic functions.Additionally, if the
arg-type
error is# type: ignore
'd, the inferred type is aTypeVar
(removed from its scope!) rather thanAny
.To Reproduce
(https://mypy-play.net/?mypy=latest&python=3.11&gist=a66524b9632714157bd87ef422725e41)
Expected Behavior
The inferred type of
operator.call(ident, "foo")
should match the inferred type ofident("foo")
. (Aside: should this bestr
orLiteral["foo"]
?)Comparing to other type checkers,
call2(ident, "foo")
works with pyre (inferred asLiteral["foo"]
), sort-of-works with pyright (inferred asU@ident | str
), and doesn't work with pytype (inferred asAny
).Also, if
# type: ignore[arg-type]
is used on thearg-type
error, the inferred type should byAny
, not aTypeVar
removed from its scope.Actual Behavior
See comments in the reproducer above.
Your Environment
mypy.ini
(and other config files): none required to reproduceThe text was updated successfully, but these errors were encountered: