Use `typing.Self` to support subclassing
sebastianvitterso opened this issue · 0 comments
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()
:
Lines 185 to 214 in 87a1a77
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?