Skip to content

Commit eaf4860

Browse files
ShaharNavehWillAyd
authored andcommitted
STY: x.__class__ to type(x) #batch-2 (#29893)
1 parent ea2e26a commit eaf4860

File tree

11 files changed

+35
-37
lines changed

11 files changed

+35
-37
lines changed

pandas/core/dtypes/dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def __repr__(self) -> str_type:
420420
if self.categories is None:
421421
data = "None, "
422422
else:
423-
data = self.categories._format_data(name=self.__class__.__name__)
423+
data = self.categories._format_data(name=type(self).__name__)
424424
return tpl.format(data=data, ordered=self._ordered)
425425

426426
@staticmethod

pandas/core/generic.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def _validate_dtype(self, dtype):
251251
if dtype.kind == "V":
252252
raise NotImplementedError(
253253
"compound dtypes are not implemented"
254-
" in the {0} constructor".format(self.__class__.__name__)
254+
" in the {0} constructor".format(type(self).__name__)
255255
)
256256

257257
return dtype
@@ -1534,7 +1534,7 @@ def __nonzero__(self):
15341534
raise ValueError(
15351535
"The truth value of a {0} is ambiguous. "
15361536
"Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
1537-
self.__class__.__name__
1537+
type(self).__name__
15381538
)
15391539
)
15401540

@@ -1559,7 +1559,7 @@ def bool(self):
15591559
elif is_scalar(v):
15601560
raise ValueError(
15611561
"bool cannot act on a non-boolean single element "
1562-
"{0}".format(self.__class__.__name__)
1562+
"{0}".format(type(self).__name__)
15631563
)
15641564

15651565
self.__nonzero__()
@@ -1865,7 +1865,7 @@ def _drop_labels_or_levels(self, keys, axis=0):
18651865
def __hash__(self):
18661866
raise TypeError(
18671867
"{0!r} objects are mutable, thus they cannot be"
1868-
" hashed".format(self.__class__.__name__)
1868+
" hashed".format(type(self).__name__)
18691869
)
18701870

18711871
def __iter__(self):
@@ -2059,7 +2059,7 @@ def __repr__(self) -> str:
20592059
# string representation based upon iterating over self
20602060
# (since, by definition, `PandasContainers` are iterable)
20612061
prepr = "[%s]" % ",".join(map(pprint_thing, self))
2062-
return f"{self.__class__.__name__}({prepr})"
2062+
return f"{type(self).__name__}({prepr})"
20632063

20642064
def _repr_latex_(self):
20652065
"""

pandas/core/groupby/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _gotitem(self, key, ndim, subset=None):
4141
except IndexError:
4242
groupby = self._groupby
4343

44-
self = self.__class__(subset, groupby=groupby, parent=self, **kwargs)
44+
self = type(self)(subset, groupby=groupby, parent=self, **kwargs)
4545
self._reset_cache()
4646
if subset.ndim == 2:
4747
if is_scalar(key) and key in subset or is_list_like(key):

pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def _transform_general(self, func, *args, **kwargs):
473473
"""
474474
Transform with a non-str `func`.
475475
"""
476-
klass = self._selected_obj.__class__
476+
klass = type(self._selected_obj)
477477

478478
results = []
479479
for name, group in self:

pandas/core/groupby/grouper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def __repr__(self) -> str:
211211
if getattr(self, attr_name) is not None
212212
)
213213
attrs = ", ".join(attrs_list)
214-
cls_name = self.__class__.__name__
214+
cls_name = type(self).__name__
215215
return f"{cls_name}({attrs})"
216216

217217

pandas/core/indexes/base.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
815815
else:
816816
if allow_fill and fill_value is not None:
817817
msg = "Unable to fill values because {0} cannot contain NA"
818-
raise ValueError(msg.format(self.__class__.__name__))
818+
raise ValueError(msg.format(type(self).__name__))
819819
taken = self.values.take(indices)
820820
return self._shallow_copy(taken)
821821

@@ -948,7 +948,7 @@ def __repr__(self):
948948
"""
949949
Return a string representation for this object.
950950
"""
951-
klass = self.__class__.__name__
951+
klass_name = type(self).__name__
952952
data = self._format_data()
953953
attrs = self._format_attrs()
954954
space = self._format_space()
@@ -959,7 +959,7 @@ def __repr__(self):
959959
if data is None:
960960
data = ""
961961

962-
res = f"{klass}({data}{prepr})"
962+
res = f"{klass_name}({data}{prepr})"
963963

964964
return res
965965

@@ -1287,7 +1287,7 @@ def _set_names(self, values, level=None):
12871287
for name in values:
12881288
if not is_hashable(name):
12891289
raise TypeError(
1290-
"{}.name must be a hashable type".format(self.__class__.__name__)
1290+
"{}.name must be a hashable type".format(type(self).__name__)
12911291
)
12921292
self.name = values[0]
12931293

@@ -1794,7 +1794,7 @@ def is_all_dates(self) -> bool:
17941794
def __reduce__(self):
17951795
d = dict(data=self._data)
17961796
d.update(self._get_attributes_dict())
1797-
return _new_Index, (self.__class__, d), None
1797+
return _new_Index, (type(self), d), None
17981798

17991799
def __setstate__(self, state):
18001800
"""
@@ -2290,7 +2290,7 @@ def __nonzero__(self):
22902290
raise ValueError(
22912291
"The truth value of a {0} is ambiguous. "
22922292
"Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
2293-
self.__class__.__name__
2293+
type(self).__name__
22942294
)
22952295
)
22962296

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def __reduce__(self):
423423

424424
d = dict(data=self._data)
425425
d.update(self._get_attributes_dict())
426-
return _new_DatetimeIndex, (self.__class__, d), None
426+
return _new_DatetimeIndex, (type(self), d), None
427427

428428
def __setstate__(self, state):
429429
"""Necessary for making this object picklable"""

pandas/core/indexes/frozen.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ def difference(self, other) -> "FrozenList":
6969

7070
def __getitem__(self, n):
7171
if isinstance(n, slice):
72-
return self.__class__(super().__getitem__(n))
72+
return type(self)(super().__getitem__(n))
7373
return super().__getitem__(n)
7474

7575
def __radd__(self, other):
7676
if isinstance(other, tuple):
7777
other = list(other)
78-
return self.__class__(other + list(self))
78+
return type(self)(other + list(self))
7979

8080
def __eq__(self, other) -> bool:
8181
if isinstance(other, (tuple, FrozenList)):
@@ -85,12 +85,12 @@ def __eq__(self, other) -> bool:
8585
__req__ = __eq__
8686

8787
def __mul__(self, other):
88-
return self.__class__(super().__mul__(other))
88+
return type(self)(super().__mul__(other))
8989

9090
__imul__ = __mul__
9191

9292
def __reduce__(self):
93-
return self.__class__, (list(self),)
93+
return type(self), (list(self),)
9494

9595
def __hash__(self):
9696
return hash(tuple(self))
@@ -99,15 +99,15 @@ def _disabled(self, *args, **kwargs):
9999
"""This method will not function because object is immutable."""
100100
raise TypeError(
101101
"'{cls}' does not support mutable operations.".format(
102-
cls=self.__class__.__name__
102+
cls=type(self).__name__
103103
)
104104
)
105105

106106
def __str__(self) -> str:
107107
return pprint_thing(self, quote_strings=True, escape_chars=("\t", "\r", "\n"))
108108

109109
def __repr__(self) -> str:
110-
return f"{self.__class__.__name__}({str(self)})"
110+
return f"{type(self).__name__}({str(self)})"
111111

112112
__setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled
113113
pop = append = extend = remove = sort = insert = _disabled
@@ -132,7 +132,7 @@ def __new__(cls, data, dtype=None, copy=False):
132132
def _disabled(self, *args, **kwargs):
133133
"""This method will not function because object is immutable."""
134134
raise TypeError(
135-
"'{cls}' does not support mutable operations.".format(cls=self.__class__)
135+
"'{cls}' does not support mutable operations.".format(cls=type(self))
136136
)
137137

138138
__setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled

pandas/core/indexes/interval.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def __array_wrap__(self, result, context=None):
497497
def __reduce__(self):
498498
d = dict(left=self.left, right=self.right)
499499
d.update(self._get_attributes_dict())
500-
return _new_IntervalIndex, (self.__class__, d), None
500+
return _new_IntervalIndex, (type(self), d), None
501501

502502
@Appender(_index_shared_docs["copy"])
503503
def copy(self, deep=False, name=None):
@@ -512,7 +512,7 @@ def copy(self, deep=False, name=None):
512512

513513
@Appender(_index_shared_docs["astype"])
514514
def astype(self, dtype, copy=True):
515-
with rewrite_exception("IntervalArray", self.__class__.__name__):
515+
with rewrite_exception("IntervalArray", type(self).__name__):
516516
new_values = self.values.astype(dtype, copy=copy)
517517
if is_interval_dtype(new_values):
518518
return self._shallow_copy(new_values.left, new_values.right)
@@ -1205,7 +1205,7 @@ def _format_attrs(self):
12051205
return attrs
12061206

12071207
def _format_space(self):
1208-
space = " " * (len(self.__class__.__name__) + 1)
1208+
space = " " * (len(type(self).__name__) + 1)
12091209
return "\n{space}".format(space=space)
12101210

12111211
# --------------------------------------------------------------------

pandas/core/indexes/multi.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1245,9 +1245,7 @@ def _set_names(self, names, level=None, validate=True):
12451245
# All items in 'names' need to be hashable:
12461246
if not is_hashable(name):
12471247
raise TypeError(
1248-
"{}.name must be a hashable type".format(
1249-
self.__class__.__name__
1250-
)
1248+
"{}.name must be a hashable type".format(type(self).__name__)
12511249
)
12521250
self._names[lev] = name
12531251

@@ -1911,7 +1909,7 @@ def __reduce__(self):
19111909
sortorder=self.sortorder,
19121910
names=list(self.names),
19131911
)
1914-
return ibase._new_Index, (self.__class__, d), None
1912+
return ibase._new_Index, (type(self), d), None
19151913

19161914
def __setstate__(self, state):
19171915
"""Necessary for making this object picklable"""
@@ -3264,7 +3262,7 @@ def astype(self, dtype, copy=True):
32643262
elif not is_object_dtype(dtype):
32653263
msg = (
32663264
"Setting {cls} dtype to anything other than object is not supported"
3267-
).format(cls=self.__class__)
3265+
).format(cls=type(self))
32683266
raise TypeError(msg)
32693267
elif copy is True:
32703268
return self._shallow_copy()

pandas/core/indexes/range.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _get_data_as_items(self):
179179
def __reduce__(self):
180180
d = self._get_attributes_dict()
181181
d.update(dict(self._get_data_as_items()))
182-
return ibase._new_Index, (self.__class__, d), None
182+
return ibase._new_Index, (type(self), d), None
183183

184184
# --------------------------------------------------------------------
185185
# Rendering Methods
@@ -592,27 +592,27 @@ def _union(self, other, sort):
592592
and (start_s - end_o) <= step_s
593593
and (start_o - end_s) <= step_s
594594
):
595-
return self.__class__(start_r, end_r + step_s, step_s)
595+
return type(self)(start_r, end_r + step_s, step_s)
596596
if (
597597
(step_s % 2 == 0)
598598
and (abs(start_s - start_o) <= step_s / 2)
599599
and (abs(end_s - end_o) <= step_s / 2)
600600
):
601-
return self.__class__(start_r, end_r + step_s / 2, step_s / 2)
601+
return type(self)(start_r, end_r + step_s / 2, step_s / 2)
602602
elif step_o % step_s == 0:
603603
if (
604604
(start_o - start_s) % step_s == 0
605605
and (start_o + step_s >= start_s)
606606
and (end_o - step_s <= end_s)
607607
):
608-
return self.__class__(start_r, end_r + step_s, step_s)
608+
return type(self)(start_r, end_r + step_s, step_s)
609609
elif step_s % step_o == 0:
610610
if (
611611
(start_s - start_o) % step_o == 0
612612
and (start_s + step_o >= start_o)
613613
and (end_s - step_o <= end_o)
614614
):
615-
return self.__class__(start_r, end_r + step_o, step_o)
615+
return type(self)(start_r, end_r + step_o, step_o)
616616
return self._int64index._union(other, sort=sort)
617617

618618
@Appender(_index_shared_docs["join"])
@@ -781,7 +781,7 @@ def _evaluate_numeric_binop(self, other):
781781
rstart = op(left.start, right)
782782
rstop = op(left.stop, right)
783783

784-
result = self.__class__(rstart, rstop, rstep, **attrs)
784+
result = type(self)(rstart, rstop, rstep, **attrs)
785785

786786
# for compat with numpy / Int64Index
787787
# even if we can represent as a RangeIndex, return

0 commit comments

Comments
 (0)