Skip to content

CLN: Clean uses of super, part II #26230

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
invgrep -R --include="*.py" --include="*.pyx" -E "class\s\S*\(object\):" pandas scripts
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Check for python2-style super usage' ; echo $MSG
invgrep -R --include="*.py" --include="*.pyx" -E "super\(\w*, (self|cls)\)" pandas
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Check for backticks incorrectly rendering because of missing spaces' ; echo $MSG
invgrep -R --include="*.rst" -E "[a-zA-Z0-9]\`\`?[a-zA-Z0-9]" doc/source/
RET=$(($RET + $?)) ; echo $MSG "DONE"
Expand Down
19 changes: 9 additions & 10 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -483,44 +483,43 @@ cdef class TimedeltaEngine(DatetimeEngine):
cdef class PeriodEngine(Int64Engine):

cdef _get_index_values(self):
return super(PeriodEngine, self).vgetter()
return super().vgetter()

cpdef _call_map_locations(self, values):
super(PeriodEngine, self)._call_map_locations(values.view('i8'))
super()._call_map_locations(values.view('i8'))

def _call_monotonic(self, values):
return super(PeriodEngine, self)._call_monotonic(values.view('i8'))
return super()._call_monotonic(values.view('i8'))

def get_indexer(self, values):
cdef ndarray[int64_t, ndim=1] ordinals

super(PeriodEngine, self)._ensure_mapping_populated()
super()._ensure_mapping_populated()

freq = super(PeriodEngine, self).vgetter().freq
freq = super().vgetter().freq
ordinals = periodlib.extract_ordinals(values, freq)

return self.mapping.lookup(ordinals)

def get_pad_indexer(self, other, limit=None):
freq = super(PeriodEngine, self).vgetter().freq
freq = super().vgetter().freq
ordinal = periodlib.extract_ordinals(other, freq)

return algos.pad(self._get_index_values(),
np.asarray(ordinal), limit=limit)

def get_backfill_indexer(self, other, limit=None):
freq = super(PeriodEngine, self).vgetter().freq
freq = super().vgetter().freq
ordinal = periodlib.extract_ordinals(other, freq)

return algos.backfill(self._get_index_values(),
np.asarray(ordinal), limit=limit)

def get_indexer_non_unique(self, targets):
freq = super(PeriodEngine, self).vgetter().freq
freq = super().vgetter().freq
ordinal = periodlib.extract_ordinals(targets, freq)
ordinal_array = np.asarray(ordinal)

return super(PeriodEngine, self).get_indexer_non_unique(ordinal_array)
return super().get_indexer_non_unique(ordinal_array)


cpdef convert_scalar(ndarray arr, object value):
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ default 'raise'
return create_timestamp_from_ts(value, dts, _tzinfo, self.freq)

def isoformat(self, sep='T'):
base = super(_Timestamp, self).isoformat(sep=sep)
base = super().isoformat(sep=sep)
Copy link
Contributor Author

@topper-123 topper-123 Apr 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the first parameter in super to implivitly be TimeStamp rather than _Timestamp, which is ok in this case.

if self.nanosecond == 0:
return base

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2025,8 +2025,8 @@ def __unicode__(self):
return result

def __repr__(self):
# We want PandasObject.__repr__, which dispatches to __unicode__
return super(ExtensionArray, self).__repr__()
# We want to bypass ExtensionArray.__repr__.
return str(self)
Copy link
Contributor Author

@topper-123 topper-123 Apr 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ends up in both master and this PR to call self.__unicode__. __unicode__ isn't needed anymore so should be cleaned up in a future PR.


def _maybe_coerce_indexer(self, indexer):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ def _add_unary_ops(cls):
def _add_comparison_ops(cls):
cls.__and__ = cls._create_comparison_method(operator.and_)
cls.__or__ = cls._create_comparison_method(operator.or_)
super(SparseArray, cls)._add_comparison_ops()
super()._add_comparison_ops()

# ----------
# Formatting
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ def is_dtype(cls, dtype):
return False
else:
return False
return super(PeriodDtype, cls).is_dtype(dtype)
return super().is_dtype(dtype)

@classmethod
def construct_array_type(cls):
Expand Down Expand Up @@ -1047,4 +1047,4 @@ def is_dtype(cls, dtype):
return False
else:
return False
return super(IntervalDtype, cls).is_dtype(dtype)
return super().is_dtype(dtype)
7 changes: 3 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6328,10 +6328,9 @@ def aggregate(self, func, axis=0, *args, **kwargs):

