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

Skip to content

mypy recognizes value type in nested dicts as object #13374

Open
@amatyukhin0

Description

@amatyukhin0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions