False positive [operator]
on union of dicts with heterogeneous value types
#18236
Labels
[operator]
on union of dicts with heterogeneous value types
#18236
Bug Report
If I take the union of two dicts with different value types, I cannot assign the result to a variable that is annotated with the resulting type. Doing so causes an error. Assigning to an non-annotated variable does not cause an error, which seems like the correct behavior.
Do I really need the annotation on
d5
below? No. But I have a non-toy example of this in a code base whered5
is a type annotated attribute on an object, which results in the same error.To Reproduce
See: https://mypy-play.net/?mypy=master&python=3.13&flags=show-error-codes&gist=2643cb3ff1ea956a1c4ed0869140d46d
Expected Behavior
I expect
d5: dict[str, str | None] = d1 | d2
to be legal.Actual Behavior
error: Unsupported operand types for | ("dict[str, str]" and "dict[str, str | None]") [operator]
.Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: