Use typing_extensions.Self in the stdlib (#9694)

This commit is contained in:
Alex Waygood
2023-02-09 09:12:13 +00:00
committed by GitHub
parent 10086c06a1
commit 9ed39d8796
98 changed files with 627 additions and 654 deletions

View File

@@ -1,9 +1,8 @@
import sys
from _typeshed import Self
from abc import abstractmethod
from time import struct_time
from typing import ClassVar, NamedTuple, NoReturn, TypeVar, overload
from typing_extensions import Literal, TypeAlias, final
from typing_extensions import Literal, Self, TypeAlias, final
if sys.version_info >= (3, 11):
__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo", "MINYEAR", "MAXYEAR", "UTC")
@@ -50,18 +49,18 @@ class date:
min: ClassVar[date]
max: ClassVar[date]
resolution: ClassVar[timedelta]
def __new__(cls: type[Self], year: int, month: int, day: int) -> Self: ...
def __new__(cls, year: int, month: int, day: int) -> Self: ...
@classmethod
def fromtimestamp(cls: type[Self], __timestamp: float) -> Self: ...
def fromtimestamp(cls, __timestamp: float) -> Self: ...
@classmethod
def today(cls: type[Self]) -> Self: ...
def today(cls) -> Self: ...
@classmethod
def fromordinal(cls: type[Self], __n: int) -> Self: ...
def fromordinal(cls, __n: int) -> Self: ...
@classmethod
def fromisoformat(cls: type[Self], __date_string: str) -> Self: ...
def fromisoformat(cls, __date_string: str) -> Self: ...
if sys.version_info >= (3, 8):
@classmethod
def fromisocalendar(cls: type[Self], year: int, week: int, day: int) -> Self: ...
def fromisocalendar(cls, year: int, week: int, day: int) -> Self: ...
@property
def year(self) -> int: ...
@@ -82,16 +81,16 @@ class date:
def isoformat(self) -> str: ...
def timetuple(self) -> struct_time: ...
def toordinal(self) -> int: ...
def replace(self: Self, year: int = ..., month: int = ..., day: int = ...) -> Self: ...
def replace(self, year: int = ..., month: int = ..., day: int = ...) -> Self: ...
def __le__(self, __other: date) -> bool: ...
def __lt__(self, __other: date) -> bool: ...
def __ge__(self, __other: date) -> bool: ...
def __gt__(self, __other: date) -> bool: ...
if sys.version_info >= (3, 8):
def __add__(self: Self, __other: timedelta) -> Self: ...
def __radd__(self: Self, __other: timedelta) -> Self: ...
def __add__(self, __other: timedelta) -> Self: ...
def __radd__(self, __other: timedelta) -> Self: ...
@overload
def __sub__(self: Self, __other: timedelta) -> Self: ...
def __sub__(self, __other: timedelta) -> Self: ...
@overload
def __sub__(self, __other: datetime) -> NoReturn: ...
@overload
@@ -119,7 +118,7 @@ class time:
max: ClassVar[time]
resolution: ClassVar[timedelta]
def __new__(
cls: type[Self],
cls,
hour: int = ...,
minute: int = ...,
second: int = ...,
@@ -146,7 +145,7 @@ class time:
def __gt__(self, __other: time) -> bool: ...
def isoformat(self, timespec: str = ...) -> str: ...
@classmethod
def fromisoformat(cls: type[Self], __time_string: str) -> Self: ...
def fromisoformat(cls, __time_string: str) -> Self: ...
# On <3.12, the name of the parameter in the pure-Python implementation
# didn't match the name in the C implementation,
# meaning it is only *safe* to pass it as a keyword argument on 3.12+
@@ -160,7 +159,7 @@ class time:
def tzname(self) -> str | None: ...
def dst(self) -> timedelta | None: ...
def replace(
self: Self,
self,
hour: int = ...,
minute: int = ...,
second: int = ...,
@@ -178,7 +177,7 @@ class timedelta:
max: ClassVar[timedelta]
resolution: ClassVar[timedelta]
def __new__(
cls: type[Self],
cls,
days: float = ...,
seconds: float = ...,
microseconds: float = ...,
@@ -223,7 +222,7 @@ class datetime(date):
min: ClassVar[datetime]
max: ClassVar[datetime]
def __new__(
cls: type[Self],
cls,
year: int,
month: int,
day: int,
@@ -252,26 +251,26 @@ class datetime(date):
# meaning it is only *safe* to pass it as a keyword argument on 3.12+
if sys.version_info >= (3, 12):
@classmethod
def fromtimestamp(cls: type[Self], timestamp: float, tz: _TzInfo | None = ...) -> Self: ...
def fromtimestamp(cls, timestamp: float, tz: _TzInfo | None = ...) -> Self: ...
else:
@classmethod
def fromtimestamp(cls: type[Self], __timestamp: float, tz: _TzInfo | None = ...) -> Self: ...
def fromtimestamp(cls, __timestamp: float, tz: _TzInfo | None = ...) -> Self: ...
@classmethod
def utcfromtimestamp(cls: type[Self], __t: float) -> Self: ...
def utcfromtimestamp(cls, __t: float) -> Self: ...
if sys.version_info >= (3, 8):
@classmethod
def now(cls: type[Self], tz: _TzInfo | None = None) -> Self: ...
def now(cls, tz: _TzInfo | None = None) -> Self: ...
else:
@overload
@classmethod
def now(cls: type[Self], tz: None = None) -> Self: ...
def now(cls, tz: None = None) -> Self: ...
@overload
@classmethod
def now(cls, tz: _TzInfo) -> datetime: ...
@classmethod
def utcnow(cls: type[Self]) -> Self: ...
def utcnow(cls) -> Self: ...
@classmethod
def combine(cls, date: _Date, time: _Time, tzinfo: _TzInfo | None = ...) -> datetime: ...
def timestamp(self) -> float: ...
@@ -280,7 +279,7 @@ class datetime(date):
def time(self) -> _Time: ...
def timetz(self) -> _Time: ...
def replace(
self: Self,
self,
year: int = ...,
month: int = ...,
day: int = ...,
@@ -293,7 +292,7 @@ class datetime(date):
fold: int = ...,
) -> Self: ...
if sys.version_info >= (3, 8):
def astimezone(self: Self, tz: _TzInfo | None = ...) -> Self: ...
def astimezone(self, tz: _TzInfo | None = ...) -> Self: ...
else:
def astimezone(self, tz: _TzInfo | None = ...) -> datetime: ...
@@ -309,7 +308,7 @@ class datetime(date):
def __gt__(self, __other: datetime) -> bool: ... # type: ignore[override]
if sys.version_info >= (3, 8):
@overload # type: ignore[override]
def __sub__(self: Self, __other: timedelta) -> Self: ...
def __sub__(self, __other: timedelta) -> Self: ...
@overload
def __sub__(self: _D, __other: _D) -> timedelta: ...
else: