-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
OrderedDict() needs type annotation but {} doesn't #4551
Comments
Since mypy doesn't know the type of the ordered dict when you create it, you need to add a type annotation. |
Oops ... the problem I was trying to report (about slots) turned out to be due to having As for OrderedDict ... shouldn't it work? When I change the code to |
The issue with |
Mypy has custom type inference rules for common collection types such as |
The example below (updated to have type information to express subclasses will have import collections
from typing import Any, Dict, Text
class PlainOldData:
__slots__: tuple[str, ...]
def as_json_dict(self) -> Dict[Text, Any]:
result = collections.OrderedDict()
for k in self.__slots__:
value = getattr(self, k)
if value is not None:
result[k] = value
return result passes with the latest mypy, closing. |
The following error message is triggered when by adding
-> Dict[Text, Any]
to the type signature:This is the contents of
/tmp/pod.py
:The text was updated successfully, but these errors were encountered: