Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3367,6 +3367,25 @@ 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.assertTrue('__qualname__' in C.__dict__)
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