Skip to content

[match-case] Fix narrowing of class pattern with union-argument. #19517

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

randolf-scholz
Copy link
Contributor

@randolf-scholz randolf-scholz commented Jul 27, 2025

Sibling PR to #19473, with some experimental changes.

  • refactored the conditional_types function.
    • return UninhabitedType(), default when no ranges are given. This corresponds to isinstance(x, ()), with an empty tuple, which always returns False at runtime.
  • modified the changes from [match-case] Fix narrowing of class pattern with union-argument. #19473 to modify the proposed type, rather than directly returning. This is essential to maintain the behavior of the unit test testIsinstanceWithOverlappingPromotionTypes
  • Added special casing in restrict_subtype_away: if the second argument is a TypeVar, replace it with its upper bound (crucial to get correct result in testNarrowSelfType)
  • Allow TypeChecker.get_isinstance_type to return empty list (fixes isinstance(x, ()) behavior).

Modified tests

Comment on lines +8006 to +8008
if not proposed_type_ranges:
# This is the case for `if isinstance(x, ())` which always returns False.
return UninhabitedType(), default
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is actually redundant, it works just as well if we comment it out since make_simplified_union([]) returns UninhabitedType().

This comment has been minimized.

@randolf-scholz randolf-scholz marked this pull request as ready for review July 27, 2025 19:32
Copy link
Contributor

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

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/tasks.py:559: error: Incompatible types in assignment (expression has type "Union[int, float, list[float], None]", variable has type "list[float]")  [assignment]
+ src/prefect/tasks.py:559: error: Incompatible types in assignment (expression has type "Union[float, int, list[float], None]", variable has type "list[float]")  [assignment]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/wrapper/_implementations.py:932: error: Incompatible return value type (got "type[object]", expected "DataClass_ | type[DataClass_] | ListConfig | DictConfig")  [return-value]

spark (https://github.com/apache/spark)
- python/pyspark/pandas/series.py:5116: error: Need type annotation for "cond"  [var-annotated]
- python/pyspark/pandas/series.py:5129: error: Incompatible types in assignment (expression has type "Column", variable has type "Never")  [assignment]
- python/pyspark/pandas/series.py:5131: error: Incompatible types in assignment (expression has type "Column", variable has type "Never")  [assignment]
- python/pyspark/pandas/series.py:5134: error: Incompatible types in assignment (expression has type "Column", variable has type "Never")  [assignment]
- python/pyspark/ml/connect/readwrite.py:100: error: Redundant cast to "JavaEstimator[Any]"  [redundant-cast]
- python/pyspark/ml/connect/readwrite.py:103: error: Redundant cast to "JavaEvaluator"  [redundant-cast]

sympy (https://github.com/sympy/sympy)
- sympy/polys/domains/domain.py:542: error: Cannot infer value of type parameter "Es" of "convert_from" of "Domain"  [misc]
- sympy/polys/domains/domain.py:549: error: Cannot infer value of type parameter "Es" of "convert_from" of "Domain"  [misc]
- sympy/polys/domains/domain.py:551: error: Cannot infer value of type parameter "Es" of "convert_from" of "Domain"  [misc]
- sympy/polys/domains/domain.py:584: error: Incompatible types in assignment (expression has type "Basic", variable has type "Es | Expr | complex")  [assignment]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy