Runtime Access to Generic Types on Class Method
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Description
I would like to be able to access the type arguments of a class from its class methods.
To make this concrete, here is a generic instance:
import typing
import typing_inspect
T = typing.TypeVar("T")
class Inst(typing.Generic[T]):
@classmethod
def hi(cls):
return cls
I can get its generic args:
>>> typing_inspect.get_args(Inst[int])
(int,)
But, these seem to get erased inside the class methods:
>>> typing_inspect.get_args(Inst[int].hi())
()
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.
Research direction
Start with the Inst[int] and classmethod examples in the issue, then inspect the typing and typing_inspect behavior involved in resolving generic aliases at runtime. The work is done when a class method can access the type arguments of the specialized class without erasing them, with behavior demonstrated for the reported example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100