8000 Why do I need annotations for an empty list or dictionary · Issue #922 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Why do I need annotations for an empty list or dictionary #922

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

Closed
thomascellerier opened this issue Oct 13, 2015 · 7 comments
Closed

Why do I need annotations for an empty list or dictionary #922

thomascellerier opened this issue Oct 13, 2015 · 7 comments

Comments

@thomascellerier
Copy link

Every time I do

var = []

or

var = {}

mypy complains and I end up writing

var = []  # type: list

or

var = {} # type: dict

If I'm not specifying a more narrow type why does mypy require me to annotate the variables in a way that gives it no additional information. If I type [] then # type: list is just additional noise.

@refi64
Copy link
Contributor
refi64 commented Oct 13, 2015

Because a lot of people will guess "Hey, mypy infers the types of my lists!", while it really doesn't.

Crystal does the exact same thing.

@thomascellerier
Copy link
Author

From my point of view, if mypy is an optional type checker then it should stay out of the way unless you explicitly define types. I have no idea what the python3.5 spec says about this though.

@o11c
Copy link
Contributor
o11c commented Oct 13, 2015

IIRC mypy does stay out of your way until you start annotating a function, and then it complains for every single issue in the function.

The granularity is rather poor for global code however.

@JukkaL
Copy link
Collaborator
JukkaL commented Oct 14, 2015

There are two related things:

  1. Mypy complains about x = [] on the module top level. Yes, this should be fixed, via something called 'weak mode' (I'll write about this later). Basically the type should default to List[Any] if there is no annotation. There should also be an opt-in 'strong' or 'strict' mode which would be similar to how mypy works currently.
  2. Mypy complains about x = [] in an annotated function. Mypy could do a better job here, but in general mypy should complain if it can't infer the item type, as otherwise you have code that can't be type checked even though everything looks as if it is fully annotated.

@gvanrossum
Copy link
Member

See also #1055. For some simple cases involving local variables this has now been solved.

@thomascellerier
Copy link
Author

Great :) thanks for the fix.

@JukkaL
Copy link
Collaborator
JukkaL commented Apr 28, 2016

Closing this as this is resolved for most cases that we are planning to support. Feel free to file new issues for particular examples where type inference still fails.

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

5 participants
0