[match-case] Fix narrowing of class pattern with union-argument.#19517
Merged
hauntsaninja merged 1 commit intopython:masterfrom Oct 23, 2025
Merged
[match-case] Fix narrowing of class pattern with union-argument.#19517hauntsaninja merged 1 commit intopython:masterfrom
hauntsaninja merged 1 commit intopython:masterfrom
Conversation
randolf-scholz
commented
Jul 27, 2025
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 |
Contributor
Author
There was a problem hiding this comment.
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This was referenced Oct 6, 2025
Closed
This comment has been minimized.
This comment has been minimized.
4f8d34a to
8b92fcc
Compare
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: spark (https://github.com/apache/spark)
- 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]
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]
starlette (https://github.com/encode/starlette)
+ starlette/middleware/errors.py:176: error: Incompatible types in assignment (expression has type "Any | None", variable has type "Response") [assignment]
+ starlette/middleware/errors.py:176: error: Argument 1 has incompatible type "Request"; expected "WebSocket" [arg-type]
+ starlette/_exception_handler.py:59: error: Argument 1 has incompatible type "Request | WebSocket"; expected "WebSocket" [arg-type]
- starlette/_exception_handler.py:61: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request | WebSocket, Exception], Any]" [arg-type]
+ starlette/_exception_handler.py:61: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request | WebSocket, Exception], Any | None]" [arg-type]
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/wrapper/_implementations.py:933: error: Incompatible return value type (got "type[object]", expected "DataClass_ | type[DataClass_] | ListConfig | DictConfig") [return-value]
urllib3 (https://github.com/urllib3/urllib3)
+ src/urllib3/_collections.py:359: error: Redundant cast to "Iterable[tuple[str, str]]" [redundant-cast]
websockets (https://github.com/aaugustin/websockets)
+ src/websockets/legacy/protocol.py:641: error: Redundant cast to "Iterable[str | bytes | bytearray | memoryview[int]]" [redundant-cast]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19468
Based on earlier PR #19473
conditional_typesfunction.UninhabitedType(), defaultwhen no ranges are given. This corresponds toisinstance(x, ()), with an empty tuple, which always returnsFalseat runtime.isinstance(A|B, C) -> yes = A_yes | B_yes(note: this does not apply to the no-type generally, see [match-case] Fix narrowing of class pattern with union-argument. #19473 (comment)testIsinstanceWithOverlappingPromotionTypesTypeChecker.get_isinstance_typeto return empty list (fixesisinstance(x, ())behavior).Modified tests
testIsInstanceWithEmtpy2ndArgnow correctly infers unreachable forisinstance(x, ()).testNarrowingUnionMixinsnow predicts the same results as pyright playground, https://mypy-play.net/?mypy=latest&python=3.12&gist=734c95f128705cfb0dc4ce24f1ad8eecNew Tests
testMatchNarrowDownUnionUsingClassPattern(https://mypy-play.net/?mypy=1.17.0&python=3.12&gist=e9ec514f49903022bd32a82ae1774abd)