From 7abba3877b977ab84a3cd86dabf29ccfe2e20887 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann Date: Tue, 1 Aug 2023 23:15:22 +0200 Subject: [PATCH] CLN: Remove None check in attrs property lookup `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. --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index aa6578bbcaf66..8a3a105749800 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -381,8 +381,6 @@ def attrs(self) -> dict[Hashable, Any]: >>> df.attrs {'A': [10, 20, 30]} """ - if self._attrs is None: - self._attrs = {} return self._attrs @attrs.setter @@ -2126,6 +2124,8 @@ def __setstate__(self, state) -> None: typ = state.get("_typ") if typ is not None: attrs = state.get("_attrs", {}) + if attrs is None: # should not happen, but better be on the safe side + attrs = {} object.__setattr__(self, "_attrs", attrs) flags = state.get("_flags", {"allows_duplicate_labels": True}) object.__setattr__(self, "_flags", Flags(self, **flags))