Skip to content
Merged
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
3 changes: 3 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ def _get_field(cls, a_name, a_type):
if isinstance(default, Field):
f = default
else:
if isinstance(default, types.MemberDescriptorType):
# This is a field in __slots__, so it has no default value.
default = MISSING
f = field(default=default)

# Assume it's a normal field until proven otherwise.
Expand Down
42 changes: 42 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2564,5 +2564,47 @@ class S(D):
self.assertEqual(s.cached, True)


class TestSlots(unittest.TestCase):
def test_simple(self):
@dataclass
class C:
__slots__ = ('x',)
x: Any

# There was a bug where a variable in a slot was assumed
# to also have a default value (of type types.MemberDescriptorType).
with self.assertRaisesRegex(TypeError,
"__init__\(\) missing 1 required positional argument: 'x'"):
C()

# We can create an instance, and assign to x.
c = C(10)
self.assertEqual(c.x, 10)
c.x = 5
self.assertEqual(c.x, 5)

# We can't assign to anything else.
with self.assertRaisesRegex(AttributeError, "'C' object has no attribute 'y'"):
c.y = 5

def test_derived_added_field(self):
# See bpo-33100.
@dataclass
class Base:
__slots__ = ('x',)
x: Any

@dataclass
class Derived(Base):
x: int
y: int

d = Derived(1, 2)
self.assertEqual((d.x, d.y), (1, 2))

# We can add a new field to the derived instance.
d.z = 10


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Dataclasses: If a field has a default value that's a MemberDescriptorType,
then it's from that field being in __slots__, not an actual default value.