Have datetime.{date,datetime} define __new__ instead of __init__. (#3829)

This is more faithful to the implementation:
https://github.com/python/cpython/blob/3.5/Lib/datetime.py.

When these classes define __init__, pytype has trouble type-checking
classes that inherit from datetime.datetime (done in, e.g., the third party
datetime_tz library) because it gets confused about what arguments the
constructor expects.
This commit is contained in:
Rebecca Chen
2020-03-07 03:52:04 -08:00
committed by GitHub
parent de4305760d
commit 37051ec699

View File

@@ -34,7 +34,7 @@ class date:
max: ClassVar[date]
resolution: ClassVar[timedelta]
def __init__(self, year: int, month: int, day: int) -> None: ...
def __new__(cls: Type[_S], year: int, month: int, day: int) -> _S: ...
@classmethod
def fromtimestamp(cls: Type[_S], __timestamp: float) -> _S: ...
@@ -201,13 +201,31 @@ class datetime(date):
resolution: ClassVar[timedelta]
if sys.version_info >= (3, 6):
def __init__(self, year: int, month: int, day: int, hour: int = ...,
minute: int = ..., second: int = ..., microsecond: int = ...,
tzinfo: Optional[_tzinfo] = ..., *, fold: int = ...) -> None: ...
def __new__(
cls: Type[_S],
year: int,
month: int,
day: int,
hour: int = ...,
minute: int = ...,
second: int = ...,
microsecond: int = ...,
tzinfo: Optional[_tzinfo] = ...,
*,
fold: int = ...,
) -> _S: ...
else:
def __init__(self, year: int, month: int, day: int, hour: int = ...,
minute: int = ..., second: int = ..., microsecond: int = ...,
tzinfo: Optional[_tzinfo] = ...) -> None: ...
def __new__(
cls: Type[_S],
year: int,
month: int,
day: int,
hour: int = ...,
minute: int = ...,
second: int = ...,
microsecond: int = ...,
tzinfo: Optional[_tzinfo] = ...,
) -> _S: ...
@property
def year(self) -> int: ...