Incorrect type check for nested unions of dicts
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
For all types X and Y, if a type checks correctly as X it should also check correctly as Union[X, Y].
This is not the case for at least one specific combination of unions and dicts. See reproduction below.
To Reproduce
from typing import Dict, Union
A = Dict[str, Union[str, Dict[str, str]]]
B = Union[A, Dict[str, str]]
example1: A = {"x": {"y": "z"}}
example2: B = {"x": {"y": "z"}}
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=c72e66a8600324e7b1c1af24a0c533ca
Expected Behavior
example1 and example2 type check as valid.
Actual Behavior
example2 fails to type check with:
main.py:7: error: Incompatible types in assignment (expression has type "Dict[str, Dict[str, str]]", variable has type "Union[Dict[str, Union[str, Dict[str, str]]], Dict[str, str]]") [assignment]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Python 3.10 and 3.11.
Mypy 1.3.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Python reproduction and comparing the behavior of the two assignments in the linked mypy playground. Trace the type-checking path for nested unions containing dicts; done means both example1 and example2 type check without the reported assignment error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100