Skip to content

CLN: clean Index._id #37087

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 4 commits into from
Oct 14, 2020
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
21 changes: 13 additions & 8 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
FrozenSet,
Hashable,
List,
NewType,
Optional,
Sequence,
Tuple,
Expand Down Expand Up @@ -139,7 +140,9 @@ def index_arithmetic_method(self, other):


_o_dtype = np.dtype(object)
_Identity = object


_Identity = NewType("_Identity", object)


def _new_Index(cls, d):
Expand Down Expand Up @@ -238,7 +241,7 @@ def _outer_indexer(self, left, right):

_typ = "index"
_data: Union[ExtensionArray, np.ndarray]
_id = None
_id: _Identity
_name: Label = None
# MultiIndex.levels previously allowed setting the index name. We
# don't allow this anymore, and raise if it happens rather than
Expand Down Expand Up @@ -453,8 +456,9 @@ def _simple_new(cls, values, name: Label = None):
result._index_data = values
result._name = name
result._cache = {}
result._reset_identity()

return result._reset_identity()
return result

@cache_readonly
def _constructor(self):
Expand Down Expand Up @@ -558,15 +562,16 @@ def is_(self, other) -> bool:
--------
Index.identical : Works like ``Index.is_`` but also checks metadata.
"""
# use something other than None to be clearer
return self._id is getattr(other, "_id", Ellipsis) and self._id is not None
try:
return self._id is other._id
except AttributeError:
return False

def _reset_identity(self):
def _reset_identity(self) -> None:
"""
Initializes or resets ``_id`` attribute with new object.
"""
self._id = _Identity()
return self
self._id = _Identity(object())

def _cleanup(self):
self._engine.clear_mapping()
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ def __new__(
new_codes = result._verify_integrity()
result._codes = new_codes

return result._reset_identity()
result._reset_identity()

return result

def _validate_codes(self, level: List, code: List):
"""
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/arithmetic/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ def _simple_new(cls, values, name=None, dtype=None):
result._index_data = values
result._name = name
result._calls = 0
result._reset_identity()

return result._reset_identity()
return result

def __add__(self, other):
self._calls += 1
Expand Down