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

Should collections containing None require type annotations? #2246

Closed
ddfisher opened this issue Oct 13, 2016 · 4 comments
Closed

Should collections containing None require type annotations? #2246

ddfisher opened this issue Oct 13, 2016 · 4 comments
Assignees

Comments

@ddfisher
Copy link
Collaborator

Currently, collections containing only None require type annotations (in strict Optional -- they're meaningless otherwise). For example:

x = [None]  # E: Need type annotation for variable

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.

@gvanrossum
Copy link
Member

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 Need type annotation for variable.

So I'm all for not making this an error and just inferring it as list-of-none.

@ddfisher
Copy link
Collaborator Author

ddfisher commented Oct 13, 2016

That said, this will work:

x = [None]
x.append(12)

We don't currently pay attention to assignments for figuring out partial types.

@gvanrossum
Copy link
Member

gvanrossum commented Oct 13, 2016 via email

@ddfisher ddfisher self-assigned this Oct 13, 2016
@JukkaL
Copy link
Collaborator

JukkaL commented Oct 14, 2016

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:

x = [None]
x.append(1)

@gvanrossum gvanrossum added this to the 0.4.x milestone Oct 20, 2016
gvanrossum pushed a commit that referenced this issue 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
Projects
None yet
Development

No branches or pull requests

3 participants