def _aggregate(self, arg, axis=0, *args, **kwargs):
if axis == 1:
# NDFrame.aggregate returns a tuple, and we need to transpose
# NDFrame._aggregate returns a tuple, and we need to transpose
# only result
result, how = (super(DataFrame, self.T)
._aggregate(arg, *args, **kwargs))
result, how = self.T._aggregate(arg, *args, axis=0, **kwargs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is clearer to read than the super version.

result = result.T if result is not None else result
return result, how
return super()._aggregate(arg, *args, **kwargs)
Expand All @@ -6342,7 +6341,7 @@ def _aggregate(self, arg, axis=0, *args, **kwargs):
def transform(self, func, axis=0, *args, **kwargs):
axis = self._get_axis_number(axis)
if axis == 1:
return super(DataFrame, self.T).transform(func, *args, **kwargs).T
return self.T.transform(func, *args, axis=0, **kwargs).T
return super().transform(func, *args, **kwargs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is clearer to read than the super version.


def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __new__(cls, *args, **kwargs):
if kwargs.get('freq') is not None:
from pandas.core.resample import TimeGrouper
cls = TimeGrouper
return super(Grouper, cls).__new__(cls)
return super().__new__(cls)

def __init__(self, key=None, level=None, freq=None, axis=0, sort=False):
self.key = key
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/msgpack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __new__(cls, code, data):
raise TypeError("data must be bytes")
if not 0 <= code <= 127:
raise ValueError("code must be 0~127")
return super(ExtType, cls).__new__(cls, code, data)
return super().__new__(cls, code, data)

import os # noqa

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
if isinstance(scalars, (pd.Series, pd.Index)):
raise TypeError

return super(DecimalArray2, cls)._from_sequence(
scalars, dtype=dtype, copy=copy
)
return super()._from_sequence(scalars, dtype=dtype, copy=copy)


@pytest.mark.parametrize("box", [pd.Series, pd.Index])
Expand Down
18 changes: 9 additions & 9 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ class TestEvalNumexprPython(TestEvalNumexprPandas):

@classmethod
def setup_class(cls):
super(TestEvalNumexprPython, cls).setup_class()
super().setup_class()
import numexpr as ne
cls.ne = ne
cls.engine = 'numexpr'
Expand All @@ -738,7 +738,7 @@ class TestEvalPythonPython(TestEvalNumexprPython):

@classmethod
def setup_class(cls):
super(TestEvalPythonPython, cls).setup_class()
super().setup_class()
cls.engine = 'python'
cls.parser = 'python'

Expand Down Expand Up @@ -768,7 +768,7 @@ class TestEvalPythonPandas(TestEvalPythonPython):

@classmethod
def setup_class(cls):
super(TestEvalPythonPandas, cls).setup_class()
super().setup_class()
cls.engine = 'python'
cls.parser = 'pandas'

Expand Down Expand Up @@ -1494,7 +1494,7 @@ class TestOperationsNumExprPython(TestOperationsNumExprPandas):

@classmethod
def setup_class(cls):
super(TestOperationsNumExprPython, cls).setup_class()
super().setup_class()
cls.engine = 'numexpr'
cls.parser = 'python'
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
Expand Down Expand Up @@ -1570,7 +1570,7 @@ class TestOperationsPythonPython(TestOperationsNumExprPython):

@classmethod
def setup_class(cls):
super(TestOperationsPythonPython, cls).setup_class()
super().setup_class()
cls.engine = cls.parser = 'python'
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
cls.arith_ops = filter(lambda x: x not in ('in', 'not in'),
Expand All @@ -1581,7 +1581,7 @@ class TestOperationsPythonPandas(TestOperationsNumExprPandas):

@classmethod
def setup_class(cls):
super(TestOperationsPythonPandas, cls).setup_class()
super().setup_class()
cls.engine = 'python'
cls.parser = 'pandas'
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
Expand Down Expand Up @@ -1708,7 +1708,7 @@ class TestMathPythonPandas(TestMathPythonPython):

@classmethod
def setup_class(cls):
super(TestMathPythonPandas, cls).setup_class()
super().setup_class()
cls.engine = 'python'
cls.parser = 'pandas'

Expand All @@ -1717,7 +1717,7 @@ class TestMathNumExprPandas(TestMathPythonPython):

@classmethod
def setup_class(cls):
super(TestMathNumExprPandas, cls).setup_class()
super().setup_class()
cls.engine = 'numexpr'
cls.parser = 'pandas'

Expand All @@ -1726,7 +1726,7 @@ class TestMathNumExprPython(TestMathPythonPython):

@classmethod
def setup_class(cls):
super(TestMathNumExprPython, cls).setup_class()
super().setup_class()
cls.engine = 'numexpr'
cls.parser = 'python'

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ class TestDataFrameQueryNumExprPython(TestDataFrameQueryNumExprPandas):

@classmethod
def setup_class(cls):
super(TestDataFrameQueryNumExprPython, cls).setup_class()
super().setup_class()
cls.engine = 'numexpr'
cls.parser = 'python'
cls.frame = TestData().frame
Expand Down Expand Up @@ -794,7 +794,7 @@ class TestDataFrameQueryPythonPandas(TestDataFrameQueryNumExprPandas):

@classmethod
def setup_class(cls):
super(TestDataFrameQueryPythonPandas, cls).setup_class()
super().setup_class()
cls.engine = 'python'
cls.parser = 'pandas'
cls.frame = TestData().frame
Expand All @@ -815,7 +815,7 @@ class TestDataFrameQueryPythonPython(TestDataFrameQueryNumExprPython):

@classmethod
def setup_class(cls):
super(TestDataFrameQueryPythonPython, cls).setup_class()
super().setup_class()
cls.engine = cls.parser = 'python'
cls.frame = TestData().frame

Expand Down
3 changes: 1 addition & 2 deletions pandas/tseries/holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,7 @@ def get_calendar(name):
class HolidayCalendarMetaClass(type):

def __new__(cls, clsname, bases, attrs):
calendar_class = super(HolidayCalendarMetaClass, cls).__new__(
cls, clsname, bases, attrs)
calendar_class = super().__new__(cls, clsname, bases, attrs)
register(calendar_class)
return calendar_class

Expand Down