FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[match-case] Fix narrowing of class pattern with union-argument. by randolf-scholz · Pull Request #19517 · python/mypy · GitHub

Repository navigation

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

Merged
hauntsaninja merged 1 commit into
python:masterfrom
randolf-scholz:fix_match_callable_2
Oct 23, 2025
Merged

hauntsaninja merged 1 commit into
python:masterfrom
randolf-scholz:fix_match_callable_2

Conversation

randolf-scholz commented Jul 27, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

Fixes #19468

Based on earlier PR #19473

  • refactored the conditional_types function.
  • modified [match-case] Fix narrowing of class pattern with union-argument. #19473 to change the proposed type, rather than directly returning. This is essential to maintain the behavior of the unit test testIsinstanceWithOverlappingPromotionTypes
  • Allow TypeChecker.get_isinstance_type to return empty list (fixes isinstance(x, ()) behavior).

Modified tests

New Tests

Comment thread mypy/checker.py
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 marked this pull request as ready for review July 27, 2025 19:32

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.

Copy link
Copy Markdown
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]

hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks!

hauntsaninja merged commit fb16e93 into python:master Oct 23, 2025
20 checks passed
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[match-case] class pattern narrowing with Union-argument different from equivalent isinstance call.

2 participants


Back | FazBrowse Home | New Git URL