-
-
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
Mapping to abs doesn't work #6697
Comments
This looks like a type inference bug. It is hard to tell what exactly went wrong, but mypy complains because it wanted |
This happened again in #6703, and looks like a fundamental issue, so raising priority to high. |
I was able to recreate it with something more generic - from typing import TypeVar, Callable
T = TypeVar("T")
S = TypeVar("S")
def f(x: T) -> T:
return x
def g(f: Callable[[T], S], x: T) -> S:
return f(x)
g(f, 1) Output: |
Yes, this is a nice repro. |
Has this been fixed yet? I tried to follow the linked issues but I can't find an obvious answer. I'm running in a similar issue where I'm trying to map set over a list of lists: map(set, mylist) And similar to the original issue here, it complains about the first argument to map(lambda x: set(x), list_of_lists) Is there any practical workaround at this point without having to convert to lambda (that just doesn't make much sense)? |
Just encountered this using mypy 1.4 with def f(t: tuple[int, ...]) -> int:
return max(map(abs, t), default=0) |
I think I got something: It seems reveal_type(map(abs, [1,2,3])) # builtins.map[_T`-1], not builtins.map[int] ! If we look at the definition of def __init__(self, __func: Callable[[_T1], _S], __iter1: Iterable[_T1]) -> None: ... and in our case, abs_hinted: Callable[[SupportsAbs[int]], int] = abs # ✔
map(abs_hinted, [1,2,3]) # ✔ I created a mypy-playground with a minimal reproduction. definitions of the relevant builtinsDefinition of def abs(__x: SupportsAbs[_T]) -> _T: ... Definition of @runtime_checkable
class SupportsAbs(Protocol[_T_co]):
@abstractmethod
def __abs__(self) -> _T_co: ... Definition of def __init__(self, __func: Callable[[_T1], _S], __iter1: Iterable[_T1]) -> None: ... |
I don't know much about the internals of |
I just tried examples in this issue and they all seem to work on master if I use |
@ilevkivskyi Does this new inference solve issues like the following? from typing import Callable, TypeVar, assert_type
T = TypeVar("T")
identity: Callable[[T], T] = lambda x: x
assert_type(identity(0), int) # ✔
def foo(func: Callable[[T], T]) -> None:
assert_type(func(0), int) # ✘ incompatible type "int"; expected "T" https://mypy-play.net/?mypy=master&python=3.11&gist=5e696ae12fe41e704d5df7d70ca187c2 |
@randolf-scholz No, because there is no issue. When you define class Identity(Protocol):
def __call__(self, __x: T) -> T: ...
def foo(func: Identity) -> None:
assert_type(func(0), int) # OK |
@ilevkivskyi Oh wow, I tried this earlier and it failed, seems I wrote |
I first encountered this issue with Python 3.6.7 and mypy 0.560 from my distribution's repositories, but it also occurs on Python 3.7 and mypy 0.701, and the current master.
This is the code in question:
It works, of course. But running mypy on it raises an error:
If I change it to the following code, there are no errors:
(This is a minimal example, my original code was something like
sum(map(abs, list_of_floats))
.)So, what's the problem here?
int
andfloat
have.__abs__
.The text was updated successfully, but these errors were encountered: