Skip to content
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

operator.itemgetter can accept any hashable type #558

Closed
berdario opened this issue Sep 19, 2016 · 4 comments
Closed

operator.itemgetter can accept any hashable type #558

berdario opened this issue Sep 19, 2016 · 4 comments

Comments

@berdario
Copy link
Contributor

python/mypy#2153

basically, the following is valid Python code:

>>> itemgetter(Ellipsis)({Ellipsis: -1})
-1

in my case I got the error message with a more mundane string key, thus yielding this error:

 No overload variant of "itemgetter" matches argument types [builtins.str]
@berdario berdario changed the title operator.temgetter can accept any hashable type operator.itemgetter can accept any hashable type Sep 19, 2016
@gvanrossum
Copy link
Member

Oh, there are lots of interesting things we could do here. Maybe it should be a generic function, e.g.

@overload
def itemgetter(item: int) -> Callable[[Sequence[T]], T]: ...
@overload
def itemgetter(item: K) -> Callable[[Mapping[K, V], V]: ...

except the latter doesn't work yet because it introduces a type variable in the (callable) return type (I think the reason is related to python/mypy#1927). The *items case is even more interesting, see python/typing#239.

So maybe for now we need something with a lots of Any (or for the arguments object) and a TODO comment.

@berdario
Copy link
Contributor Author

berdario commented Sep 19, 2016

Even if it would work, doesn't overload resolution rely only on the input arguments?

In that case

itemgetter(1)({1: 42})

would fail to typecheck, due to the the returned function expecting a Sequence, not a Mapping (unless Sequence[X] are Mapping[int, X] ?)

@gvanrossum
Copy link
Member

Yeah, that's one of the challenges. It needs something that supports
getitem but there's no ABC for that...

--Guido (mobile)

@gvanrossum
Copy link
Member

Fixed by #564. (Note: if you include "Fixes #558" in the body of your commit message it will auto-close the issue.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants