-
-
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
Should collections containing None require type annotations? #2246
Comments
It's especially annoying because this doesn't seem to work with partial types. E.g. this works: x = None
x = 12 but this doesn't: x = [None]
x[0] = 12 The latter still gives the error So I'm all for not making this an error and just inferring it as list-of-none. |
That said, this will work: x = [None]
x.append(12) We don't currently pay attention to assignments for figuring out partial types. |
We discussed this offline, and ended up deciding that the type of `[None]`
isjust `List[None]` (not a partial type), and if you want something else
you should add a type comment.
|
Sounds reasonable to me. Ultimately, as this will often result in an error, the important bit is to generate an understandable error message for code like this:
|
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, collections containing only
None
require type annotations (in strict Optional -- they're meaningless otherwise). For example:As @dmoisset mentioned in #2230, this can be confusing. We should consider whether or not we really want to require a type annotation. And if we do, we should consider finding a more understandable error message.
The text was updated successfully, but these errors were encountered: