Skip to content

Commit 292eb32

Browse files
timhoffmTim Hoffmann
andauthored
CLN: Remove None check in attrs property lookup (#54364)
`self._attrs` is always initialized to an empty dict per https://github.com/pandas-dev/pandas/blob/c93e8034a13d3dbe2358b1b2f868a0d54d1034a7/pandas/core/generic.py#L275 The attribute `_attrs` is only witten to in two other places a) in the attrs.setter property (which enforces dict as well) b) in __setstate__, which takes whatever the state is: https://github.com/pandas-dev/pandas/blob/c93e8034a13d3dbe2358b1b2f868a0d54d1034a7/pandas/core/generic.py#L2129C24-L2129C24 AFAICS (including code history) I do not see a reason that this could be None. But if we want to be very defensive, we should do the dict enforcing here. Co-authored-by: Tim Hoffmann <tim.hoffmann@zeiss.com>
1 parent fb6f704 commit 292eb32

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

pandas/core/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,6 @@ def attrs(self) -> dict[Hashable, Any]:
381381
>>> df.attrs
382382
{'A': [10, 20, 30]}
383383
"""
384-
if self._attrs is None:
385-
self._attrs = {}
386384
return self._attrs
387385

388386
@attrs.setter
@@ -2126,6 +2124,8 @@ def __setstate__(self, state) -> None:
21262124
typ = state.get("_typ")
21272125
if typ is not None:
21282126
attrs = state.get("_attrs", {})
2127+
if attrs is None: # should not happen, but better be on the safe side
2128+
attrs = {}
21292129
object.__setattr__(self, "_attrs", attrs)
21302130
flags = state.get("_flags", {"allows_duplicate_labels": True})
21312131
object.__setattr__(self, "_flags", Flags(self, **flags))

0 commit comments

Comments
 (0)