Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Union of dicts with heterogeneous value types #1895

Answered by erictraut
harahu asked this question in Q&A
Discussion options

I'm running into a type checking error where I'm unsure whether what I'm looking at is an issue in mypy or an issue in my understanding of python type annotations.

See: https://mypy-play.net/?mypy=master&python=3.13&flags=show-error-codes&gist=2643cb3ff1ea956a1c4ed0869140d46d

d1: dict[str, str] = {}
d2: dict[str, str | None] = {}
d3 = d1 | d2
reveal_type(d3) # builtins.dict[builtins.str, Union[builtins.str, None]], aka dict[str, str | None]
# This is fine
d4: dict[str, str | None] = d3 # aka d1 | d2
# This isn't?
d5: dict[str, str | None] = d1 | d2

I cannot figure out why I'm not allowed to do d5: dict[str, str | None] = d1 | d2 in this case.

You must be logged in to vote

I think this is an issue with mypy. The code looks fine to me, and pyright accepts it without any errors.

Replies: 1 comment

Comment options

I think this is an issue with mypy. The code looks fine to me, and pyright accepts it without any errors.

You must be logged in to vote
0 replies
Answer selected by harahu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /