Skip to content

explicit type got from dict.get #1576

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

Merged
merged 1 commit into from
May 30, 2016
Merged

Conversation

tharvik
Copy link
Contributor

@tharvik tharvik commented May 25, 2016

When trying to fix Mapping and MutableMapping definition in mypy, I got an error stating that it can't add a List[None] to a List[Frame] (or List[Constaint]), having these comments allow it to work.

Not much related, shouldn't it be infered as a List[Any] instead?

@gvanrossum
Copy link
Member

Honestly I wish mypy was smart enough to figure out that the empty list in this case has the same type as the non-default list return value...

@rwbarton
Copy link
Contributor

Not much related, shouldn't it be infered as a List[Any] instead?

It inferred the type Union[List[Constraint], List[None]] for a, then it tried to check a.append(con) with a having each type from the union.

Honestly I wish mypy was smart enough to figure out that the empty list in this case has the same type as the non-default list return value...

This reminds me of the situation with x if b else y where you implemented some special logic to type check y with the inferred type of x, IIRC. Seems sensible to do something similar here but I don't know how we'd tell mypy to do that.

In principle mypy could determine that the [] must have type List[Constraint] from the usage on the next line, but that is pretty far outside its capabilities at the moment.

@gvanrossum
Copy link
Member

What I did for [...] if ... else [] was different -- there I play games with using either side as context for the other. That doesn't seem applicable here, at least it seems harder since the general case may be arbitrarily complex. I think it would be possible if [] came out with some type other than List[None] so that the unification code (I think in join.py) could special-case it safely (it currently can't know whether the List[None] came from [] or from actually writing e.g. [None, None]).

@tharvik
Copy link
Contributor Author

tharvik commented May 28, 2016

Not much related, shouldn't it be infered as a List[Any] instead?

It inferred the type Union[List[Constraint], List[None]] for a, then it tried to check a.append(con) with a having each type from the union.

I meant for [], that it should get infered a List[Any]. Or eventually a special case for an empty list agreeing to join with any other type of list.

@gvanrossum gvanrossum merged commit 2b53266 into python:master May 30, 2016
@tharvik tharvik deleted the explicit_dict_get branch July 11, 2016 15:43
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

Successfully merging this pull request may close these issues.

3 participants