gh-113878: Add doc parameter to dataclasses.field (gh-114051)

If using `slots=True`, the `doc` parameter ends up in the `__slots__` dict. The `doc` parameter is also in the corresponding `Field` object.
This commit is contained in:
sobolevn 2024-09-27 19:20:49 +03:00 committed by GitHub
parent 0a3577bdfc
commit 9c7657f099
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 81 additions and 21 deletions

View file

@ -61,7 +61,7 @@ class TestCase(unittest.TestCase):
x: int = field(default=1, default_factory=int)
def test_field_repr(self):
int_field = field(default=1, init=True, repr=False)
int_field = field(default=1, init=True, repr=False, doc='Docstring')
int_field.name = "id"
repr_output = repr(int_field)
expected_output = "Field(name='id',type=None," \
@ -69,6 +69,7 @@ class TestCase(unittest.TestCase):
"init=True,repr=False,hash=None," \
"compare=True,metadata=mappingproxy({})," \
f"kw_only={MISSING!r}," \
"doc='Docstring'," \
"_field_type=None)"
self.assertEqual(repr_output, expected_output)
@ -3304,7 +3305,7 @@ class TestSlots(unittest.TestCase):
j: str
h: str
self.assertEqual(Base.__slots__, ('y', ))
self.assertEqual(Base.__slots__, ('y',))
@dataclass(slots=True)
class Derived(Base):
@ -3314,7 +3315,7 @@ class TestSlots(unittest.TestCase):
k: str
h: str
self.assertEqual(Derived.__slots__, ('z', ))
self.assertEqual(Derived.__slots__, ('z',))
@dataclass
class AnotherDerived(Base):
@ -3322,6 +3323,24 @@ class TestSlots(unittest.TestCase):
self.assertNotIn('__slots__', AnotherDerived.__dict__)
def test_slots_with_docs(self):
class Root:
__slots__ = {'x': 'x'}
@dataclass(slots=True)
class Base(Root):
y1: int = field(doc='y1')
y2: int
self.assertEqual(Base.__slots__, {'y1': 'y1', 'y2': None})
@dataclass(slots=True)
class Child(Base):
z1: int = field(doc='z1')
z2: int
self.assertEqual(Child.__slots__, {'z1': 'z1', 'z2': None})
def test_cant_inherit_from_iterator_slots(self):
class Root: