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

[conflict] Allow assignment of types with Nones without explicit annotation #2225

Closed
wants to merge 1 commit into from

Conversation

ddfisher
Copy link
Collaborator

@ddfisher ddfisher commented Oct 7, 2016

Currently, mypy will not infer types containing None unless they are
valid PartialTypes (i.e. are bare None, list, set, or dict). This means
that all assignments of such types must be explicitly annotated.
It doesn't make sense to require this with strict Optional, as None is a
perfectly valid type. This PR makes types containing None valid
inference targets.

Fixes #2195.

Currently, mypy will not infer types containing None unless they are
valid PartialTypes (i.e. are bare None, list, set, or dict). This means
that all assignments of such types must be explicitly annotated.
It doesn't make sense to require this with strict Optional, as None is a
perfectly valid type.  This PR makes types containing None valid
inference targets.

Fixes #2195.
[case testOptionalNonPartialTypeWithNone]
from typing import Generator
def f() -> Generator[str, None, None]: pass
x = f()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reveal the type of x.

from typing import Generator
def f() -> Generator[str, None, None]: pass
x = f()
l = [f()]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reveal the type of l.

if not is_valid_inferred_type(arg):
return False
fullname = typ.type.fullname()
if ((fullname == 'builtins.list' or
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that these are special cased as a convenience, as collections with all None types are rarely useful and thus we require them to be annotated, but for other generic types all None args may be totally reasonable. This seems pretty reasonable to me, but it's worth adding a comment here about why we special case just these types.

Also, can you add tests for the special casing?

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 11, 2016

Other than the notes above (and the merge conflict), LGTM!

@gvanrossum gvanrossum changed the title Allow assignment of types with Nones without explicit annotation [conflict] Allow assignment of types with Nones without explicit annotation Oct 13, 2016
@ddfisher
Copy link
Collaborator Author

I'm going to drop this PR, as fixing #2246 should fix this too (and more cleanly, at that).

@ddfisher ddfisher closed this Oct 17, 2016
@ddfisher ddfisher deleted the generator-explicit-type branch October 17, 2016 21:49
gvanrossum pushed a commit that referenced this pull request Oct 22, 2016
This means mypy will no longer give Need type annotation for variable errors for assignments with values of type Generator[str, None, None] or List[None] or similar. However, lists, sets, and dicts that are initialized with None values that were previously inferred may now need type annotations. I.e. constructs that look like:

x = [None]
x.append(0)

will now be type errors unless they have explicit annotations.

Supplants #2225. Fixes #2246. Fixes #2195. Fixes #2258.
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.

2 participants