mypy icon indicating copy to clipboard operation
mypy copied to clipboard

mypy recognizes value type in nested dicts as object

Open amatyukhin0 opened this issue 3 years ago • 1 comments

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 avatar Aug 10 '22 06:08 amatyukhin0

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

KotlinIsland avatar Aug 11 '22 05:08 KotlinIsland