Thanks to visit codestin.com
Credit goes to github.com

Skip to content

mypy recognizes value type in nested dicts as object #13374

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

Open
amatyukhin0 opened this issue Aug 10, 2022 · 1 comment
Open

mypy recognizes value type in nested dicts as object #13374

amatyukhin0 opened this issue Aug 10, 2022 · 1 comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@amatyukhin0
Copy link

Bug Report

In some cases (see code below) mypy recognizes type of values in nested dict as object. Adding hint with more exact type results in an error.

To Reproduce

from typing import Union

key = 'foo'
x: dict[str, Union[str, bool]] = {
    'foo': {
        'key1': 'value',
        'key2': True,
    },
    'bar': {
        'key3': 'value',
        'key4': False,
    },
}[key]

Expected Behavior

mypy should not raise any error.

Actual Behavior

main.py:4: error: Incompatible types in assignment (expression has type "Dict[str, object]", variable has type "Dict[str, Union[str, bool]]")
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.971
  • Python version used: 3.11
  • Operating system: Linux
@amatyukhin0 amatyukhin0 added the bug mypy got something wrong label Aug 10, 2022
@JelleZijlstra JelleZijlstra added the topic-join-v-union Using join vs. using unions label Aug 10, 2022
@KotlinIsland
Copy link
Contributor

Have you tried basedmypy 1.5? It seems to handle this case correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

3 participants