Skip to content

Use union of current context and left side for right side narrowing of binary ops #19249

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

Merged

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Jun 7, 2025

Fixes #12001. Fixes #6898. Fixes #15368. Improves #17790 and #11508.

When encountering a {and,or} b, we used to use a as primary context for b inference. This results in weird errors when a and b are completely unrelated, and in many cases return type/assignment type context can do much better. Inferring to union should be harmless in most cases, so use union of a and current context instead.

@sterliakov sterliakov changed the title Use union of current context and left side for right side narrowing Use union of current context and left side for right side narrowing of binary ops Jun 7, 2025

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@sterliakov sterliakov marked this pull request as ready for review June 11, 2025 23:55
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

discord.py (https://github.com/Rapptz/discord.py)
- discord/activity.py:384: error: Incompatible types in assignment (expression has type "Any | dict[Any, Any]", variable has type "ActivityAssets")  [assignment]

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Makes sense.

@JukkaL JukkaL merged commit 2ba79cb into python:master Jun 13, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants