Skip to content

CLN: remove cython<3 reversed-op pattern #56006

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

Merged
merged 1 commit into from
Nov 17, 2023
Merged
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
15 changes: 0 additions & 15 deletions pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -495,17 +495,6 @@ cdef class Interval(IntervalMixin):
or cnp.is_timedelta64_object(y)
):
return Interval(self.left + y, self.right + y, closed=self.closed)
elif (
# __radd__ pattern
# TODO(cython3): remove this
isinstance(y, Interval)
and (
isinstance(self, numbers.Number)
or PyDelta_Check(self)
or cnp.is_timedelta64_object(self)
)
):
return Interval(y.left + self, y.right + self, closed=y.closed)
return NotImplemented

def __radd__(self, other):
Expand All @@ -529,10 +518,6 @@ cdef class Interval(IntervalMixin):
def __mul__(self, y):
if isinstance(y, numbers.Number):
return Interval(self.left * y, self.right * y, closed=self.closed)
elif isinstance(y, Interval) and isinstance(self, numbers.Number):
# __radd__ semantics
# TODO(cython3): remove this
return Interval(y.left * self, y.right * self, closed=y.closed)
return NotImplemented

def __rmul__(self, other):
Expand Down
29 changes: 3 additions & 26 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ cdef class _NaT(datetime):
return NotImplemented

def __add__(self, other):
if self is not c_NaT:
# TODO(cython3): remove this it moved to __radd__
# cython __radd__ semantics
self, other = other, self

if PyDateTime_Check(other):
return c_NaT
elif PyDelta_Check(other):
Expand Down Expand Up @@ -158,14 +153,6 @@ cdef class _NaT(datetime):
def __sub__(self, other):
# Duplicate some logic from _Timestamp.__sub__ to avoid needing
# to subclass; allows us to @final(_Timestamp.__sub__)
cdef:
bint is_rsub = False

if self is not c_NaT:
# cython __rsub__ semantics
# TODO(cython3): remove __rsub__ logic from here
self, other = other, self
is_rsub = True

if PyDateTime_Check(other):
return c_NaT
Expand All @@ -180,19 +167,9 @@ cdef class _NaT(datetime):

elif util.is_array(other):
if other.dtype.kind == "m":
if not is_rsub:
# NaT - timedelta64 we treat NaT as datetime64, so result
# is datetime64
result = np.empty(other.shape, dtype="datetime64[ns]")
result.fill("NaT")
return result

# __rsub__ logic here
# TODO(cython3): remove this, move above code out of
# ``if not is_rsub`` block
# timedelta64 - NaT we have to treat NaT as timedelta64
# for this to be meaningful, and the result is timedelta64
result = np.empty(other.shape, dtype="timedelta64[ns]")
# NaT - timedelta64 we treat NaT as datetime64, so result
# is datetime64
result = np.empty(other.shape, dtype="datetime64[ns]")
result.fill("NaT")
return result

Expand Down
24 changes: 1 addition & 23 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,7 @@ cdef class BaseOffset:
return type(self)(n=1, normalize=self.normalize, **self.kwds)

def __add__(self, other):
if not isinstance(self, BaseOffset):
# cython semantics; this is __radd__
# TODO(cython3): remove this, this moved to __radd__
return other.__add__(self)

elif util.is_array(other) and other.dtype == object:
if util.is_array(other) and other.dtype == object:
return np.array([self + x for x in other])

try:
Expand All @@ -511,10 +506,6 @@ cdef class BaseOffset:
elif type(other) is type(self):
return type(self)(self.n - other.n, normalize=self.normalize,
**self.kwds)
elif not isinstance(self, BaseOffset):
# TODO(cython3): remove, this moved to __rsub__
# cython semantics, this is __rsub__
return (-other).__add__(self)
else:
# e.g. PeriodIndex
return NotImplemented
Expand All @@ -528,10 +519,6 @@ cdef class BaseOffset:
elif is_integer_object(other):
return type(self)(n=other * self.n, normalize=self.normalize,
**self.kwds)
elif not isinstance(self, BaseOffset):
# TODO(cython3): remove this, this moved to __rmul__
# cython semantics, this is __rmul__
return other.__mul__(self)
return NotImplemented

def __rmul__(self, other):
Expand Down Expand Up @@ -1025,10 +1012,6 @@ cdef class Tick(SingleConstructorOffset):
return self.delta.__gt__(other)

def __mul__(self, other):
if not isinstance(self, Tick):
# TODO(cython3), remove this, this moved to __rmul__
# cython semantics, this is __rmul__
return other.__mul__(self)
if is_float_object(other):
n = other * self.n
# If the new `n` is an integer, we can represent it using the
Expand Down Expand Up @@ -1056,11 +1039,6 @@ cdef class Tick(SingleConstructorOffset):
return _wrap_timedelta_result(result)

def __add__(self, other):
if not isinstance(self, Tick):
# cython semantics; this is __radd__
# TODO(cython3): remove this, this moved to __radd__
return other.__add__(self)

if isinstance(other, Tick):
if type(self) is type(other):
return type(self)(self.n + other.n)
Expand Down
16 changes: 1 addition & 15 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1855,13 +1855,6 @@ cdef class _Period(PeriodMixin):

@cython.overflowcheck(True)
def __add__(self, other):
if not is_period_object(self):
# cython semantics; this is analogous to a call to __radd__
# TODO(cython3): remove this
if self is NaT:
return NaT
return other.__add__(self)

if is_any_td_scalar(other):
return self._add_timedeltalike_scalar(other)
elif is_offset_object(other):
Expand Down Expand Up @@ -1893,14 +1886,7 @@ cdef class _Period(PeriodMixin):
return self.__add__(other)

def __sub__(self, other):
if not is_period_object(self):
# cython semantics; this is like a call to __rsub__
# TODO(cython3): remove this
if self is NaT:
return NaT
return NotImplemented

elif (
if (
is_any_td_scalar(other)
or is_offset_object(other)
or util.is_integer_object(other)
Expand Down
5 changes: 0 additions & 5 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,6 @@ cdef class _Timestamp(ABCTimestamp):
dtype=object,
)

elif not isinstance(self, _Timestamp):
# cython semantics, args have been switched and this is __radd__
# TODO(cython3): remove this it moved to __radd__
return other.__add__(self)

return NotImplemented

def __radd__(self, other):
Expand Down