Skip to content

gh-129915: Add __qualname__ to class namespace when constructing slotted dataclass #129916

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,11 +1299,12 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
# Clear existing `__weakref__` descriptor, it belongs to a previous type:
cls_dict.pop('__weakref__', None) # gh-102069

# Set the `__qualname__ accordingly
if (qualname := getattr(cls, '__qualname__', None)) is not None:
cls_dict['__qualname__'] = qualname

# And finally create the class.
qualname = getattr(cls, '__qualname__', None)
newcls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
if qualname is not None:
newcls.__qualname__ = qualname

if is_frozen:
# Need this for pickling frozen classes with slots.
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3367,6 +3367,24 @@ class A:
self.assertFalse(hasattr(A, "__slots__"))
self.assertTrue(hasattr(B, "__slots__"))

def test_slots_qualname(self):
# Test that __qualname__ is set correctly when using slots
@dataclass(slots=True)
class C:
x: int

def __init_subclass__(cls):
expected = f'TestSlots.test_slots_qualname.<locals>.{cls.__name__}'
self.assertEqual(cls.__qualname__, expected)

self.assertEqual(C.__qualname__, 'TestSlots.test_slots_qualname.<locals>.C')

@dataclass(slots=True)
class D(C):
pass

self.assertEqual(D.__qualname__, 'TestSlots.test_slots_qualname.<locals>.D')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please run your tests as -m test test_dataclasses and as python.exe Lib/test/test_dataclasses/__init__.py directly? qualname might be changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not following how running it directly would produce a different result than the passing CI check? What am I missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume @sobolevn thinks the imported class name might change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am not sure it will, just double checking.


# Can't be local to test_frozen_pickle.
@dataclass(frozen=True, slots=True)
class FrozenSlotsClass:
Expand Down
Loading