Generic ParamSpec in subclass definitions
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
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.
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?
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
canonical/paas-charm#368 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
tech debt
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
StevenBlack/hosts#3256 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
qualcomm/qai-appbuilder#275 ·