Skip to content

Conversation

@timhoffm
Copy link
Contributor

@timhoffm timhoffm commented Aug 1, 2023

self._attrs is always initialized to an empty dict per

object.__setattr__(self, "_attrs", {})

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:

object.__setattr__(self, "_attrs", attrs)

AFAICS (including code history) I do not see a reason that this could be None.
But if we wanted to be very defensive, we should do the dict enforcing here.

@mroeschke
Copy link
Member

But if we wanted to be very defensive, we should do the dict enforcing here.

This would be a good change for this PR

@mroeschke mroeschke added the metadata _metadata, .attrs label Aug 1, 2023
`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.
@timhoffm
Copy link
Contributor Author

timhoffm commented Aug 1, 2023

I've now included the dict-enforcement in __setstate__.

@mroeschke mroeschke added this to the 2.1 milestone Aug 2, 2023
@mroeschke mroeschke merged commit 292eb32 into pandas-dev:main Aug 2, 2023
@mroeschke
Copy link
Member

Thanks @timhoffm

@timhoffm timhoffm deleted the attrs-property branch August 2, 2023 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

metadata _metadata, .attrs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants