-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 None
s without explicit annotation
#2225
Conversation
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.
97557ed
to
8643103
Compare
[case testOptionalNonPartialTypeWithNone] | ||
from typing import Generator | ||
def f() -> Generator[str, None, None]: pass | ||
x = f() |
There was a problem hiding this comment.
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()] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
Other than the notes above (and the merge conflict), LGTM! |
None
s without explicit annotationNone
s without explicit annotation
I'm going to drop this PR, as fixing #2246 should fix this too (and more cleanly, at that). |
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.
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.