Skip to content

Fall back to argument-only inference when literal context is too narrow - #22351

Draft
edgarrmondragon wants to merge 3 commits into
python:masterfrom
edgarrmondragon:22334-generic-and-literal
Draft

edgarrmondragon wants to merge 3 commits into
python:masterfrom
edgarrmondragon:22334-generic-and-literal

Conversation

@edgarrmondragon

@edgarrmondragon edgarrmondragon commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

Fixes #22334

When the return type is a bare type variable (or an optional one) and the
type context is a union mixing literals 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:

```python
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.

Fixes python#22334

Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
@ilevkivskyi

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...)

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@edgarrmondragon

edgarrmondragon commented Nov 2, 2026 •

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.

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.

Sentinel and enum type merge issues with generic

2 participants