Skip to content

Return type of dict.get is wrong when using strict optional checking #1732

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
JukkaL opened this issue Jun 22, 2016 · 4 comments
Closed

Return type of dict.get is wrong when using strict optional checking #1732

JukkaL opened this issue Jun 22, 2016 · 4 comments
Labels
bug mypy got something wrong topic-strict-optional

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 22, 2016

The revealed type in the following program is int though it should be Optional[int] when using --strict-optional:

d = {'x': 1}
a = d.get('y')
reveal_type(a)

This also needs a typeshed change, but I think that there was a mypy issue with overloaded functions that is related. It's worth having a separate issue to track this here.

@JukkaL JukkaL added the bug mypy got something wrong label Jun 22, 2016
@gvanrossum
Copy link
Member

I think this is relevant: python/typeshed#223 (comment)

@gvanrossum gvanrossum changed the title Return type of dict get wrong then using strict optional checking Return type of dict.get is wrong when using strict optional checking Jun 23, 2016
@ambv
Copy link
Contributor

ambv commented Jan 12, 2017

Currently landed PR for this feature in typeshed: python/typeshed#822. Contains a Union[] return type so might have to be fixed to simple @overload.

@ilevkivskyi
Copy link
Member

It looks like this is fixed on master, I get:

Revealed type is 'Union[builtins.int*, builtins.None]'

@gvanrossum
Copy link
Member

(Also, if you pass an explicit default of a different type, it becomes a union of int and that type, regardless of the --strict-optional setting. IIRC that was a problem in the past too.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-strict-optional
Projects
None yet
Development

No branches or pull requests

5 participants