Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive [operator] on union of dicts with heterogeneous value types #18236

Open
harahu opened this issue Dec 3, 2024 · 3 comments
Open
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference

Comments

@harahu
Copy link

harahu commented Dec 3, 2024

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 where d5 is a type annotated attribute on an object, which results in the same error.

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

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 version used: 1.13.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12
@harahu harahu added the bug mypy got something wrong label Dec 3, 2024
@brianschubert brianschubert added the topic-type-context Type context / bidirectional inference label Dec 3, 2024
@Aarya2004
Copy link

Hey @harahu, I'd love to work on this! Thanks for including the code snippet, I'm looking through the code for ways to make a fix. Let me know if you have any possible solutions, I'll let you know if I have any updates!

@brianschubert
Copy link
Collaborator

@Aarya2004 Thanks for your interest in contributing to mypy! Unfortunately, this probably isn't a good issue for a first contribution. This issue has to do with they way mypy applies bidirectional inference, which a) is a very complex part of the code, and b) involves making tradeoffs that are tricky to get right and have a high risk of breaking behavior that mypy users rely on.

I'd recommend taking a look at issue tagged good-first-issue or good-second-issue , which should be a bit more approachable and are a good way to build up to larger issues. You can also check out CONTRIBUTING.md and the Developer Guides for contributing guidelines and tips on where to start.

@Aarya2004
Copy link

Thanks for the guidance @brianschubert. I'll take a look at that then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference
Projects
None yet
Development

No branches or pull requests

3 participants