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 |
Answered by
JelleZijlstra
Dec 4, 2024
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. |
0 replies
Answer selected by
Sanchoyzer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to 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.