-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[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
[match-case] Fix narrowing of class pattern with union-argument. #19517
Conversation
| if not proposed_type_ranges: | ||
| # This is the case for `if isinstance(x, ())` which always returns False. | ||
| return UninhabitedType(), default |
There was a problem hiding this comment.
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.
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 comment has been minimized.
This comment has been minimized.
4f8d34a to
8b92fcc
Compare
|
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]
|
hauntsaninja
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
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)