Skip to content

Conversation

@randolf-scholz
Copy link
Contributor

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

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 on lines +8006 to +8193
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().

@github-actions

This comment has been minimized.

@randolf-scholz randolf-scholz marked this pull request as ready for review July 27, 2025 19:32
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
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]

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks!

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

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