Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use typing.Self to support subclassing #1168

Open
sebastianvitterso opened this issue Dec 18, 2023 · 0 comments · May be fixed by #1174
Open

Use typing.Self to support subclassing #1168

sebastianvitterso opened this issue Dec 18, 2023 · 0 comments · May be fixed by #1174

Comments

@sebastianvitterso
Copy link

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():

arrow/arrow/arrow.py

Lines 185 to 214 in 87a1a77

@classmethod
def now(cls, tzinfo: Optional[dt_tzinfo] = None) -> "Arrow":
"""Constructs an :class:`Arrow <arrow.arrow.Arrow>` object, representing "now" in the given
timezone.
:param tzinfo: (optional) a ``tzinfo`` object. Defaults to local time.
Usage::
>>> arrow.now('Asia/Baku')
<Arrow [2019-01-24T20:26:31.146412+04:00]>
"""
if tzinfo is None:
tzinfo = dateutil_tz.tzlocal()
dt = dt_datetime.now(tzinfo)
return cls(
dt.year,
dt.month,
dt.day,
dt.hour,
dt.minute,
dt.second,
dt.microsecond,
dt.tzinfo,
fold=getattr(dt, "fold", 0),
)

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant