Skip to content

Commit 8ae6781

Browse files
authored
Use Self return type for the 'replace()' methods in datetime classes (#7595)
1 parent 3941e95 commit 8ae6781

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

stdlib/datetime.pyi

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ class date:
6565
def isoformat(self) -> str: ...
6666
def timetuple(self) -> struct_time: ...
6767
def toordinal(self) -> int: ...
68-
def replace(self, year: int = ..., month: int = ..., day: int = ...) -> date: ...
68+
if sys.version_info >= (3, 6):
69+
def replace(self: Self, year: int = ..., month: int = ..., day: int = ...) -> Self: ...
70+
else:
71+
# Prior to Python 3.6, the `replace` method always returned `date`, even in subclasses
72+
def replace(self, year: int = ..., month: int = ..., day: int = ...) -> date: ...
73+
6974
def __le__(self, __other: date) -> bool: ...
7075
def __lt__(self, __other: date) -> bool: ...
7176
def __ge__(self, __other: date) -> bool: ...
@@ -139,16 +144,29 @@ class time:
139144
def utcoffset(self) -> timedelta | None: ...
140145
def tzname(self) -> str | None: ...
141146
def dst(self) -> timedelta | None: ...
142-
def replace(
143-
self,
144-
hour: int = ...,
145-
minute: int = ...,
146-
second: int = ...,
147-
microsecond: int = ...,
148-
tzinfo: _tzinfo | None = ...,
149-
*,
150-
fold: int = ...,
151-
) -> time: ...
147+
if sys.version_info >= (3, 6):
148+
def replace(
149+
self: Self,
150+
hour: int = ...,
151+
minute: int = ...,
152+
second: int = ...,
153+
microsecond: int = ...,
154+
tzinfo: _tzinfo | None = ...,
155+
*,
156+
fold: int = ...,
157+
) -> Self: ...
158+
else:
159+
# Prior to Python 3.6, the `replace` method always returned `time`, even in subclasses
160+
def replace(
161+
self,
162+
hour: int = ...,
163+
minute: int = ...,
164+
second: int = ...,
165+
microsecond: int = ...,
166+
tzinfo: _tzinfo | None = ...,
167+
*,
168+
fold: int = ...,
169+
) -> time: ...
152170

153171
_date = date
154172
_time = time
@@ -260,19 +278,35 @@ class datetime(date):
260278
def date(self) -> _date: ...
261279
def time(self) -> _time: ...
262280
def timetz(self) -> _time: ...
263-
def replace(
264-
self,
265-
year: int = ...,
266-
month: int = ...,
267-
day: int = ...,
268-
hour: int = ...,
269-
minute: int = ...,
270-
second: int = ...,
271-
microsecond: int = ...,
272-
tzinfo: _tzinfo | None = ...,
273-
*,
274-
fold: int = ...,
275-
) -> datetime: ...
281+
if sys.version_info >= (3, 6):
282+
def replace(
283+
self: Self,
284+
year: int = ...,
285+
month: int = ...,
286+
day: int = ...,
287+
hour: int = ...,
288+
minute: int = ...,
289+
second: int = ...,
290+
microsecond: int = ...,
291+
tzinfo: _tzinfo | None = ...,
292+
*,
293+
fold: int = ...,
294+
) -> Self: ...
295+
else:
296+
# Prior to Python 3.6, the `replace` method always returned `datetime`, even in subclasses
297+
def replace(
298+
self,
299+
year: int = ...,
300+
month: int = ...,
301+
day: int = ...,
302+
hour: int = ...,
303+
minute: int = ...,
304+
second: int = ...,
305+
microsecond: int = ...,
306+
tzinfo: _tzinfo | None = ...,
307+
*,
308+
fold: int = ...,
309+
) -> datetime: ...
276310
if sys.version_info >= (3, 8):
277311
def astimezone(self: Self, tz: _tzinfo | None = ...) -> Self: ...
278312
else:

0 commit comments

Comments
 (0)