Skip to content

Commit 6f03e76

Browse files
ShaharNavehWillAyd
authored andcommitted
x.__class__ to type(x) (#29902)
1 parent ed98ff0 commit 6f03e76

File tree

10 files changed

+33
-38
lines changed

10 files changed

+33
-38
lines changed

pandas/io/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def to_string(self) -> str:
352352

353353
if len(series) == 0:
354354
return "{name}([], {footer})".format(
355-
name=self.series.__class__.__name__, footer=footer
355+
name=type(self.series).__name__, footer=footer
356356
)
357357

358358
fmt_index, have_header = self._get_formatted_index()

pandas/io/formats/printing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def format_object_summary(
321321
if display_width is None:
322322
display_width = get_option("display.width") or 80
323323
if name is None:
324-
name = obj.__class__.__name__
324+
name = type(obj).__name__
325325

326326
if indent_for_name:
327327
name_len = len(name)

pandas/io/packers.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def encode(obj):
404404
if isinstance(obj, RangeIndex):
405405
return {
406406
"typ": "range_index",
407-
"klass": obj.__class__.__name__,
407+
"klass": type(obj).__name__,
408408
"name": getattr(obj, "name", None),
409409
"start": obj._range.start,
410410
"stop": obj._range.stop,
@@ -413,7 +413,7 @@ def encode(obj):
413413
elif isinstance(obj, PeriodIndex):
414414
return {
415415
"typ": "period_index",
416-
"klass": obj.__class__.__name__,
416+
"klass": type(obj).__name__,
417417
"name": getattr(obj, "name", None),
418418
"freq": getattr(obj, "freqstr", None),
419419
"dtype": obj.dtype.name,
@@ -429,7 +429,7 @@ def encode(obj):
429429
obj = obj.tz_convert("UTC")
430430
return {
431431
"typ": "datetime_index",
432-
"klass": obj.__class__.__name__,
432+
"klass": type(obj).__name__,
433433
"name": getattr(obj, "name", None),
434434
"dtype": obj.dtype.name,
435435
"data": convert(obj.asi8),
@@ -444,7 +444,7 @@ def encode(obj):
444444
typ = "interval_array"
445445
return {
446446
"typ": typ,
447-
"klass": obj.__class__.__name__,
447+
"klass": type(obj).__name__,
448448
"name": getattr(obj, "name", None),
449449
"left": getattr(obj, "left", None),
450450
"right": getattr(obj, "right", None),
@@ -453,7 +453,7 @@ def encode(obj):
453453
elif isinstance(obj, MultiIndex):
454454
return {
455455
"typ": "multi_index",
456-
"klass": obj.__class__.__name__,
456+
"klass": type(obj).__name__,
457457
"names": getattr(obj, "names", None),
458458
"dtype": obj.dtype.name,
459459
"data": convert(obj.values),
@@ -462,7 +462,7 @@ def encode(obj):
462462
else:
463463
return {
464464
"typ": "index",
465-
"klass": obj.__class__.__name__,
465+
"klass": type(obj).__name__,
466466
"name": getattr(obj, "name", None),
467467
"dtype": obj.dtype.name,
468468
"data": convert(obj.values),
@@ -472,7 +472,7 @@ def encode(obj):
472472
elif isinstance(obj, Categorical):
473473
return {
474474
"typ": "category",
475-
"klass": obj.__class__.__name__,
475+
"klass": type(obj).__name__,
476476
"name": getattr(obj, "name", None),
477477
"codes": obj.codes,
478478
"categories": obj.categories,
@@ -483,7 +483,7 @@ def encode(obj):
483483
elif isinstance(obj, Series):
484484
return {
485485
"typ": "series",
486-
"klass": obj.__class__.__name__,
486+
"klass": type(obj).__name__,
487487
"name": getattr(obj, "name", None),
488488
"index": obj.index,
489489
"dtype": obj.dtype.name,
@@ -498,15 +498,15 @@ def encode(obj):
498498
# the block manager
499499
return {
500500
"typ": "block_manager",
501-
"klass": obj.__class__.__name__,
501+
"klass": type(obj).__name__,
502502
"axes": data.axes,
503503
"blocks": [
504504
{
505505
"locs": b.mgr_locs.as_array,
506506
"values": convert(b.values),
507507
"shape": b.values.shape,
508508
"dtype": b.dtype.name,
509-
"klass": b.__class__.__name__,
509+
"klass": type(b).__name__,
510510
"compress": compressor,
511511
}
512512
for b in data.blocks
@@ -553,15 +553,15 @@ def encode(obj):
553553
elif isinstance(obj, BlockIndex):
554554
return {
555555
"typ": "block_index",
556-
"klass": obj.__class__.__name__,
556+
"klass": type(obj).__name__,
557557
"blocs": obj.blocs,
558558
"blengths": obj.blengths,
559559
"length": obj.length,
560560
}
561561
elif isinstance(obj, IntIndex):
562562
return {
563563
"typ": "int_index",
564-
"klass": obj.__class__.__name__,
564+
"klass": type(obj).__name__,
565565
"indices": obj.indices,
566566
"length": obj.length,
567567
}

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3692,7 +3692,7 @@ def create_axes(
36923692
# the non_index_axes info
36933693
info = _get_info(self.info, i)
36943694
info["names"] = list(a.names)
3695-
info["type"] = a.__class__.__name__
3695+
info["type"] = type(a).__name__
36963696

36973697
self.non_index_axes.append((i, append_axis))
36983698

pandas/io/stata.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -856,12 +856,11 @@ def __str__(self) -> str:
856856
return self.string
857857

858858
def __repr__(self) -> str:
859-
# not perfect :-/
860-
return "{cls}({obj})".format(cls=self.__class__, obj=self)
859+
return f"{type(self)}({self})"
861860

862861
def __eq__(self, other) -> bool:
863862
return (
864-
isinstance(other, self.__class__)
863+
isinstance(other, type(self))
865864
and self.string == other.string
866865
and self.value == other.value
867866
)

pandas/tests/extension/base/dtype.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_eq(self, dtype):
9696
assert dtype != "anonther_type"
9797

9898
def test_construct_from_string(self, dtype):
99-
dtype_instance = dtype.__class__.construct_from_string(dtype.name)
100-
assert isinstance(dtype_instance, dtype.__class__)
99+
dtype_instance = type(dtype).construct_from_string(dtype.name)
100+
assert isinstance(dtype_instance, type(dtype))
101101
with pytest.raises(TypeError):
102-
dtype.__class__.construct_from_string("another_type")
102+
type(dtype).construct_from_string("another_type")

pandas/tests/extension/base/ops.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ def test_direct_arith_with_series_returns_not_implemented(self, data):
123123
result = data.__add__(other)
124124
assert result is NotImplemented
125125
else:
126-
raise pytest.skip(
127-
"{} does not implement add".format(data.__class__.__name__)
128-
)
126+
raise pytest.skip(f"{type(data).__name__} does not implement add")
129127

130128

131129
class BaseComparisonOpsTests(BaseOpsUtil):
@@ -169,6 +167,4 @@ def test_direct_arith_with_series_returns_not_implemented(self, data):
169167
result = data.__eq__(other)
170168
assert result is NotImplemented
171169
else:
172-
raise pytest.skip(
173-
"{} does not implement __eq__".format(data.__class__.__name__)
174-
)
170+
raise pytest.skip(f"{type(data).__name__} does not implement __eq__")

pandas/tests/extension/base/printing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_array_repr(self, data, size):
1818
data = type(data)._concat_same_type([data] * 5)
1919

2020
result = repr(data)
21-
assert data.__class__.__name__ in result
21+
assert type(data).__name__ in result
2222
assert "Length: {}".format(len(data)) in result
2323
assert str(data.dtype) in result
2424
if size == "big":

pandas/tests/frame/test_apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def test_applymap_box(self):
642642
}
643643
)
644644

645-
result = df.applymap(lambda x: "{0}".format(x.__class__.__name__))
645+
result = df.applymap(lambda x: type(x).__name__)
646646
expected = pd.DataFrame(
647647
{
648648
"a": ["Timestamp", "Timestamp"],

pandas/tests/indexes/common.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_str(self):
244244
idx = self.create_index()
245245
idx.name = "foo"
246246
assert "'foo'" in str(idx)
247-
assert idx.__class__.__name__ in str(idx)
247+
assert type(idx).__name__ in str(idx)
248248

249249
def test_repr_max_seq_item_setting(self):
250250
# GH10182
@@ -260,8 +260,8 @@ def test_copy_name(self, indices):
260260
if isinstance(indices, MultiIndex):
261261
return
262262

263-
first = indices.__class__(indices, copy=True, name="mario")
264-
second = first.__class__(first, copy=False)
263+
first = type(indices)(indices, copy=True, name="mario")
264+
second = type(first)(first, copy=False)
265265

266266
# Even though "copy=False", we want a new object.
267267
assert first is not second
@@ -292,7 +292,7 @@ def test_ensure_copied_data(self, indices):
292292
# MultiIndex and CategoricalIndex are tested separately
293293
return
294294

295-
index_type = indices.__class__
295+
index_type = type(indices)
296296
result = index_type(indices.values, copy=True, **init_kwargs)
297297
tm.assert_index_equal(indices, result)
298298
tm.assert_numpy_array_equal(
@@ -502,7 +502,7 @@ def test_difference_base(self, sort, indices):
502502
cases = [klass(second.values) for klass in [np.array, Series, list]]
503503
for case in cases:
504504
if isinstance(indices, (DatetimeIndex, TimedeltaIndex)):
505-
assert result.__class__ == answer.__class__
505+
assert type(result) == type(answer)
506506
tm.assert_numpy_array_equal(
507507
result.sort_values().asi8, answer.sort_values().asi8
508508
)
@@ -677,9 +677,9 @@ def test_hasnans_isnans(self, indices):
677677
values[1] = np.nan
678678

679679
if isinstance(indices, PeriodIndex):
680-
idx = indices.__class__(values, freq=indices.freq)
680+
idx = type(indices)(values, freq=indices.freq)
681681
else:
682-
idx = indices.__class__(values)
682+
idx = type(indices)(values)
683683

684684
expected = np.array([False] * len(idx), dtype=bool)
685685
expected[1] = True
@@ -716,9 +716,9 @@ def test_fillna(self, indices):
716716
values[1] = np.nan
717717

718718
if isinstance(indices, PeriodIndex):
719-
idx = indices.__class__(values, freq=indices.freq)
719+
idx = type(indices)(values, freq=indices.freq)
720720
else:
721-
idx = indices.__class__(values)
721+
idx = type(indices)(values)
722722

723723
expected = np.array([False] * len(idx), dtype=bool)
724724
expected[1] = True

0 commit comments

Comments
 (0)