microsoft / microsoft/pyright

Type inference fails for application of higher-order generic function

Open
#11,303 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Code or Screenshots**
I'm not sure how to describe it better than giving an example:

```python
import typing as tp

class Wrapper[T](tp.NamedTuple):
value: T

type IdFn[T] = tp.Callable[[T], T]

def foo[T](f: IdFn[Wrapper[T]]):
def _id_fn(x: T):
# Wrapper isn't important, we could even
# return x
return f(Wrapper(x)).value

return _id_fn

# Type is inferred correctly for foo
reveal_type(foo) # "(f: (Wrapper[T@foo]) -> Wrapper[T@foo]) -> ((x: T@foo) -> T@foo)"

def id_fn[T](x: T) -> T:
return x

id_fn_foo = foo(id_fn)
reveal_type(id_fn_foo) # Should be (x: T) -> T, is "(x: Unknown) -> T@foo"
```

**VS Code extension or command-line**
I am running through the pylance (version 2026.1.1) extension, python 3.12.12

Contributor guide

Open the contributing guide

Research direction

Reproduce the supplied Python snippet in Pyright or through the Pylance extension, then inspect the type-inference path for the higher-order generic call from foo(id_fn). Done means id_fn_foo is inferred as (x: T) -> T rather than using Unknown for the parameter.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.