diff --git a/arrow/arrow.py b/arrow/arrow.py index 8d329efd..d75f41ae 100644 --- a/arrow/arrow.py +++ b/arrow/arrow.py @@ -29,6 +29,11 @@ overload, ) +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self + from dateutil import tz as dateutil_tz from dateutil.relativedelta import relativedelta @@ -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 ` object from a ``datetime`` and optional replacement timezone. @@ -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 ` object, representing the "floor" of the timespan of the :class:`Arrow ` object in a given timeframe. Equivalent to the first element in the 2-tuple returned by @@ -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 ` object, representing the "ceiling" of the timespan of the :class:`Arrow ` object in a given timeframe. Equivalent to the second element in the 2-tuple returned by @@ -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( @@ -927,7 +932,7 @@ def imaginary(self) -> bool: # mutation and duplication. - def clone(self) -> "Arrow": + def clone(self) -> Self: """Returns a new :class:`Arrow ` object, cloned from the current one. Usage: @@ -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 ` object with attributes updated according to inputs. @@ -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 ` object with attributes updated according to inputs. @@ -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 ` object, converted to the target timezone.