Skip to content

Fix meet_types for literal and instance #19605

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

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

randolf-scholz
Copy link
Contributor

@randolf-scholz randolf-scholz commented Aug 7, 2025

Fixes #19560

  • is_proper_subtype(Literal?, Literal) now always returns False; is_subtype retains original behavior.
  • updated meet_types with special casing to carry over last_known_value correctly.

Currently, one unit test still fails, as an "unreachable" with match-case is not raises

This comment has been minimized.

@randolf-scholz
Copy link
Contributor Author

The first operator failure seems like a bug in typeshed to me: python/typeshed#9004

This comment has been minimized.

This comment has been minimized.

@randolf-scholz
Copy link
Contributor Author

randolf-scholz commented Aug 7, 2025

Hm, the xarray redundant cast looks like this: mypy-playground

from typing import Literal, cast

type PDDatetimeUnitOptions = Literal["s", "ms", "ns"]

time_unit = cast(PDDatetimeUnitOptions, "s")  # [redundant-cast]

Is the current behavior of emitting redundant cast here intentional? It seems formally incorrect, since "s" should be Literal["s"]? and not Literal["s", "ms", "ns"], but maybe this was introduces as a convenience thing?

In comparison, pyright does not issue RedundantCast here, which aligns with my priors. pyright-playground

@randolf-scholz randolf-scholz marked this pull request as ready for review August 8, 2025 06:29
@randolf-scholz randolf-scholz marked this pull request as draft August 8, 2025 06:29

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Aug 9, 2025

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

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/task_worker.py:370: error: Argument 1 to "submit" of "Executor" has incompatible type "Callable[[Callable[_P, _T], **_P], _T]"; expected "Callable[[Callable[[Task[P, R], Optional[UUID], Optional[TaskRun], Optional[dict[str, Any]], Union[PrefectFuture[Any], Any, Iterable[Union[PrefectFuture[Any], Any]], None], Literal['state', 'result'], Optional[dict[str, set[RunInput]]], Optional[dict[str, Any]]], Union[R, State[Any], None]], Task[[VarArg(Any), KwArg(Any)], Any], UUID, TaskRun, dict[Any, Any], list[Any], str, Optional[Any]], Union[Any, State[Any], None]]"  [arg-type]

pyinstrument (https://github.com/joerick/pyinstrument)
- pyinstrument/context_manager.py:40: error: Argument 1 to "Profiler" has incompatible type "**dict[str, Literal['enabled', 'disabled', 'strict'] | float | bool | None]"; expected "float"  [arg-type]
+ pyinstrument/context_manager.py:40: error: Argument 1 to "Profiler" has incompatible type "**dict[str, Literal['enabled', 'strict'] | float | str | bool | None]"; expected "float"  [arg-type]
- pyinstrument/context_manager.py:40: error: Argument 1 to "Profiler" has incompatible type "**dict[str, Literal['enabled', 'disabled', 'strict'] | float | bool | None]"; expected "Literal['enabled', 'disabled', 'strict']"  [arg-type]
+ pyinstrument/context_manager.py:40: error: Argument 1 to "Profiler" has incompatible type "**dict[str, Literal['enabled', 'strict'] | float | str | bool | None]"; expected "Literal['enabled', 'disabled', 'strict']"  [arg-type]
- pyinstrument/context_manager.py:40: error: Argument 1 to "Profiler" has incompatible type "**dict[str, Literal['enabled', 'disabled', 'strict'] | float | bool | None]"; expected "bool | None"  [arg-type]
+ pyinstrument/context_manager.py:40: error: Argument 1 to "Profiler" has incompatible type "**dict[str, Literal['enabled', 'strict'] | float | str | bool | None]"; expected "bool | None"  [arg-type]

operator (https://github.com/canonical/operator)
+ ops/model.py:831: error: Unsupported operand types for - ("set[tuple[Literal['tcp', 'udp', 'icmp'], int | None]]" and "set[tuple[str, int] | tuple[Literal['tcp', 'udp', 'icmp'], int | None]]")  [operator]
+ ops/model.py:833: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['tcp', 'udp', 'icmp']")  [assignment]

strawberry (https://github.com/strawberry-graphql/strawberry)
- strawberry/codegen/query_codegen.py:476: error: Redundant cast to "Literal['query', 'mutation', 'subscription']"  [redundant-cast]

discord.py (https://github.com/Rapptz/discord.py)
- discord/app_commands/transformers.py:139: error: Incompatible types in assignment (expression has type "list[dict[str, Any]]", target has type "str | int")  [assignment]
+ discord/app_commands/transformers.py:139: error: Incompatible types in assignment (expression has type "list[dict[str, Any]]", target has type "bool | str | int")  [assignment]
- discord/app_commands/transformers.py:141: error: Incompatible types in assignment (expression has type "list[int]", target has type "str | int")  [assignment]
+ discord/app_commands/transformers.py:141: error: Incompatible types in assignment (expression has type "list[int]", target has type "bool | str | int")  [assignment]
- discord/app_commands/transformers.py:149: error: Incompatible types in assignment (expression has type "int | float", target has type "str | int")  [assignment]
+ discord/app_commands/transformers.py:149: error: Incompatible types in assignment (expression has type "int | float", target has type "bool | str | int")  [assignment]
- discord/app_commands/transformers.py:151: error: Incompatible types in assignment (expression has type "int | float", target has type "str | int")  [assignment]
+ discord/app_commands/transformers.py:151: error: Incompatible types in assignment (expression has type "int | float", target has type "bool | str | int")  [assignment]

dedupe (https://github.com/dedupeio/dedupe)
- dedupe/api.py:1547: error: Redundant cast to "Literal['match', 'distinct']"  [redundant-cast]

xarray (https://github.com/pydata/xarray)
- xarray/coding/times.py: note: In function "resolve_time_unit_from_attrs_dtype":
- xarray/coding/times.py:1420: error: Redundant cast to "Literal['s', 'ms', 'us', 'ns']"  [redundant-cast]
- xarray/coding/times.py:1433: error: Redundant cast to "Literal['s', 'ms', 'us', 'ns']"  [redundant-cast]
- xarray/coding/times.py: note: In member "decode" of class "CFTimedeltaCoder":
- xarray/coding/times.py:1537: error: Redundant cast to "Literal['s', 'ms', 'us', 'ns']"  [redundant-cast]

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.

meet_types gives unexpected results when meeting literal and Instance
1 participant