Skip to content

gh-89979: Sync C/Python datetime implementations positional arguments #136366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ def microseconds(self):
"""microseconds"""
return self._microseconds

def __add__(self, other):
def __add__(self, other, /):
if isinstance(other, timedelta):
# for CPython compatibility, we cannot use
# our __class__ here, but need a real timedelta
Expand All @@ -825,7 +825,7 @@ def __add__(self, other):

__radd__ = __add__

def __sub__(self, other):
def __sub__(self, other, /):
if isinstance(other, timedelta):
# for CPython compatibility, we cannot use
# our __class__ here, but need a real timedelta
Expand All @@ -834,7 +834,7 @@ def __sub__(self, other):
self._microseconds - other._microseconds)
return NotImplemented

def __rsub__(self, other):
def __rsub__(self, other, /):
if isinstance(other, timedelta):
return -self + other
return NotImplemented
Expand All @@ -855,7 +855,7 @@ def __abs__(self):
else:
return self

def __mul__(self, other):
def __mul__(self, other, /):
if isinstance(other, int):
# for CPython compatibility, we cannot use
# our __class__ here, but need a real timedelta
Expand All @@ -874,7 +874,7 @@ def _to_microseconds(self):
return ((self._days * (24*3600) + self._seconds) * 1000000 +
self._microseconds)

