From 8ae678129e58acc19e48a42ee9960f49fedb47ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Zago=C5=BEen?= Date: Tue, 5 Apr 2022 22:20:31 +0200 Subject: [PATCH] Use Self return type for the 'replace()' methods in datetime classes (#7595) --- stdlib/datetime.pyi | 82 ++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/stdlib/datetime.pyi b/stdlib/datetime.pyi index df33fb88f..18a4d2dd2 100644 --- a/stdlib/datetime.pyi +++ b/stdlib/datetime.pyi @@ -65,7 +65,12 @@ class date: def isoformat(self) -> str: ... def timetuple(self) -> struct_time: ... def toordinal(self) -> int: ... - def replace(self, year: int = ..., month: int = ..., day: int = ...) -> date: ... + if sys.version_info >= (3, 6): + def replace(self: Self, year: int = ..., month: int = ..., day: int = ...) -> Self: ... + else: + # Prior to Python 3.6, the `replace` method always returned `date`, even in subclasses + def replace(self, year: int = ..., month: int = ..., day: int = ...) -> date: ... + def __le__(self, __other: date) -> bool: ... def __lt__(self, __other: date) -> bool: ... def __ge__(self, __other: date) -> bool: ... @@ -139,16 +144,29 @@ class time: def utcoffset(self) -> timedelta | None: ... def tzname(self) -> str | None: ... def dst(self) -> timedelta | None: ... - def replace( - self, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: _tzinfo | None = ..., - *, - fold: int = ..., - ) -> time: ... + if sys.version_info >= (3, 6): + def replace( + self: Self, + hour: int = ..., + minute: int = ..., + second: int = ..., + microsecond: int = ..., + tzinfo: _tzinfo | None = ..., + *, + fold: int = ..., + ) -> Self: ... + else: + # Prior to Python 3.6, the `replace` method always returned `time`, even in subclasses + def replace( + self, + hour: int = ..., + minute: int = ..., + second: int = ..., + microsecond: int = ..., + tzinfo: _tzinfo | None = ..., + *, + fold: int = ..., + ) -> time: ... _date = date _time = time @@ -260,19 +278,35 @@ class datetime(date): def date(self) -> _date: ... def time(self) -> _time: ... def timetz(self) -> _time: ... - def replace( - self, - year: int = ..., - month: int = ..., - day: int = ..., - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: _tzinfo | None = ..., - *, - fold: int = ..., - ) -> datetime: ... + if sys.version_info >= (3, 6): + def replace( + self: Self, + year: int = ..., + month: int = ..., + day: int = ..., + hour: int = ..., + minute: int = ..., + second: int = ..., + microsecond: int = ..., + tzinfo: _tzinfo | None = ..., + *, + fold: int = ..., + ) -> Self: ... + else: + # Prior to Python 3.6, the `replace` method always returned `datetime`, even in subclasses + def replace( + self, + year: int = ..., + month: int = ..., + day: int = ..., + hour: int = ..., + minute: int = ..., + second: int = ..., + microsecond: int = ..., + tzinfo: _tzinfo | None = ..., + *, + fold: int = ..., + ) -> datetime: ... if sys.version_info >= (3, 8): def astimezone(self: Self, tz: _tzinfo | None = ...) -> Self: ... else: