From 9c2413403342263489d8effc86952ef372f9a8b6 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 7 Nov 2019 10:15:04 -0800 Subject: [PATCH] CLN: annotate __str__ and __repr__ methods --- pandas/_libs/internals.pyx | 4 ++-- pandas/_libs/interval.pyx | 4 ++-- pandas/_libs/intervaltree.pxi.in | 4 ++-- pandas/_libs/sparse.pyx | 4 ++-- pandas/_libs/tslibs/c_timestamp.pyx | 2 +- pandas/_libs/tslibs/nattype.pyx | 4 ++-- pandas/_libs/tslibs/offsets.pyx | 2 +- pandas/_libs/tslibs/period.pyx | 4 ++-- pandas/_libs/tslibs/timedeltas.pyx | 4 ++-- pandas/core/arrays/base.py | 2 +- pandas/core/arrays/categorical.py | 2 +- pandas/core/arrays/integer.py | 2 +- pandas/core/arrays/interval.py | 2 +- pandas/core/arrays/numpy_.py | 2 +- pandas/core/arrays/sparse/array.py | 2 +- pandas/core/arrays/sparse/dtype.py | 2 +- pandas/core/base.py | 2 +- pandas/core/computation/expr.py | 2 +- pandas/core/computation/ops.py | 10 +++++----- pandas/core/computation/pytables.py | 6 +++--- pandas/core/computation/scope.py | 2 +- pandas/core/dtypes/base.py | 2 +- pandas/core/dtypes/dtypes.py | 8 ++++---- pandas/core/generic.py | 2 +- pandas/core/groupby/groupby.py | 2 +- pandas/core/groupby/grouper.py | 4 ++-- pandas/core/indexes/frozen.py | 6 +++--- pandas/core/internals/blocks.py | 2 +- pandas/core/internals/concat.py | 2 +- pandas/core/internals/managers.py | 2 +- pandas/core/resample.py | 2 +- pandas/core/series.py | 2 +- pandas/errors/__init__.py | 2 +- pandas/io/msgpack/exceptions.py | 2 +- pandas/io/pytables.py | 10 +++++----- pandas/io/stata.py | 4 ++-- pandas/tests/extension/decimal/array.py | 2 +- pandas/tests/frame/test_alter_axes.py | 6 +++--- pandas/tests/indexing/test_indexing.py | 2 +- pandas/tests/internals/test_internals.py | 4 ++-- pandas/tests/io/json/test_pandas.py | 2 +- pandas/tests/io/json/test_ujson.py | 2 +- pandas/tests/series/test_repr.py | 2 +- pandas/tests/series/test_ufunc.py | 2 +- pandas/tseries/holiday.py | 2 +- pandas/util/_depr_module.py | 2 +- 46 files changed, 73 insertions(+), 73 deletions(-) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index ff143fea892ae..db9f16d46e48c 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -53,7 +53,7 @@ cdef class BlockPlacement: self._as_array = arr self._has_array = True - def __str__(self): + def __str__(self) -> str: cdef: slice s = self._ensure_has_slice() if s is not None: @@ -63,7 +63,7 @@ cdef class BlockPlacement: return '%s(%r)' % (self.__class__.__name__, v) - def __repr__(self): + def __repr__(self) -> str: return str(self) def __len__(self): diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index b13ce7c294f37..2bd38524852ec 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -377,7 +377,7 @@ cdef class Interval(IntervalMixin): return left, right - def __repr__(self): + def __repr__(self) -> str: left, right = self._repr_base() name = type(self).__name__ @@ -385,7 +385,7 @@ cdef class Interval(IntervalMixin): name=name, left=left, right=right, closed=self.closed) return repr_str - def __str__(self): + def __str__(self) -> str: left, right = self._repr_base() start_symbol = '[' if self.closed_left else '(' diff --git a/pandas/_libs/intervaltree.pxi.in b/pandas/_libs/intervaltree.pxi.in index 6e3be19f2b73e..8cb51be36645e 100644 --- a/pandas/_libs/intervaltree.pxi.in +++ b/pandas/_libs/intervaltree.pxi.in @@ -195,7 +195,7 @@ cdef class IntervalTree(IntervalMixin): return (result.to_array().astype('intp'), missing.to_array().astype('intp')) - def __repr__(self): + def __repr__(self) -> str: return (''.format( dtype=self.dtype, closed=self.closed, @@ -394,7 +394,7 @@ cdef class {{dtype_title}}Closed{{closed_title}}IntervalNode: else: result.extend(self.center_left_indices) - def __repr__(self): + def __repr__(self) -> str: if self.is_leaf_node: return ('<{{dtype_title}}Closed{{closed_title}}IntervalNode: ' '%s elements (terminal)>' % self.n_elements) diff --git a/pandas/_libs/sparse.pyx b/pandas/_libs/sparse.pyx index 6abaaca010b00..1944f9592829c 100644 --- a/pandas/_libs/sparse.pyx +++ b/pandas/_libs/sparse.pyx @@ -51,7 +51,7 @@ cdef class IntIndex(SparseIndex): args = (self.length, self.indices) return IntIndex, args - def __repr__(self): + def __repr__(self) -> str: output = 'IntIndex\n' output += 'Indices: %s\n' % repr(self.indices) return output @@ -341,7 +341,7 @@ cdef class BlockIndex(SparseIndex): args = (self.length, self.blocs, self.blengths) return BlockIndex, args - def __repr__(self): + def __repr__(self) -> str: output = 'BlockIndex\n' output += 'Block locations: %s\n' % repr(self.blocs) output += 'Block lengths: %s' % repr(self.blengths) diff --git a/pandas/_libs/tslibs/c_timestamp.pyx b/pandas/_libs/tslibs/c_timestamp.pyx index 032363d867196..8e4143a053ba3 100644 --- a/pandas/_libs/tslibs/c_timestamp.pyx +++ b/pandas/_libs/tslibs/c_timestamp.pyx @@ -124,7 +124,7 @@ cdef class _Timestamp(datetime): # now __reduce_ex__ is defined and higher priority than __reduce__ return self.__reduce__() - def __repr__(self): + def __repr__(self) -> str: stamp = self._repr_base zone = None diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 241aff0e19112..e491d6111a919 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -259,10 +259,10 @@ cdef class _NaT(datetime): """ return self.to_datetime64() - def __repr__(self): + def __repr__(self) -> str: return 'NaT' - def __str__(self): + def __str__(self) -> str: return 'NaT' def isoformat(self, sep='T'): diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index aaefab6ee7ff6..434252677f1a1 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -422,7 +422,7 @@ class _BaseOffset: # that allows us to use methods that can go in a `cdef class` return self * 1 - def __repr__(self): + def __repr__(self) -> str: className = getattr(self, '_outputName', type(self).__name__) if abs(self.n) != 1: diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index e297d11c5144d..2512fdb891e3e 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -2215,12 +2215,12 @@ cdef class _Period: def freqstr(self): return self.freq.freqstr - def __repr__(self): + def __repr__(self) -> str: base, mult = get_freq_code(self.freq) formatted = period_format(self.ordinal, base) return "Period('%s', '%s')" % (formatted, self.freqstr) - def __str__(self): + def __str__(self) -> str: """ Return a string representation for a particular DataFrame """ diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 8435f1cd7d732..9d8ed62388655 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1142,10 +1142,10 @@ cdef class _Timedelta(timedelta): return fmt.format(**comp_dict) - def __repr__(self): + def __repr__(self) -> str: return "Timedelta('{val}')".format(val=self._repr_base(format='long')) - def __str__(self): + def __str__(self) -> str: return self._repr_base(format='long') def __bool__(self): diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 7333254831838..2980f0d4cb906 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -913,7 +913,7 @@ def view(self, dtype=None) -> Union[ABCExtensionArray, np.ndarray]: # Printing # ------------------------------------------------------------------------ - def __repr__(self): + def __repr__(self) -> str: from pandas.io.formats.printing import format_object_summary template = "{class_name}{data}\nLength: {length}, dtype: {dtype}" diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index ce174baa66a97..d3e9c6a415879 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2048,7 +2048,7 @@ def _get_repr(self, length=True, na_rep="NaN", footer=True): result = formatter.to_string() return str(result) - def __repr__(self): + def __repr__(self) -> str: """ String representation. """ diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index 630c3e50f2c09..08b53e54b91ef 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -45,7 +45,7 @@ class _IntegerDtype(ExtensionDtype): type = None # type: Type na_value = np.nan - def __repr__(self): + def __repr__(self) -> str: sign = "U" if self.is_unsigned_integer else "" return "{sign}Int{size}Dtype()".format(sign=sign, size=8 * self.itemsize) diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 869019cd3d222..cc41797e7872b 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -860,7 +860,7 @@ def _format_data(self): return summary - def __repr__(self): + def __repr__(self) -> str: template = ( "{class_name}" "{data}\n" diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index bf7404e8997c6..6f2bb095a014d 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -44,7 +44,7 @@ def __init__(self, dtype): self._name = dtype.name self._type = dtype.type - def __repr__(self): + def __repr__(self) -> str: return "PandasDtype({!r})".format(self.name) @property diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index e1691de234335..075cdf09d531f 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -1515,7 +1515,7 @@ def _add_comparison_ops(cls): # ---------- # Formatting # ----------- - def __repr__(self): + def __repr__(self) -> str: return "{self}\nFill: {fill}\n{index}".format( self=printing.pprint_thing(self), fill=printing.pprint_thing(self.fill_value), diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index 6fd73ae14fff1..4de958cc386b9 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -165,7 +165,7 @@ def subtype(self): def name(self): return "Sparse[{}, {}]".format(self.subtype.name, self.fill_value) - def __repr__(self): + def __repr__(self) -> str: return self.name @classmethod diff --git a/pandas/core/base.py b/pandas/core/base.py index 61dc5f35cadf7..bab358aa5dc8d 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -55,7 +55,7 @@ def _constructor(self): """class constructor (for this class it's just `__class__`""" return self.__class__ - def __repr__(self): + def __repr__(self) -> str: """ Return a string representation for a particular object. """ diff --git a/pandas/core/computation/expr.py b/pandas/core/computation/expr.py index 72367c8fb7a4f..39653c3d695b2 100644 --- a/pandas/core/computation/expr.py +++ b/pandas/core/computation/expr.py @@ -834,7 +834,7 @@ def assigner(self): def __call__(self): return self.terms(self.env) - def __repr__(self): + def __repr__(self) -> str: return printing.pprint_thing(self.terms) def __len__(self): diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index dc0f381414970..fe74b6994be7c 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -82,7 +82,7 @@ def __init__(self, name, env, side=None, encoding=None): def local_name(self): return self.name.replace(_LOCAL_TAG, "") - def __repr__(self): + def __repr__(self) -> str: return pprint_thing(self.name) def __call__(self, *args, **kwargs): @@ -182,7 +182,7 @@ def _resolve_name(self): def name(self): return self.value - def __repr__(self): + def __repr__(self) -> str: # in python 2 str() of float # can truncate shorter than repr() return repr(self.name) @@ -204,7 +204,7 @@ def __init__(self, op, operands, *args, **kwargs): def __iter__(self): return iter(self.operands) - def __repr__(self): + def __repr__(self) -> str: """ Print a generic n-ary operator and its operands using infix notation. """ @@ -557,7 +557,7 @@ def __call__(self, env): operand = self.operand(env) return self.func(operand) - def __repr__(self): + def __repr__(self) -> str: return pprint_thing("{0}({1})".format(self.op, self.operand)) @property @@ -582,7 +582,7 @@ def __call__(self, env): with np.errstate(all="ignore"): return self.func.func(*operands) - def __repr__(self): + def __repr__(self) -> str: operands = map(str, self.operands) return pprint_thing("{0}({1})".format(self.op, ",".join(operands))) diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 81658ab23ba46..3a2ea30cbc8b9 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -229,7 +229,7 @@ def convert_values(self): class FilterBinOp(BinOp): - def __repr__(self): + def __repr__(self) -> str: return pprint_thing( "[Filter : [{lhs}] -> [{op}]".format(lhs=self.filter[0], op=self.filter[1]) ) @@ -295,7 +295,7 @@ def evaluate(self): class ConditionBinOp(BinOp): - def __repr__(self): + def __repr__(self) -> str: return pprint_thing("[Condition : [{cond}]]".format(cond=self.condition)) def invert(self): @@ -545,7 +545,7 @@ def __init__(self, where, queryables=None, encoding=None, scope_level=0): ) self.terms = self.parse() - def __repr__(self): + def __repr__(self) -> str: if self.terms is not None: return pprint_thing(self.terms) return pprint_thing(self.expr) diff --git a/pandas/core/computation/scope.py b/pandas/core/computation/scope.py index b11411eb2dc66..81c7b04bf3284 100644 --- a/pandas/core/computation/scope.py +++ b/pandas/core/computation/scope.py @@ -139,7 +139,7 @@ def __init__( self.resolvers = DeepChainMap(*resolvers) self.temps = {} - def __repr__(self): + def __repr__(self) -> str: scope_keys = _get_pretty_string(list(self.scope.keys())) res_keys = _get_pretty_string(list(self.resolvers.keys())) unicode_str = "{name}(scope={scope_keys}, resolvers={res_keys})" diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 59ef17e3d121f..7d98a42e06257 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -71,7 +71,7 @@ class property**. _metadata = () # type: Tuple[str, ...] - def __str__(self): + def __str__(self) -> str: return self.name def __eq__(self, other): diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 7dca588e33839..4a4ad076f14ca 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -415,7 +415,7 @@ def __eq__(self, other: Any) -> bool: return True return hash(self) == hash(other) - def __repr__(self): + def __repr__(self) -> str_type: tpl = "CategoricalDtype(categories={}ordered={})" if self.categories is None: data = "None, " @@ -752,7 +752,7 @@ def construct_from_string(cls, string): raise TypeError("Could not construct DatetimeTZDtype") - def __str__(self): + def __str__(self) -> str_type: return "datetime64[{unit}, {tz}]".format(unit=self.unit, tz=self.tz) @property @@ -889,7 +889,7 @@ def construct_from_string(cls, string): pass raise TypeError("could not construct PeriodDtype") - def __str__(self): + def __str__(self) -> str_type: return self.name @property @@ -1068,7 +1068,7 @@ def construct_from_string(cls, string): def type(self): return Interval - def __str__(self): + def __str__(self) -> str_type: if self.subtype is None: return "interval" return "interval[{subtype}]".format(subtype=self.subtype) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index bafc37d478fdb..2c778b7011f76 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2107,7 +2107,7 @@ def __setstate__(self, state): # ---------------------------------------------------------------------- # Rendering Methods - def __repr__(self): + def __repr__(self) -> str: # string representation based upon iterating over self # (since, by definition, `PandasContainers` are iterable) prepr = "[%s]" % ",".join(map(pprint_thing, self)) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 81ba594c97391..31d6e2206f569 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -402,7 +402,7 @@ def __init__( def __len__(self): return len(self.groups) - def __repr__(self): + def __repr__(self) -> str: # TODO: Better repr for GroupBy object return object.__repr__(self) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index dc6336b17ac1e..ff3b4b1096ecb 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -210,7 +210,7 @@ def _set_grouper(self, obj, sort=False): def groups(self): return self.grouper.groups - def __repr__(self): + def __repr__(self) -> str: attrs_list = ( "{}={!r}".format(attr_name, getattr(self, attr_name)) for attr_name in self._attributes @@ -372,7 +372,7 @@ def __init__( self.grouper = self.grouper.astype("timedelta64[ns]") - def __repr__(self): + def __repr__(self) -> str: return "Grouping({0})".format(self.name) def __iter__(self): diff --git a/pandas/core/indexes/frozen.py b/pandas/core/indexes/frozen.py index 4791ea2b70691..08c86b81b59c0 100644 --- a/pandas/core/indexes/frozen.py +++ b/pandas/core/indexes/frozen.py @@ -105,10 +105,10 @@ def _disabled(self, *args, **kwargs): ) ) - def __str__(self): + def __str__(self) -> str: return pprint_thing(self, quote_strings=True, escape_chars=("\t", "\r", "\n")) - def __repr__(self): + def __repr__(self) -> str: return "%s(%s)" % (self.__class__.__name__, str(self)) __setitem__ = __setslice__ = __delitem__ = __delslice__ = _disabled @@ -148,7 +148,7 @@ def values(self): arr = self.view(np.ndarray).copy() return arr - def __repr__(self): + def __repr__(self) -> str: """ Return a string representation for this object. """ diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 448d2faf8b85f..fd5d3f2247a90 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -268,7 +268,7 @@ def make_block_same_class(self, values, placement=None, ndim=None, dtype=None): values, placement=placement, ndim=ndim, klass=self.__class__, dtype=dtype ) - def __repr__(self): + def __repr__(self) -> str: # don't want to print out all of the items here name = pprint_thing(self.__class__.__name__) if self._is_single_block: diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index 4ba485c85d8ba..f981c00fdad36 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -120,7 +120,7 @@ def __init__(self, block, shape, indexers=None): self.indexers = indexers self.shape = shape - def __repr__(self): + def __repr__(self) -> str: return "{name}({block!r}, {indexers})".format( name=self.__class__.__name__, block=self.block, indexers=self.indexers ) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 21ae820cfcee6..74cebd8b59fba 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -325,7 +325,7 @@ def _post_setstate(self): def __len__(self): return len(self.items) - def __repr__(self): + def __repr__(self) -> str: output = pprint_thing(self.__class__.__name__) for i, ax in enumerate(self.axes): if i == 0: diff --git a/pandas/core/resample.py b/pandas/core/resample.py index e418461883e6c..6d877bf666881 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -86,7 +86,7 @@ def __init__(self, obj, groupby=None, axis=0, kind=None, **kwargs): if self.groupby is not None: self.groupby._set_grouper(self._convert_obj(obj), sort=True) - def __str__(self): + def __str__(self) -> str: """ Provide a nice str repr of our rolling object. """ diff --git a/pandas/core/series.py b/pandas/core/series.py index 73a05b4cdfa66..bdc319c6d7bab 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1555,7 +1555,7 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False): # ---------------------------------------------------------------------- # Rendering Methods - def __repr__(self): + def __repr__(self) -> str: """ Return a string representation for a particular Series. """ diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index 883af5c2e62f0..73cc40ae0e0d3 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -174,7 +174,7 @@ def __init__(self, class_instance, methodtype="method"): self.methodtype = methodtype self.class_instance = class_instance - def __str__(self): + def __str__(self) -> str: if self.methodtype == "classmethod": name = self.class_instance.__name__ else: diff --git a/pandas/io/msgpack/exceptions.py b/pandas/io/msgpack/exceptions.py index 40f5a8af8f583..2966f69920930 100644 --- a/pandas/io/msgpack/exceptions.py +++ b/pandas/io/msgpack/exceptions.py @@ -19,7 +19,7 @@ def __init__(self, unpacked, extra): self.unpacked = unpacked self.extra = extra - def __str__(self): + def __str__(self) -> str: return "unpack(b) received extra data." diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 35e6d53127e59..8580e0069ccdf 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -543,7 +543,7 @@ def __contains__(self, key): def __len__(self): return len(self.groups()) - def __repr__(self): + def __repr__(self) -> str: return "{type}\nFile path: {path}\n".format( type=type(self), path=pprint_thing(self._path) ) @@ -1725,7 +1725,7 @@ def set_table(self, table): self.table = table return self - def __repr__(self): + def __repr__(self) -> str: temp = tuple( map(pprint_thing, (self.name, self.cname, self.axis, self.pos, self.kind)) ) @@ -2052,7 +2052,7 @@ def __init__( self.set_data(data) self.set_metadata(metadata) - def __repr__(self): + def __repr__(self) -> str: temp = tuple( map( pprint_thing, (self.name, self.cname, self.dtype, self.kind, self.shape) @@ -2518,7 +2518,7 @@ def pandas_type(self): def format_type(self): return "fixed" - def __repr__(self): + def __repr__(self) -> str: """ return a pretty representation of myself """ self.infer_axes() s = self.shape @@ -3213,7 +3213,7 @@ def table_type_short(self): def format_type(self): return "table" - def __repr__(self): + def __repr__(self) -> str: """ return a pretty representation of myself """ self.infer_axes() dc = ",dc->[{columns}]".format( diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 07475f224bd5f..d62c3f7d2e3b8 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -862,10 +862,10 @@ def __init__(self, value): lambda self: self._value, doc="The binary representation of the missing value." ) - def __str__(self): + def __str__(self) -> str: return self.string - def __repr__(self): + def __repr__(self) -> str: # not perfect :-/ return "{cls}({obj})".format(cls=self.__class__, obj=self) diff --git a/pandas/tests/extension/decimal/array.py b/pandas/tests/extension/decimal/array.py index a1988744d76a1..93816e3a8a613 100644 --- a/pandas/tests/extension/decimal/array.py +++ b/pandas/tests/extension/decimal/array.py @@ -22,7 +22,7 @@ class DecimalDtype(ExtensionDtype): def __init__(self, context=None): self.context = context or decimal.getcontext() - def __repr__(self): + def __repr__(self) -> str: return "DecimalDtype(context={})".format(self.context) @classmethod diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index 11d73fc37105e..9b76be18b0e88 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -341,7 +341,7 @@ def __init__(self, name, color): self.name = name self.color = color - def __str__(self): + def __str__(self) -> str: return "".format(self=self) # necessary for pretty KeyError @@ -380,7 +380,7 @@ def test_set_index_custom_label_hashable_iterable(self): class Thing(frozenset): # need to stabilize repr for KeyError (due to random order in sets) - def __repr__(self): + def __repr__(self) -> str: tmp = sorted(list(self)) # double curly brace prints one brace in format string return "frozenset({{{}}})".format(", ".join(map(repr, tmp))) @@ -418,7 +418,7 @@ def __init__(self, name, color): self.name = name self.color = color - def __str__(self): + def __str__(self) -> str: return "".format(self=self) thing1 = Thing("One", "red") diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index d611dc5497cca..d6d3763981131 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -591,7 +591,7 @@ class TO: def __init__(self, value): self.value = value - def __str__(self): + def __str__(self) -> str: return "[{0}]".format(self.value) __repr__ = __str__ diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 16f14f35fdbae..ee7fca6ec7672 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1185,10 +1185,10 @@ def __init__(self, value, dtype): def __array__(self): return np.array(self.value, dtype=self.dtype) - def __str__(self): + def __str__(self) -> str: return "DummyElement({}, {})".format(self.value, self.dtype) - def __repr__(self): + def __repr__(self) -> str: return str(self) def astype(self, dtype, copy=False): diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index eaa46c4e9dc9b..aa065b6e13079 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -594,7 +594,7 @@ def __init__(self, hexed): self.hexed = hexed self.binary = bytes.fromhex(hexed) - def __str__(self): + def __str__(self) -> str: return self.hexed hexed = "574b4454ba8c5eb4f98a8f45" diff --git a/pandas/tests/io/json/test_ujson.py b/pandas/tests/io/json/test_ujson.py index 20e2690084e2a..8dcc77fc2fbc1 100644 --- a/pandas/tests/io/json/test_ujson.py +++ b/pandas/tests/io/json/test_ujson.py @@ -621,7 +621,7 @@ def __init__(self, val): def recursive_attr(self): return _TestObject("recursive_attr") - def __str__(self): + def __str__(self) -> str: return str(self.val) msg = "Maximum recursion level reached" diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 9f881f5a5aa29..f1661ad034e4c 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -227,7 +227,7 @@ class County: name = "San Sebastián" state = "PR" - def __repr__(self): + def __repr__(self) -> str: return self.name + ", " + self.state cat = pd.Categorical([County() for _ in range(61)]) diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index 8144a3931b9b8..c8a127f89bf91 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -285,7 +285,7 @@ def __add__(self, other): def __eq__(self, other): return type(other) is Thing and self.value == other.value - def __repr__(self): + def __repr__(self) -> str: return "Thing({})".format(self.value) s = pd.Series([Thing(1), Thing(2)]) diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index eb8600031439f..d4f02286ff8d6 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -183,7 +183,7 @@ class from pandas.tseries.offsets assert days_of_week is None or type(days_of_week) == tuple self.days_of_week = days_of_week - def __repr__(self): + def __repr__(self) -> str: info = "" if self.year is not None: info += "year={year}, ".format(year=self.year) diff --git a/pandas/util/_depr_module.py b/pandas/util/_depr_module.py index 54f090ede3fc4..45e7db9281837 100644 --- a/pandas/util/_depr_module.py +++ b/pandas/util/_depr_module.py @@ -38,7 +38,7 @@ def __dir__(self): deprmodule = self._import_deprmod() return dir(deprmodule) - def __repr__(self): + def __repr__(self) -> str: deprmodule = self._import_deprmod() return repr(deprmodule)