GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix `meet_types` for literal and instance by randolf-scholz · Pull Request #19605 · python/mypy

Conversation

@randolf-scholz

@randolf-scholz randolf-scholz commented Aug 7, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Fixes meet_types gives unexpected results when meeting literal and Instance #19560
  • Fixes Specify behavior for Literal? types. #19625
  • Fixes Regression: false positive avoid redundant cast with literal union #19055

New Tests

  • JoinSuite.test_mixed_literal_types tests join between Literal? and other types.
  • MeetSuite.test_mixed_literal_types tests meet between Literal? and other types
  • TypeOpsSuite.test_simplified_union_with_mixed_str_literals2 tests simplified unions containing Literal? types
  • SubtypingSuite.test_literal tests subtype checks with Literal? types
  • RestrictionSuite: new test suite for testing the restrict_subtype_away method.
  • testJoinLiteralInstanceAndEnum tests join between Literal? and StrEnum

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz

randolf-scholz commented Aug 7, 2025

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

This comment has been minimized.

Sign in to view
1 similar comment
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz randolf-scholz mentioned this pull request Aug 7, 2025
meet_types gives unexpected results when meeting literal and Instance #19560
Open
@randolf-scholz

randolf-scholz commented Aug 7, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz
randolf-scholz force-pushed the meet_literal branch from 2f41cab to 35ec958 Compare August 8, 2025 18:02
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz randolf-scholz mentioned this pull request Aug 9, 2025
Specify behavior for Literal? types. #19625
Open
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz

randolf-scholz commented Aug 11, 2025

Copy link
Copy Markdown
Contributor Author

Repro for the pyinstrument change:

from typing import TypedDict, Literal

type AsyncMode = Literal["enabled", "disabled", "strict"]

class Options(TypedDict, total=False):
    async_mode: AsyncMode

def test(kwargs: Options, d: dict[Literal["async_mode"], AsyncMode]) -> None:
    reveal_type(kwargs)
    reveal_type(kwargs.get)
    reveal_type(d.get)
    reveal_type(kwargs.get("async_mode", "disabled"))
    # master: AsyncMode
    # feature: "Literal['enabled'] | Literal['strict'] | Literal['disabled']?"

I believe the problem here is not the new union schema ("x"? | "x" --> "x"? rather than "x"; see #19625), but that mypy is too simplistic about kwargs.get.

reveal_type(kwargs.get)
# mypy: Overload
#        def (str) -> object,
#        def (str, default: object) -> object,
#        def [_T] (str, default: _T`1) -> object)"
# pyright: Overload
#        def (k: Literal['async_mode']) -> (AsyncMode | None)
#        def [T] (k: Literal['async_mode'], default: T | AsyncMode) -> (T | AsyncMode)
#        def [T] (k: str) -> (Any | None), (k: str, default: Any | T) -> (Any | T)

Basically, we want here to pick the second signature shown by pyright, and use the minimal solution T=Never, or equivalently an overload of the form

def [T] (k: Literal['async_mode'], default: AsyncMode) -> (AsyncMode)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@randolf-scholz randolf-scholz mentioned this pull request Aug 11, 2025
Special case TypedDict.get #19639
Closed
@randolf-scholz
randolf-scholz force-pushed the meet_literal branch from 1fd8382 to 2059a1a Compare August 11, 2025 12:40
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz
randolf-scholz force-pushed the meet_literal branch from 2059a1a to 44b65ff Compare September 19, 2025 14:43
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz
randolf-scholz force-pushed the meet_literal branch from 44b65ff to 178e8f6 Compare December 15, 2025 11:30
@github-actions

This comment has been minimized.

Sign in to view
@github-actions github-actions Bot mentioned this pull request Dec 15, 2025
Check #19605 sterliakov/mypy-issues#229
Closed
@randolf-scholz
change behavior of literal? types
473fb48
randolf-scholz added a commit to randolf-scholz/mypy that referenced this pull request Dec 15, 2025
@randolf-scholz
added test for python#19605
cb6a089
@randolf-scholz
randolf-scholz force-pushed the meet_literal branch from 178e8f6 to cb6a089 Compare December 15, 2025 14:46
@github-actions

This comment has been minimized.

Sign in to view
1 similar comment
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz
fixed test after rebase
55456ba
@randolf-scholz
randolf-scholz force-pushed the meet_literal branch from c96d66e to 55456ba Compare December 16, 2025 15:57
@github-actions

This comment has been minimized.

Sign in to view
randolf-scholz added 2 commits December 17, 2025 16:27
@randolf-scholz
simplification
52b5a53
@randolf-scholz
simplification
b6abcbb
@github-actions

github-actions Bot commented Dec 17, 2025

Copy link
Copy Markdown
Contributor

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

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]

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/server/services/task_run_recorder.py:181: error: "Collection[Any]" has no attribute "state"  [attr-defined]
+ src/prefect/server/services/task_run_recorder.py:181: error: "object" has no attribute "state"  [attr-defined]
- src/prefect/server/services/task_run_recorder.py:184: error: "Collection[Any]" has no attribute "id"  [attr-defined]
+ src/prefect/server/services/task_run_recorder.py:184: error: "object" has no attribute "id"  [attr-defined]
- src/prefect/server/services/task_run_recorder.py:190: error: "Collection[Any]" has no attribute "keys"  [attr-defined]
+ src/prefect/server/services/task_run_recorder.py:190: error: "object" has no attribute "keys"  [attr-defined]
- src/prefect/concurrency/_asyncio.py:112: error: R? has no attribute "json"  [attr-defined]
+ 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], UUID | None, TaskRun | None, dict[str, Any] | None, PrefectFuture[Any] | Any | Iterable[PrefectFuture[Any] | Any] | None, Literal['state', 'result'], dict[str, set[RunInput]] | None, dict[str, Any] | None], R | State[Any] | None], Task[[VarArg(Any), KwArg(Any)], Any], UUID, TaskRun, dict[Any, Any], list[Any], str, Any | None], Any | State[Any] | None]"  [arg-type]

operator (https://github.com/canonical/operator)
+ ops/model.py:809: 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:811: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['tcp', 'udp', 'icmp']")  [assignment]

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]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@randolf-scholz

randolf-scholz commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

Resubmitted with clean history as #20444

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@randolf-scholz randolf-scholz closed this Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Specify behavior for Literal? types. meet_types gives unexpected results when meeting literal and Instance Regression: false positive avoid redundant cast with literal union

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@randolf-scholz
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.