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

Enhance typing with Self to support subclassing for multiple Python versions #1174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
overload,
)

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be added to requirements for 3.11 and below?

https://github.com/arrow-py/arrow/tree/master/requirements

Copy link
Member

@jadchaar jadchaar Jun 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A recent PR was merged to remove typing extensions for 3.8 and below. Mind rebasing this CR against the change and updating the requirements.txt to only pull it in for 3.9 and 3.10

reference PR - #1176


from dateutil import tz as dateutil_tz
from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -303,7 +308,7 @@ def utcfromtimestamp(cls, timestamp: Union[int, float, str]) -> "Arrow":
)

@classmethod
def fromdatetime(cls, dt: dt_datetime, tzinfo: Optional[TZ_EXPR] = None) -> "Arrow":
def fromdatetime(cls, dt: dt_datetime, tzinfo: Optional[TZ_EXPR] = None) -> Self:
"""Constructs an :class:`Arrow <arrow.arrow.Arrow>` object from a ``datetime`` and
optional replacement timezone.

Expand Down Expand Up @@ -597,7 +602,7 @@ def span(

return floor, ceil

def floor(self, frame: _T_FRAMES) -> "Arrow":
def floor(self, frame: _T_FRAMES) -> Self:
"""Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, representing the "floor"
of the timespan of the :class:`Arrow <arrow.arrow.Arrow>` object in a given timeframe.
Equivalent to the first element in the 2-tuple returned by
Expand All @@ -612,9 +617,9 @@ def floor(self, frame: _T_FRAMES) -> "Arrow":

"""

return self.span(frame)[0]
return cast(Self, self.span(frame)[0])

def ceil(self, frame: _T_FRAMES) -> "Arrow":
def ceil(self, frame: _T_FRAMES) -> Self:
"""Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, representing the "ceiling"
of the timespan of the :class:`Arrow <arrow.arrow.Arrow>` object in a given timeframe.
Equivalent to the second element in the 2-tuple returned by
Expand All @@ -629,7 +634,7 @@ def ceil(self, frame: _T_FRAMES) -> "Arrow":

"""

return self.span(frame)[1]
return cast(Self, self.span(frame)[1])

@classmethod
def span_range(
Expand Down Expand Up @@ -927,7 +932,7 @@ def imaginary(self) -> bool:

# mutation and duplication.

def clone(self) -> "Arrow":
def clone(self) -> Self:
"""Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, cloned from the current one.

Usage:
Expand All @@ -939,7 +944,7 @@ def clone(self) -> "Arrow":

return self.fromdatetime(self._datetime)

def replace(self, **kwargs: Any) -> "Arrow":
def replace(self, **kwargs: Any) -> Self:
"""Returns a new :class:`Arrow <arrow.arrow.Arrow>` object with attributes updated
according to inputs.

Expand Down Expand Up @@ -985,7 +990,7 @@ def replace(self, **kwargs: Any) -> "Arrow":

return self.fromdatetime(current)

def shift(self, **kwargs: Any) -> "Arrow":
def shift(self, **kwargs: Any) -> Self:
"""Returns a new :class:`Arrow <arrow.arrow.Arrow>` object with attributes updated
according to inputs.

Expand Down Expand Up @@ -1040,7 +1045,7 @@ def shift(self, **kwargs: Any) -> "Arrow":

return self.fromdatetime(current)

def to(self, tz: TZ_EXPR) -> "Arrow":
def to(self, tz: TZ_EXPR) -> Self:
"""Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, converted
to the target timezone.

Expand Down
Loading