-
-
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
mypy requires type annotations for empty lists even if they're never added #1164
Comments
Yeah, mypy should infer the type of |
+1 for this, have found the same is true for empty dicts. I think it would also be quite helpful if there was an option to make type-checking of variables optional, but this would be a separate issue I think. If such an issue does not already exist, I may create one to explore this idea. |
Why should we declare the type of container? Python container, such as list, set and dict, can have various types of data. We can store 'str' and 'int' to one list at the same time. Why mypy need that? |
Mypy needs to know the item types so that it can check that any operations on the items are valid. It also uses the types to check that you don't accidentally add values with invalid types. If you don't care about the types, you can use |
@JukkaL is there a setting to make List[Any] the default list type? |
@robertocarta You can try The original example no longer generates an error, so I'm closing this issue. If there are other cases which don't work as expected, feel free to open a new issue. |
Reproducer:
Error:
foo.py:12: error: Need type annotation for variable
It seems like this is a case where mypy can be 100% confident that a type annotation is pointless, and so it should probably not throw an error.
The text was updated successfully, but these errors were encountered: