Skip to content

Commit

Permalink
Use canonical "magic methods" (replace x.__repr__() with repr(x)). (
Browse files Browse the repository at this point in the history
#10735)

This PR uses the canonical builtin function `repr(x)` to compute representations instead of the double-underscored form `x.__repr__()`. The magic method `def __repr__(self): ...` is how a custom representation is defined for a class, but it is not the typical way to call that function. Same for `x.__iter__()` ➡️ `iter(x)` and similarly for `__hash__`, `__str__`, etc. This PR fixes all of the instances of that pattern that I could find, mostly affecting `repr(x)`.

Note that non-standard magic methods like `__dlpack__` or `__dataframe__` that are not defined in the Python object model do not necessarily follow this convention where a free function of the same name is used to call those methods.

Authors:
  - Bradley Dice (https://github.com/bdice)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #10735
  • Loading branch information
bdice authored Apr 26, 2022
1 parent 47740bc commit 41dfdc2
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 68 deletions.
2 changes: 1 addition & 1 deletion python/cudf/cudf/_fuzz_testing/fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def log_stats(self):
logging.info(f"Run-Time elapsed (hh:mm:ss.ms) {total_time_taken}")

def write_crash(self, error):
error_file_name = datetime.datetime.now().__str__()
error_file_name = str(datetime.datetime.now())
if self._crash_dir:
crash_path = os.path.join(
self._crash_dir,
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/_lib/scalar.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ cdef class DeviceScalar:
if self.value is cudf.NA:
return (
f"{self.__class__.__name__}"
f"({self.value}, {self.dtype.__repr__()})"
f"({self.value}, {repr(self.dtype)})"
)
else:
return f"{self.__class__.__name__}({self.value.__repr__()})"
return f"{self.__class__.__name__}({repr(self.value)})"

@staticmethod
cdef DeviceScalar from_unique_ptr(unique_ptr[scalar] ptr, dtype=None):
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/column_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _create_unsafe(
return obj

def __iter__(self):
return self._data.__iter__()
return iter(self._data)

def __getitem__(self, key: Any) -> ColumnBase:
return self._data[key]
Expand All @@ -158,7 +158,7 @@ def __setitem__(self, key: Any, value: Any):
self.set_by_label(key, value)

def __delitem__(self, key: Any):
self._data.__delitem__(key)
del self._data[key]
self._clear_cache()

def __len__(self) -> int:
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def __eq__(self, other):

def __repr__(self):
if isinstance(self.element_type, (ListDtype, StructDtype)):
return f"{type(self).__name__}({self.element_type.__repr__()})"
return f"{type(self).__name__}({repr(self.element_type)})"
else:
return f"{type(self).__name__}({self.element_type})"

Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3375,7 +3375,7 @@ def to_string(self):
>>> df.to_string()
' key val\\n0 0 10.0\\n1 1 11.0\\n2 2 12.0'
"""
return self.__repr__()
return repr(self)

def __str__(self):
return self.to_string()
Expand Down
9 changes: 4 additions & 5 deletions python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ def __repr__(self):
# related issue : https://github.com/pandas-dev/pandas/issues/35389
if isinstance(preprocess, CategoricalIndex):
if preprocess.categories.dtype.kind == "f":
output = (
output = repr(
preprocess.astype("str")
.to_pandas()
.astype(
Expand All @@ -1127,18 +1127,17 @@ def __repr__(self):
ordered=preprocess.dtype.ordered,
)
)
.__repr__()
)
break_idx = output.find("ordered=")
output = (
output[:break_idx].replace("'", "") + output[break_idx:]
)
else:
output = preprocess.to_pandas().__repr__()
output = repr(preprocess.to_pandas())

output = output.replace("nan", cudf._NA_REP)
elif preprocess._values.nullable:
output = self._clean_nulls_from_index().to_pandas().__repr__()
output = repr(self._clean_nulls_from_index().to_pandas())

if not isinstance(self, StringIndex):
# We should remove all the single quotes
Expand All @@ -1150,7 +1149,7 @@ def __repr__(self):
# of StringIndex and it is valid to have them.
output = output.replace("'", "")
else:
output = preprocess.to_pandas().__repr__()
output = repr(preprocess.to_pandas())

# Fix and correct the class name of the output
# string by finding first occurrence of "(" in the output
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def __repr__(self):
else:
preprocess = preprocess.to_pandas(nullable=True)

output = preprocess.__repr__()
output = repr(preprocess)
output_prefix = self.__class__.__name__ + "("
output = output.lstrip(output_prefix)
lines = output.split("\n")
Expand Down
9 changes: 3 additions & 6 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,11 +1064,8 @@ def __repr__(self):
) or isinstance(
preprocess._column, cudf.core.column.timedelta.TimeDeltaColumn
):
output = (
preprocess.astype("O")
.fillna(cudf._NA_REP)
.to_pandas()
.__repr__()
output = repr(
preprocess.astype("O").fillna(cudf._NA_REP).to_pandas()
)
elif isinstance(
preprocess._column, cudf.core.column.CategoricalColumn
Expand Down Expand Up @@ -1111,7 +1108,7 @@ def __repr__(self):
na_rep=cudf._NA_REP,
)
else:
output = preprocess.to_pandas().__repr__()
output = repr(preprocess.to_pandas())

lines = output.split("\n")

Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/udf/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __hash__(self):
Needed so that numba caches type instances with different
`value_type` separately.
"""
return self.__repr__().__hash__()
return hash(repr(self))

def unify(self, context, other):
"""
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/tests/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ def test_multiindex_copy_sem(data, levels, codes, names):
# Test same behavior when used on DataFrame
gdf.index = gmi_copy
pdf.index = pmi_copy
assert gdf.__repr__() == pdf.__repr__()
assert repr(gdf) == repr(pdf)


@pytest.mark.parametrize(
Expand Down
Loading

0 comments on commit 41dfdc2

Please sign in to comment.