Skip to content

Commit 8493c2a

Browse files
ShaharNavehproost
authored andcommitted
x.__class__ TO type(x) (pandas-dev#29889)
1 parent 2e7274b commit 8493c2a

File tree

10 files changed

+20
-23
lines changed

10 files changed

+20
-23
lines changed

pandas/_libs/internals.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ cdef class BlockPlacement:
5454
else:
5555
v = self._as_array
5656

57-
return f'{self.__class__.__name__}({v})'
57+
return f'{type(self).__name__}({v})'
5858

5959
def __repr__(self) -> str:
6060
return str(self)

pandas/_libs/tslibs/c_timestamp.pyx

+9-12
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ cdef class _Timestamp(datetime):
8787
return PyObject_RichCompareBool(val, other, op)
8888

8989
try:
90-
ots = self.__class__(other)
90+
ots = type(self)(other)
9191
except ValueError:
9292
return self._compare_outside_nanorange(other, op)
9393
else:
@@ -96,7 +96,7 @@ cdef class _Timestamp(datetime):
9696
if ndim != -1:
9797
if ndim == 0:
9898
if is_datetime64_object(other):
99-
other = self.__class__(other)
99+
other = type(self)(other)
100100
elif is_array(other):
101101
# zero-dim array, occurs if try comparison with
102102
# datetime64 scalar on the left hand side
@@ -105,7 +105,7 @@ cdef class _Timestamp(datetime):
105105
# the numpy C api to extract it.
106106
other = cnp.PyArray_ToScalar(cnp.PyArray_DATA(other),
107107
other)
108-
other = self.__class__(other)
108+
other = type(self)(other)
109109
else:
110110
return NotImplemented
111111
elif is_array(other):
@@ -226,8 +226,7 @@ cdef class _Timestamp(datetime):
226226

227227
if is_timedelta64_object(other):
228228
other_int = other.astype('timedelta64[ns]').view('i8')
229-
return self.__class__(self.value + other_int,
230-
tz=self.tzinfo, freq=self.freq)
229+
return type(self)(self.value + other_int, tz=self.tzinfo, freq=self.freq)
231230

232231
elif is_integer_object(other):
233232
maybe_integer_op_deprecated(self)
@@ -238,8 +237,7 @@ cdef class _Timestamp(datetime):
238237
elif self.freq is None:
239238
raise NullFrequencyError(
240239
"Cannot add integral value to Timestamp without freq.")
241-
return self.__class__((self.freq * other).apply(self),
242-
freq=self.freq)
240+
return type(self)((self.freq * other).apply(self), freq=self.freq)
243241

244242
elif PyDelta_Check(other) or hasattr(other, 'delta'):
245243
# delta --> offsets.Tick
@@ -253,8 +251,7 @@ cdef class _Timestamp(datetime):
253251
other.seconds * 1000000 +
254252
other.microseconds) * 1000
255253

256-
result = self.__class__(self.value + nanos,
257-
tz=self.tzinfo, freq=self.freq)
254+
result = type(self)(self.value + nanos, tz=self.tzinfo, freq=self.freq)
258255
return result
259256

260257
elif is_array(other):
@@ -272,7 +269,7 @@ cdef class _Timestamp(datetime):
272269

273270
result = datetime.__add__(self, other)
274271
if PyDateTime_Check(result):
275-
result = self.__class__(result)
272+
result = type(self)(result)
276273
result.nanosecond = self.nanosecond
277274
return result
278275

@@ -304,9 +301,9 @@ cdef class _Timestamp(datetime):
304301
if (PyDateTime_Check(self)
305302
and (PyDateTime_Check(other) or is_datetime64_object(other))):
306303
if isinstance(self, _Timestamp):
307-
other = self.__class__(other)
304+
other = type(self)(other)
308305
else:
309-
self = other.__class__(self)
306+
self = type(other)(self)
310307

311308
# validate tz's
312309
if not tz_compare(self.tzinfo, other.tzinfo):

pandas/_libs/tslibs/offsets.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class _BaseOffset:
363363
attrs = [(k, v) for k, v in all_paras.items()
364364
if (k not in exclude) and (k[0] != '_')]
365365
attrs = sorted(set(attrs))
366-
params = tuple([str(self.__class__)] + attrs)
366+
params = tuple([str(type(self))] + attrs)
367367
return params
368368

369369
@property

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ def __repr__(self) -> str:
923923
data = format_object_summary(
924924
self, self._formatter(), indent_for_name=False
925925
).rstrip(", \n")
926-
class_name = "<{}>\n".format(self.__class__.__name__)
926+
class_name = "<{}>\n".format(type(self).__name__)
927927
return template.format(
928928
class_name=class_name, data=data, length=len(self), dtype=self.dtype
929929
)

pandas/core/arrays/interval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ def __repr__(self) -> str:
870870
# repr does. So we include a newline in our template, and strip
871871
# any trailing newlines from format_object_summary
872872
data = self._format_data()
873-
class_name = "<{}>\n".format(self.__class__.__name__)
873+
class_name = "<{}>\n".format(type(self).__name__)
874874
return template.format(
875875
class_name=class_name,
876876
data=data,
@@ -880,7 +880,7 @@ def __repr__(self) -> str:
880880
)
881881

882882
def _format_space(self):
883-
space = " " * (len(self.__class__.__name__) + 1)
883+
space = " " * (len(type(self).__name__) + 1)
884884
return "\n{space}".format(space=space)
885885

886886
@property

pandas/core/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class PandasObject(DirNamesMixin):
5151
@property
5252
def _constructor(self):
5353
"""class constructor (for this class it's just `__class__`"""
54-
return self.__class__
54+
return type(self)
5555

5656
def __repr__(self) -> str:
5757
"""
@@ -1185,7 +1185,7 @@ def _reduce(
11851185
if func is None:
11861186
raise TypeError(
11871187
"{klass} cannot perform the operation {op}".format(
1188-
klass=self.__class__.__name__, op=name
1188+
klass=type(self).__name__, op=name
11891189
)
11901190
)
11911191
return func(skipna=skipna, **kwds)

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def get_callable_name(obj):
317317
return get_callable_name(obj.func)
318318
# fall back to class name
319319
if hasattr(obj, "__call__"):
320-
return obj.__class__.__name__
320+
return type(obj).__name__
321321
# everything failed (probably because the argument
322322
# wasn't actually callable); we return None
323323
# instead of the empty string in this case to allow

pandas/core/computation/expr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def visit(self, node, **kwargs):
435435
e.msg = "Python keyword not valid identifier in numexpr query"
436436
raise e
437437

438-
method = "visit_" + node.__class__.__name__
438+
method = "visit_" + type(node).__name__
439439
visitor = getattr(self, method)
440440
return visitor(node, **kwargs)
441441

pandas/core/computation/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def type(self):
145145
def raw(self) -> str:
146146
return pprint_thing(
147147
"{0}(name={1!r}, type={2})"
148-
"".format(self.__class__.__name__, self.name, self.type)
148+
"".format(type(self).__name__, self.name, self.type)
149149
)
150150

151151
@property

pandas/core/computation/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def visit_Attribute(self, node, **kwargs):
440440
attr = node.attr
441441
value = node.value
442442

443-
ctx = node.ctx.__class__
443+
ctx = type(node.ctx)
444444
if ctx == ast.Load:
445445
# resolve the value
446446
resolved = self.visit(value)

0 commit comments

Comments
 (0)