Use `typing.Self` to support subclassing
- Dominant language
- Python
- Stars
- 9.1k
- Forks
- 784
- PR merge metrics
- No merged PRs in 30d
Description
## Feature Request
We've implemented our own subclass of the `arrow.Arrow` class, to directly support pydantic serialization. This helps us in our use case, but we're having some troubles with the typing system, as (virtually) every function in the `arrow.Arrow` class returns `-> "Arrow"`, meaning we have to create a wrapper for every function we want to use in our subclass.
**Example of `-> "Arrow"` in `Arrow.now()`:**
https://github.com/arrow-py/arrow/blob/87a1a774aad0505d9da18ad1d16f6e571f262503/arrow/arrow.py#L185-L214
By replacing these (where correct) `-> "Arrow"` instances with `-> typing.Self`, all subclasses will have their inherited functionality correctly typed automatically, instead of this having to be done via wrapper functions.
---
### Caveats
I'm not sure there are many, but there is one:
`Self` wasn't introduced into the `typing` module until Python 3.11, meaning this might cause the support chart to look quite differently. It could, however, be possible to use `typing_extensions.Self` for backwards compatibility?
Contributor guide
No contributing guide indexed for this repository
Research direction
Review arrow.py, starting with the Arrow.now() example around lines 185-214, and identify the other return annotations that should preserve subclass types. Check the project's supported Python versions before choosing between typing.Self and typing_extensions.Self; the work is done when applicable Arrow methods type inherited subclass returns correctly without breaking the support chart.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100