python / python/typing

Generic ParamSpec in subclass definitions

Open
#1,405 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

Given some base class that is generic over a param-spec, I'd like to be able to define the param-spec using a subclass method implementation. Something like:

T = TypeVar("T")
P = ParamSpec("P")

class Base(Generic[P, T]):
    func: Callable[P, T]
    # for instance
    def wrapper(self, *args: P.args, **kwargs: P.kwargs) -> Tuple[T]:
        return (self.func(*args, **kwargs), )
        
class Subclass(Base):
    def func(self, x: int, *, y: str) -> bytes: ...

That is, the base class is generic over some function definition, and the subclass implement that function as a method. I'm not sure how common this pattern is, but I see it a fair amount when the class gets more complicated and you can't just use a decorator (e.g. torch.nn.Module.forward is a big example of this). Is there some other way of specifying "infer the parameters from this method implementation"? Is this something that would require an update to the specification, is it "just" a feature request to the method implementation "like" an assignment statement?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or implementation entry point is named. Start by reviewing the issue's ParamSpec, Generic, and subclass-method example alongside the Python typing specification; determine whether the requested inference is supported or requires a specification change. Done means the behavior and its implementation scope are explicitly decided.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.