Skip to content

Use both sides of type context for generic type inference (part 2) - #22394

Open
ilevkivskyi wants to merge 2 commits into
python:masterfrom
ilevkivskyi:new-inference-2
Open

ilevkivskyi wants to merge 2 commits into
python:masterfrom
ilevkivskyi:new-inference-2

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Ref #22063

Fixes #8478
Fixes #10281

This makes the new logic work in presence of second pass arguments (callables). I also did a little refactoring, and added some documenting comments.

Implementation is generally straightforward. I may add more tests depending on primer and mypy-issues.

@github-actions

This comment has been minimized.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

New error in discord.py looks like a new false positive, but I can't find an easy repro so far.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

OK, I found a repro

[case testTwoSidedInferenceWithParamSpecGeneric]
from typing import Callable, Generic, TypeVar, Union
from typing_extensions import ParamSpec

P = ParamSpec("P")
T = TypeVar("T")
I = TypeVar("I", bound=Union[int, str])

class C(Generic[T]): ...

def thing(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: ...

def test(x: C[I]) -> list[int]: ...
ci: C[int]
f: list[int] = thing(test, ci)
[builtins fixtures/paramspec.pyi]

This is however quite niche, it is related to weakness in how we calculate joins of "generic" Parameters. I will check if there is a simple fix.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

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

core (https://github.com/home-assistant/core)
- homeassistant/components/minecraft_server/api.py:95: error: Item "None" of "Any | None" has no attribute "timeout"  [union-attr]
+ homeassistant/components/fastdotcom/coordinator.py:34: error: Returning Any from function declared to return "dict[str, float] | None"  [no-any-return]

egglog-python (https://github.com/egraphs-good/egglog-python)
+ python/egglog/exp/array_api.py:69: error: Unused "type: ignore" comment  [unused-ignore]
+ python/egglog/exp/array_api.py:70: error: Unused "type: ignore" comment  [unused-ignore]

trio (https://github.com/python-trio/trio)
+ src/trio/_socket.py:1311: error: Unused "type: ignore" comment  [unused-ignore]
+ src/trio/_socket.py:1312: error: Argument 3 to "_nonblocking_helper" of "_SocketType" has incompatible type "*list[object]"; expected "Buffer"  [arg-type]
+ src/trio/_socket.py:1312: error: Argument 3 to "_nonblocking_helper" of "_SocketType" has incompatible type "*list[object]"; expected "tuple[Any, ...] | str | Buffer"  [arg-type]

@ilevkivskyi

Copy link
Copy Markdown
Member Author

The change in trio is actually good IMO, I think this is ready for review. @hauntsaninja @JukkaL please take a look if you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant