Skip to content
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

Always check for __dictrefoffset__ and __weakoffset__ in dataclasses with slots #118829

Open
sobolevn opened this issue May 9, 2024 · 0 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented May 9, 2024

Bug report

See #118099 (comment) and #118033

We now correctly check for __dictoffset__ and __weakoffset__ on C types without __slots__ defined:

cpython/Lib/dataclasses.py

Lines 1201 to 1212 in c68acb1

match cls.__dict__.get('__slots__'):
# `__dictoffset__` and `__weakrefoffset__` can tell us whether
# the base type has dict/weakref slots, in a way that works correctly
# for both Python classes and C extension types. Extension types
# don't use `__slots__` for slot creation
case None:
slots = []
if getattr(cls, '__weakrefoffset__', -1) != 0:
slots.append('__weakref__')
if getattr(cls, '__dictrefoffset__', -1) != 0:
slots.append('__dict__')
yield from slots

But, not in this case:

cpython/Lib/dataclasses.py

Lines 1213 to 1218 in c68acb1

case str(slot):
yield slot
# Slots may be any iterable, but we cannot handle an iterator
# because it will already be (partially) consumed.
case iterable if not hasattr(iterable, '__next__'):
yield from iterable

So, in theory there might be C types with __slots__ and __dictoffset__ and __weakoffset__.

I will investigate!

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels May 9, 2024
@sobolevn sobolevn self-assigned this May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant