Improve type hinting for `ContextDecorator` and `AsyncContextDecorator`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
I recently ran into the problem described in https://stackoverflow.com/questions/62703400/python-how-to-type-hint-a-callable-with-wrapped, where MyPy complained when I attempted to access __wrapped__ on a function decorated with a ContextDecorator subclass.
The function signature is handled correctly (as per #4399), but the fact ContextDecorator.__call__ necessarily adds the __wrapped__ attribute to the returned wrapper is lost.
The functools stub does have some machinery for accurately typing functools.wraps itself, but there isn't anything that could be readily used to adjust the ContextDecorator.__call__ signature.
Ideally something like the following would be possible:
def __call__(self, func: _F) -> _WrappedCallable[_F]: ...
However, I'm not sure how _WrappedCallable could be expressed as a generic type - the corresponding protocol in functools.pyi accepts the call parameters and return type as separate type variables.
Contributor guide
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 by examining the ContextDecorator and AsyncContextDecorator stubs alongside the _WrappedCallable machinery in functools.pyi. Determine how call can preserve the wrapped callable's signature and expose wrapped, then add coverage showing both decorators retain that attribute under MyPy.
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
- 25/100