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: Remove None-related special-casing during inference with literal contexts by edgarrmondragon · Pull Request #22351 · python/mypy

Conversation

@edgarrmondragon

@edgarrmondragon edgarrmondragon commented Nov 2, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Fixes #22334

Old impl

When the return type is a bare type variable (or an optional one) and the type context is a union mixing literals (or sentinels, or enums) with other types, the context was used to solve the type variable before looking at the arguments. This could produce a spurious error if an argument was wider than the context:

def none_if_one[T](value: T | Literal[1]) -> T | None: ...

x: str | Literal[1] | None = 1
x = none_if_one(x)  # error: expected "str | Literal[1]"

Now, if using the literal context produces errors, retry inference from the arguments only, and use that if it type checks and the result is compatible with the context.

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
1 similar comment
@github-actions

This comment has been minimized.

Sign in to view
@ilevkivskyi

ilevkivskyi commented Nov 2, 2026

Copy link
Copy Markdown
Member

Sorry, but I think this is (very) wrong. IMO this issue should be solved by removing None-related special-casing in infer_function_type_arguments_using_context() after #22286 (and its equivalent for second pass) is merged. Only if that doesn't work for some reasons we can consider some hacks.

(FWIW I verified my idea locally and it works, but there is also primer...)

edgarrmondragon reacted with eyes emoji
All reactions
  • 👀 1 reaction

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
@edgarrmondragon

edgarrmondragon commented Nov 2, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

Sorry, but I think this is (very) wrong [...]

Ok, thanks for taking a look 🙇‍♂️. Removing the is_overlapping_none checks in your branch does make the tests in this PR pass and I couldn't find any obvious regression related to generics and literals/enums/sentinels.

All reactions

Sorry, something went wrong.

Uh oh!

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

@edgarrmondragon
edgarrmondragon force-pushed the 22334-generic-and-literal branch 2 times, most recently from 7f62eeb to 0eb0c1d Compare November 6, 2026 07:42
@edgarrmondragon
fix: Remove None-related special-casing during inference with literal…
c353380
… contexts

Signed-off-by: Edgar Ramírez Mondragón <[email protected]>
@edgarrmondragon
edgarrmondragon force-pushed the 22334-generic-and-literal branch from 0eb0c1d to c353380 Compare November 6, 2026 07:42
@edgarrmondragon edgarrmondragon changed the title Fall back to argument-only inference when literal context is too narrow fix: Remove None-related special-casing during inference with literal contexts Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

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

scrapy (https://github.com/scrapy/scrapy)
+ tests/test_crawler.py:230: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/test_crawler.py:230: error: Argument 1 to "get_addon" of "Crawler" has incompatible type "type[DefaultSpider]"; expected "type[TrackingAddon] | type[None]"  [arg-type]
+ tests/test_crawler.py:230: note: Error code "arg-type" not covered by "type: ignore[assignment]" comment
+ tests/test_crawler.py:233: error: Argument 1 to "get_addon" of "Crawler" has incompatible type "type[ParentAddon]"; expected "type[TrackingAddon] | type[None]"  [arg-type]

discord.py (https://github.com/Rapptz/discord.py)
+ discord/state.py:525: error: Item "None" of "Message | None" has no attribute "id"  [union-attr]
+ discord/message.py:576: error: Item "None" of "Message | None" has no attribute "created_at"  [union-attr]
+ discord/message.py:577: error: Item "None" of "Message | None" has no attribute "edited_at"  [union-attr]
+ discord/message.py:578: error: Item "None" of "Message | None" has no attribute "content"  [union-attr]
+ discord/message.py:579: error: Item "None" of "Message | None" has no attribute "embeds"  [union-attr]
+ discord/message.py:580: error: Item "None" of "Message | None" has no attribute "components"  [union-attr]
+ discord/message.py:581: error: Item "None" of "Message | None" has no attribute "stickers"  [union-attr]
+ discord/message.py:582: error: Item "None" of "Message | None" has no attribute "attachments"  [union-attr]
+ discord/message.py:583: error: Item "None" of "Message | None" has no attribute "flags"  [union-attr]
- discord/ext/commands/converter.py:269: error: Incompatible types in assignment (expression has type "Member | User | None", variable has type "Member | None")  [assignment]
+ discord/ext/commands/converter.py:269: error: Argument 1 has incompatible type "list[User | Member]"; expected "Iterable[Member | None]"  [arg-type]
+ discord/ext/commands/converter.py:321: error: Argument 1 has incompatible type "list[User | Member]"; expected "Iterable[User | None]"  [arg-type]
+ discord/ext/commands/converter.py:328: error: Unused "type: ignore" comment  [unused-ignore]
All reactions

Sorry, something went wrong.

Uh oh!

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

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.

Sentinel and enum type merge issues with generic

Uh oh!

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

2 participants

@edgarrmondragon @ilevkivskyi
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 01402503 — 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.