Skip to content

Mapping.{get,pop} can return default type #278

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

Closed
gnprice opened this issue Jun 8, 2016 · 4 comments
Closed

Mapping.{get,pop} can return default type #278

gnprice opened this issue Jun 8, 2016 · 4 comments

Comments

@gnprice
Copy link
Contributor

gnprice commented Jun 8, 2016

See #223. That PR was reverted because it caused some type errors that it shouldn't, but it's the right thing to do and we should figure out how to make it work.

@gvanrossum , what did the errors you saw (after python/mypy#1595 was fixed) look like?

@rwbarton
Copy link
Contributor

See also python/mypy#738 and python/mypy#1114.

gvanrossum pushed a commit that referenced this issue Sep 15, 2016
Fixes #278.

The approach here is different from what we thought before.  According
to this commit, there are three valid forms of Mapping[K, V].get():

  d.get()      # returns Optional[V]
  d.get(None)  # also returns Optional[V]  (seen in the wild)
  d.get(v)     # requires v to have type V, and returns V

So we don't allow d.get(d) where d is a type unrelated to V.  This is
a compromise, but I don't think there's much use for d.get(d) with a
default that is differently-typed than V, and when I tried it my
application code (that didn't use this) wouldn't type-check correctly.
@euresti
Copy link
Contributor

euresti commented Mar 20, 2017

If I understand correctly this looks fixed by #822
@gnprice: Is it what you wanted?

@JelleZijlstra
Copy link
Member

I think it's fixed for get but not pop.

@euresti
Copy link
Contributor

euresti commented Mar 20, 2017

Oh yeah. And we use the pop version a lot. That sounds like something I can help out with.

euresti pushed a commit to euresti/typeshed that referenced this issue Mar 20, 2017
This is particularly useful for the idiom `d.pop(k, None)` to remove an item if it exists.

Fixes python#278
ambv pushed a commit that referenced this issue Mar 20, 2017
This is particularly useful for the idiom `d.pop(k, None)` to remove an item if it exists.

Fixes #278
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

4 participants