mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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:
parent
0a3577bdfc
commit
9c7657f099
5 changed files with 81 additions and 21 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue