Skip to content

STY: x.__class__ to type(x) #batch-4 #29902

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
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
2 changes: 1 addition & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def to_string(self) -> str:

if len(series) == 0:
return "{name}([], {footer})".format(
name=self.series.__class__.__name__, footer=footer
name=type(self.series).__name__, footer=footer
)

fmt_index, have_header = self._get_formatted_index()
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def format_object_summary(
if display_width is None:
display_width = get_option("display.width") or 80
if name is None:
name = obj.__class__.__name__
name = type(obj).__name__

if indent_for_name:
name_len = len(name)
Expand Down
24 changes: 12 additions & 12 deletions pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def encode(obj):
if isinstance(obj, RangeIndex):
return {
"typ": "range_index",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"name": getattr(obj, "name", None),
"start": obj._range.start,
"stop": obj._range.stop,
Expand All @@ -413,7 +413,7 @@ def encode(obj):
elif isinstance(obj, PeriodIndex):
return {
"typ": "period_index",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"name": getattr(obj, "name", None),
"freq": getattr(obj, "freqstr", None),
"dtype": obj.dtype.name,
Expand All @@ -429,7 +429,7 @@ def encode(obj):
obj = obj.tz_convert("UTC")
return {
"typ": "datetime_index",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"name": getattr(obj, "name", None),
"dtype": obj.dtype.name,
"data": convert(obj.asi8),
Expand All @@ -444,7 +444,7 @@ def encode(obj):
typ = "interval_array"
return {
"typ": typ,
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"name": getattr(obj, "name", None),
"left": getattr(obj, "left", None),
"right": getattr(obj, "right", None),
Expand All @@ -453,7 +453,7 @@ def encode(obj):
elif isinstance(obj, MultiIndex):
return {
"typ": "multi_index",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"names": getattr(obj, "names", None),
"dtype": obj.dtype.name,
"data": convert(obj.values),
Expand All @@ -462,7 +462,7 @@ def encode(obj):
else:
return {
"typ": "index",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"name": getattr(obj, "name", None),
"dtype": obj.dtype.name,
"data": convert(obj.values),
Expand All @@ -472,7 +472,7 @@ def encode(obj):
elif isinstance(obj, Categorical):
return {
"typ": "category",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"name": getattr(obj, "name", None),
"codes": obj.codes,
"categories": obj.categories,
Expand All @@ -483,7 +483,7 @@ def encode(obj):
elif isinstance(obj, Series):
return {
"typ": "series",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"name": getattr(obj, "name", None),
"index": obj.index,
"dtype": obj.dtype.name,
Expand All @@ -498,15 +498,15 @@ def encode(obj):
# the block manager
return {
"typ": "block_manager",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"axes": data.axes,
"blocks": [
{
"locs": b.mgr_locs.as_array,
"values": convert(b.values),
"shape": b.values.shape,
"dtype": b.dtype.name,
"klass": b.__class__.__name__,
"klass": type(b).__name__,
"compress": compressor,
}
for b in data.blocks
Expand Down Expand Up @@ -553,15 +553,15 @@ def encode(obj):
elif isinstance(obj, BlockIndex):
return {
"typ": "block_index",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"blocs": obj.blocs,
"blengths": obj.blengths,
"length": obj.length,
}
elif isinstance(obj, IntIndex):
return {
"typ": "int_index",
"klass": obj.__class__.__name__,
"klass": type(obj).__name__,
"indices": obj.indices,
"length": obj.length,
}
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3692,7 +3692,7 @@ def create_axes(
# the non_index_axes info
info = _get_info(self.info, i)
info["names"] = list(a.names)
info["type"] = a.__class__.__name__
info["type"] = type(a).__name__

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

Expand Down
5 changes: 2 additions & 3 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,11 @@ def __str__(self) -> str:
return self.string

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

def __eq__(self, other) -> bool:
return (
isinstance(other, self.__class__)
isinstance(other, type(self))
and self.string == other.string
and self.value == other.value
)
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/extension/base/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_eq(self, dtype):
assert dtype != "anonther_type"

def test_construct_from_string(self, dtype):
dtype_instance = dtype.__class__.construct_from_string(dtype.name)
assert isinstance(dtype_instance, dtype.__class__)
dtype_instance = type(dtype).construct_from_string(dtype.name)
assert isinstance(dtype_instance, type(dtype))
with pytest.raises(TypeError):
dtype.__class__.construct_from_string("another_type")
type(dtype).construct_from_string("another_type")
8 changes: 2 additions & 6 deletions pandas/tests/extension/base/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def test_direct_arith_with_series_returns_not_implemented(self, data):
result = data.__add__(other)
assert result is NotImplemented
else:
raise pytest.skip(
"{} does not implement add".format(data.__class__.__name__)
)
raise pytest.skip(f"{type(data).__name__} does not implement add")


class BaseComparisonOpsTests(BaseOpsUtil):
Expand Down Expand Up @@ -169,6 +167,4 @@ def test_direct_arith_with_series_returns_not_implemented(self, data):
result = data.__eq__(other)
assert result is NotImplemented
else:
raise pytest.skip(
"{} does not implement __eq__".format(data.__class__.__name__)
)
raise pytest.skip(f"{type(data).__name__} does not implement __eq__")
2 changes: 1 addition & 1 deletion pandas/tests/extension/base/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_array_repr(self, data, size):
data = type(data)._concat_same_type([data] * 5)

result = repr(data)
assert data.__class__.__name__ in result
assert type(data).__name__ in result
assert "Length: {}".format(len(data)) in result
assert str(data.dtype) in result
if size == "big":
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ def test_applymap_box(self):
}
)

result = df.applymap(lambda x: "{0}".format(x.__class__.__name__))
result = df.applymap(lambda x: type(x).__name__)
expected = pd.DataFrame(
{
"a": ["Timestamp", "Timestamp"],
Expand Down
18 changes: 9 additions & 9 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def test_str(self):
idx = self.create_index()
idx.name = "foo"
assert "'foo'" in str(idx)
assert idx.__class__.__name__ in str(idx)
assert type(idx).__name__ in str(idx)

def test_repr_max_seq_item_setting(self):
# GH10182
Expand All @@ -260,8 +260,8 @@ def test_copy_name(self, indices):
if isinstance(indices, MultiIndex):
return

first = indices.__class__(indices, copy=True, name="mario")
second = first.__class__(first, copy=False)
first = type(indices)(indices, copy=True, name="mario")
second = type(first)(first, copy=False)

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

index_type = indices.__class__
index_type = type(indices)
result = index_type(indices.values, copy=True, **init_kwargs)
tm.assert_index_equal(indices, result)
tm.assert_numpy_array_equal(
Expand Down Expand Up @@ -502,7 +502,7 @@ def test_difference_base(self, sort, indices):
cases = [klass(second.values) for klass in [np.array, Series, list]]
for case in cases:
if isinstance(indices, (DatetimeIndex, TimedeltaIndex)):
assert result.__class__ == answer.__class__
assert type(result) == type(answer)
tm.assert_numpy_array_equal(
result.sort_values().asi8, answer.sort_values().asi8
)
Expand Down Expand Up @@ -677,9 +677,9 @@ def test_hasnans_isnans(self, indices):
values[1] = np.nan

if isinstance(indices, PeriodIndex):
idx = indices.__class__(values, freq=indices.freq)
idx = type(indices)(values, freq=indices.freq)
else:
idx = indices.__class__(values)
idx = type(indices)(values)

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

if isinstance(indices, PeriodIndex):
idx = indices.__class__(values, freq=indices.freq)
idx = type(indices)(values, freq=indices.freq)
else:
idx = indices.__class__(values)
idx = type(indices)(values)

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