mypy detects a dict as an object #1896
-
I've noticed a bit of strange behaviour: mypy detects a dict as an object: def func_1() -> None:
i: dict[str, str | None]
for i in [{'a': 'qwerty'}, {'a': None}]: # error: Incompatible types in assignment (expression has type "object", variable has type "dict[Any, Any]") [assignment]
print(i.get('a'))
def func_2() -> None:
for i in [{'a': 'qwerty'}, {'a': None}]:
print(i.get('a')) # "object" has no attribute "get" [attr-defined]
def func_3() -> None:
my_list: list[dict[str, str | None]] = [{'a': 'qwerty'}, {'a': None}] # it's ok
for i in my_list:
print(i.get('a'))
Gist URL: https://gist.github.com/mypy-play/edab7242ef2538c33018bd2352baf38a And it's ok for Pyright |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is related to mypy's longstanding behavior of using the join operator instead of using unions: https://github.com/python/mypy/labels/topic-join-v-union . We'll likely change this eventually in at least some circumstances in mypy (python/mypy#12056), but it's a complex change to make. |
Beta Was this translation helpful? Give feedback.
This is related to mypy's longstanding behavior of using the join operator instead of using unions: https://github.com/python/mypy/labels/topic-join-v-union . We'll likely change this eventually in at least some circumstances in mypy (python/mypy#12056), but it's a complex change to make.