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

None gets dropped in ternary statement with lists #2258

Closed
ddfisher opened this issue Oct 14, 2016 · 2 comments
Closed

None gets dropped in ternary statement with lists #2258

ddfisher opened this issue Oct 14, 2016 · 2 comments
Assignees

Comments

@ddfisher
Copy link
Collaborator

python3 -m mypy --strict-optional -c 'reveal_type([None] if bool() else [1])' <string>:1: error: Revealed type is 'builtins.list[builtins.int]'

@refi64
Copy link
Contributor

refi64 commented Oct 14, 2016

This is basically just a duplicate of #2246. is_valid_inferred_type is False for [None] since it's technically invalid without a type annotation, so visit_conditional_expr just discards the type of that branch. Pretty sure fixing #2246 would also fix this.

@ddfisher
Copy link
Collaborator Author

Nice catch! It looks that way to me too. This makes me feel like our decision on #2246 is definitely right.

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

2 participants