Skip to content
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

STY: x.__class__ to type(x) #batch-3 #29894

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class _NDFrameIndexer(_NDFrameIndexerBase):

def __call__(self, axis=None):
# we need to return a copy of ourselves
new_self = self.__class__(self.name, self.obj)
new_self = type(self)(self.name, self.obj)

if axis is not None:
axis = self.obj._get_axis_number(axis)
Expand Down Expand Up @@ -228,7 +228,9 @@ def _validate_key(self, key, axis: int):
raise AbstractMethodError(self)

def _has_valid_tuple(self, key: Tuple):
ShaharNaveh marked this conversation as resolved.
Show resolved Hide resolved
""" check the key for valid keys across my indexer """
"""
Check the key for valid keys across my indexer.
"""
for i, k in enumerate(key):
if i >= self.ndim:
raise IndexingError("Too many indexers")
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ def make_block_same_class(self, values, placement=None, ndim=None):
placement = self.mgr_locs
if ndim is None:
ndim = self.ndim
return make_block(values, placement=placement, ndim=ndim, klass=self.__class__)
return make_block(values, placement=placement, ndim=ndim, klass=type(self))

def __repr__(self) -> str:
# don't want to print out all of the items here
name = pprint_thing(self.__class__.__name__)
name = type(self).__name__
if self._is_single_block:

result = "{name}: {len} dtype: {dtype}".format(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __init__(self, block, shape, indexers=None):

def __repr__(self) -> str:
return "{name}({block!r}, {indexers})".format(
name=self.__class__.__name__, block=self.block, indexers=self.indexers
name=type(self).__name__, block=self.block, indexers=self.indexers
)

@cache_readonly
Expand Down
22 changes: 10 additions & 12 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def make_empty(self, axes=None):
blocks = np.array([], dtype=self.array_dtype)
else:
blocks = []
return self.__class__(blocks, axes)
return type(self)(blocks, axes)

def __nonzero__(self):
return True
Expand Down Expand Up @@ -321,7 +321,7 @@ def __len__(self) -> int:
return len(self.items)

def __repr__(self) -> str:
output = pprint_thing(self.__class__.__name__)
output = type(self).__name__
for i, ax in enumerate(self.axes):
if i == 0:
output += "\nItems: {ax}".format(ax=ax)
Expand Down Expand Up @@ -435,7 +435,7 @@ def apply(

if len(result_blocks) == 0:
return self.make_empty(axes or self.axes)
bm = self.__class__(
bm = type(self)(
result_blocks, axes or self.axes, do_integrity_check=do_integrity_check
)
bm._consolidate_inplace()
Expand Down Expand Up @@ -524,7 +524,7 @@ def get_axe(block, qs, axes):
for b in blocks
]

return self.__class__(blocks, new_axes)
return type(self)(blocks, new_axes)

# single block, i.e. ndim == {1}
values = concat_compat([b.values for b in blocks])
Expand Down Expand Up @@ -634,7 +634,7 @@ def comp(s, regex=False):
rb = new_rb
result_blocks.extend(rb)

bm = self.__class__(result_blocks, self.axes)
bm = type(self)(result_blocks, self.axes)
bm._consolidate_inplace()
return bm

Expand Down Expand Up @@ -729,7 +729,7 @@ def combine(self, blocks, copy=True):
axes = list(self.axes)
axes[0] = self.items.take(indexer)

return self.__class__(new_blocks, axes, do_integrity_check=False)
return type(self)(new_blocks, axes, do_integrity_check=False)

def get_slice(self, slobj, axis=0):
if axis >= self.ndim:
Expand All @@ -746,7 +746,7 @@ def get_slice(self, slobj, axis=0):
new_axes = list(self.axes)
new_axes[axis] = new_axes[axis][slobj]

bm = self.__class__(new_blocks, new_axes, do_integrity_check=False)
bm = type(self)(new_blocks, new_axes, do_integrity_check=False)
bm._consolidate_inplace()
return bm

Expand Down Expand Up @@ -922,7 +922,7 @@ def consolidate(self):
if self.is_consolidated():
return self

bm = self.__class__(self.blocks, self.axes)
bm = type(self)(self.blocks, self.axes)
bm._is_consolidated = False
bm._consolidate_inplace()
return bm
Expand Down Expand Up @@ -1256,7 +1256,7 @@ def reindex_indexer(

new_axes = list(self.axes)
new_axes[axis] = new_axis
return self.__class__(new_blocks, new_axes)
return type(self)(new_blocks, new_axes)

def _slice_take_blocks_ax0(self, slice_or_indexer, fill_tuple=None):
"""
Expand Down Expand Up @@ -1526,9 +1526,7 @@ def get_slice(self, slobj, axis=0):
if axis >= self.ndim:
raise IndexError("Requested axis not found in manager")

return self.__class__(
self._block._slice(slobj), self.index[slobj], fastpath=True
)
return type(self)(self._block._slice(slobj), self.index[slobj], fastpath=True)

@property
def index(self):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __str__(self) -> str:
if getattr(self.groupby, k, None) is not None
)
return "{klass} [{attrs}]".format(
klass=self.__class__.__name__, attrs=", ".join(attrs)
klass=type(self).__name__, attrs=", ".join(attrs)
)

def __getattr__(self, attr):
Expand Down Expand Up @@ -885,7 +885,7 @@ def count(self):
result = self._downsample("count")
if not len(self.ax):
if self._selected_obj.ndim == 1:
result = self._selected_obj.__class__(
result = type(self._selected_obj)(
[], index=result.index, dtype="int64", name=self._selected_obj.name
)
else:
Expand Down
9 changes: 3 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ def __init__(
elif is_extension_array_dtype(data):
pass
elif isinstance(data, (set, frozenset)):
raise TypeError(
"{0!r} type is unordered".format(data.__class__.__name__)
)
raise TypeError(f"{repr(type(data).__name__)} type is unordered")
elif isinstance(data, ABCSparseArray):
# handle sparse passed here (and force conversion)
data = data.to_dense()
Expand Down Expand Up @@ -1571,9 +1569,8 @@ def to_string(
# catch contract violations
if not isinstance(result, str):
raise AssertionError(
"result must be of type unicode, type"
" of result is {0!r}"
"".format(result.__class__.__name__)
"result must be of type str, type"
f" of result is {repr(type(result).__name__)}"
)

if buf is None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _get_window(self, other=None, win_type: Optional[str] = None) -> int:

@property
def _window_type(self) -> str:
return self.__class__.__name__
return type(self).__name__

def __repr__(self) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ def __str__(self) -> str:
if self.methodtype == "classmethod":
name = self.class_instance.__name__
else:
name = self.class_instance.__class__.__name__
name = type(self.class_instance).__name__
msg = "This {methodtype} must be defined in the concrete class {name}"
return msg.format(methodtype=self.methodtype, name=name)
2 changes: 1 addition & 1 deletion pandas/io/clipboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _stringifyText(text) -> str:
if not isinstance(text, acceptedTypes):
raise PyperclipException(
f"only str, int, float, and bool values"
f"can be copied to the clipboard, not {text.__class__.__name__}"
f"can be copied to the clipboard, not {type(text).__name__}"
ShaharNaveh marked this conversation as resolved.
Show resolved Hide resolved
)
return str(text)

Expand Down