def __floordiv__(self, other):
def __floordiv__(self, other, /):
if not isinstance(other, (int, timedelta)):
return NotImplemented
usec = self._to_microseconds()
Expand All @@ -883,7 +883,7 @@ def __floordiv__(self, other):
if isinstance(other, int):
return timedelta(0, 0, usec // other)

def __truediv__(self, other):
def __truediv__(self, other, /):
if not isinstance(other, (int, float, timedelta)):
return NotImplemented
usec = self._to_microseconds()
Expand All @@ -895,13 +895,13 @@ def __truediv__(self, other):
a, b = other.as_integer_ratio()
return timedelta(0, 0, _divide_and_round(b * usec, a))

def __mod__(self, other):
def __mod__(self, other, /):
if isinstance(other, timedelta):
r = self._to_microseconds() % other._to_microseconds()
return timedelta(0, 0, r)
return NotImplemented

def __divmod__(self, other):
def __divmod__(self, other, /):
if isinstance(other, timedelta):
q, r = divmod(self._to_microseconds(),
other._to_microseconds())
Expand All @@ -916,25 +916,25 @@ def __eq__(self, other):
else:
return NotImplemented

def __le__(self, other):
def __le__(self, other, /):
if isinstance(other, timedelta):
return self._cmp(other) <= 0
else:
return NotImplemented

def __lt__(self, other):
def __lt__(self, other, /):
if isinstance(other, timedelta):
return self._cmp(other) < 0
else:
return NotImplemented

def __ge__(self, other):
def __ge__(self, other, /):
if isinstance(other, timedelta):
return self._cmp(other) >= 0
else:
return NotImplemented

def __gt__(self, other):
def __gt__(self, other, /):
if isinstance(other, timedelta):
return self._cmp(other) > 0
else:
Expand Down Expand Up @@ -1047,7 +1047,7 @@ def today(cls):
return cls.fromtimestamp(t)

@classmethod
def fromordinal(cls, n):
def fromordinal(cls, n, /):
"""Construct a date from a proleptic Gregorian ordinal.

January 1 of year 1 is day 1. Only the year, month and day are
Expand All @@ -1057,7 +1057,7 @@ def fromordinal(cls, n):
return cls(y, m, d)

@classmethod
def fromisoformat(cls, date_string):
def fromisoformat(cls, date_string, /):
"""Construct a date from a string in ISO 8601 format."""

if not isinstance(date_string, str):
Expand Down Expand Up @@ -1123,7 +1123,7 @@ def strftime(self, format):
"""
return _wrap_strftime(self, format, self.timetuple())

def __format__(self, fmt):
def __format__(self, fmt, /):
if not isinstance(fmt, str):
raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
Expand Down Expand Up @@ -1194,22 +1194,22 @@ def __eq__(self, other):
return self._cmp(other) == 0
return NotImplemented

def __le__(self, other):
def __le__(self, other, /):
if isinstance(other, date) and not isinstance(other, datetime):
return self._cmp(other) <= 0
return NotImplemented

def __lt__(self, other):
def __lt__(self, other, /):
if isinstance(other, date) and not isinstance(other, datetime):
return self._cmp(other) < 0
return NotImplemented

def __ge__(self, other):
def __ge__(self, other, /):
if isinstance(other, date) and not isinstance(other, datetime):
return self._cmp(other) >= 0
return NotImplemented

def __gt__(self, other):
def __gt__(self, other, /):
if isinstance(other, date) and not isinstance(other, datetime):
return self._cmp(other) > 0
return NotImplemented
Expand All @@ -1229,7 +1229,7 @@ def __hash__(self):

# Computations

def __add__(self, other):
def __add__(self, other, /):
"Add a date to a timedelta."
if isinstance(other, timedelta):
o = self.toordinal() + other.days
Expand All @@ -1240,7 +1240,7 @@ def __add__(self, other):

__radd__ = __add__

def __sub__(self, other):
def __sub__(self, other, /):
"""Subtract two dates, or a date and a timedelta."""
if isinstance(other, timedelta):
return self + timedelta(-other.days)
Expand Down Expand Up @@ -1316,23 +1316,23 @@ class tzinfo:
"""
__slots__ = ()

def tzname(self, dt):
def tzname(self, dt, /):
"datetime -> string name of time zone."
raise NotImplementedError("tzinfo subclass must override tzname()")

def utcoffset(self, dt):
def utcoffset(self, dt, /):
"datetime -> timedelta, positive for east of UTC, negative for west of UTC"
raise NotImplementedError("tzinfo subclass must override utcoffset()")

def dst(self, dt):
def dst(self, dt, /):
"""datetime -> DST offset as timedelta, positive for east of UTC.

Return 0 if DST not in effect. utcoffset() must include the DST
offset.
"""
raise NotImplementedError("tzinfo subclass must override dst()")

def fromutc(self, dt):
def fromutc(self, dt, /):
"datetime in UTC -> datetime in local time."

if not isinstance(dt, datetime):
Expand Down Expand Up @@ -1512,25 +1512,25 @@ def __eq__(self, other):
else:
return NotImplemented

def __le__(self, other):
def __le__(self, other, /):
if isinstance(other, time):
return self._cmp(other) <= 0
else:
return NotImplemented

def __lt__(self, other):
def __lt__(self, other, /):
if isinstance(other, time):
return self._cmp(other) < 0
else:
return NotImplemented

def __ge__(self, other):
def __ge__(self, other, /):
if isinstance(other, time):
return self._cmp(other) >= 0
else:
return NotImplemented

def __gt__(self, other):
def __gt__(self, other, /):
if isinstance(other, time):
return self._cmp(other) > 0
else:
Expand Down Expand Up @@ -1631,7 +1631,7 @@ def isoformat(self, timespec='auto'):
__str__ = isoformat

@classmethod
def fromisoformat(cls, time_string):
def fromisoformat(cls, time_string, /):
"""Construct a time from a string in one of the ISO 8601 formats."""
if not isinstance(time_string, str):
raise TypeError('fromisoformat: argument must be str')
Expand Down Expand Up @@ -1669,7 +1669,7 @@ def strftime(self, format):
0, 1, -1)
return _wrap_strftime(self, format, timetuple)

def __format__(self, fmt):
def __format__(self, fmt, /):
if not isinstance(fmt, str):
raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
Expand Down Expand Up @@ -1903,7 +1903,7 @@ def fromtimestamp(cls, timestamp, tz=None):
return cls._fromtimestamp(timestamp, tz is not None, tz)

@classmethod
def utcfromtimestamp(cls, t):
def utcfromtimestamp(cls, t, /):
"""Construct a naive UTC datetime from a POSIX timestamp."""
import warnings
warnings.warn("datetime.datetime.utcfromtimestamp() is deprecated and scheduled "
Expand Down Expand Up @@ -1947,7 +1947,7 @@ def combine(cls, date, time, tzinfo=True):
tzinfo, fold=time.fold)

@classmethod
def fromisoformat(cls, date_string):
def fromisoformat(cls, date_string, /):
"""Construct a datetime from a string in one of the ISO 8601 formats."""
if not isinstance(date_string, str):
raise TypeError('fromisoformat: argument must be str')
Expand Down Expand Up @@ -2208,7 +2208,7 @@ def __str__(self):
return self.isoformat(sep=' ')

@classmethod
def strptime(cls, date_string, format):
def strptime(cls, date_string, format, /):
'string, format -> new datetime parsed from a string (like time.strptime()).'
import _strptime
return _strptime._strptime_datetime_datetime(cls, date_string, format)
Expand Down Expand Up @@ -2258,25 +2258,25 @@ def __eq__(self, other):
else:
return NotImplemented

def __le__(self, other):
def __le__(self, other, /):
if isinstance(other, datetime):
return self._cmp(other) <= 0
else:
return NotImplemented

def __lt__(self, other):
def __lt__(self, other, /):
if isinstance(other, datetime):
return self._cmp(other) < 0
else:
return NotImplemented

def __ge__(self, other):
def __ge__(self, other, /):
if isinstance(other, datetime):
return self._cmp(other) >= 0
else:
return NotImplemented

def __gt__(self, other):
def __gt__(self, other, /):
if isinstance(other, datetime):
return self._cmp(other) > 0
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`datetime`: Mark arguments as "positional only" (:pep:`570`) for
several functions in ``_pydatetime`` to match C implementation.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy