Make type information on make_dataclass() optional.

This commit is contained in:
Eric V. Smith 2018-01-06 15:13:03 -05:00
parent a790b3821c
commit b78c28504e
1 changed files with 13 additions and 8 deletions

View File

@ -6,7 +6,7 @@ Type: Standards Track
Content-Type: text/x-rst Content-Type: text/x-rst
Created: 02-Jun-2017 Created: 02-Jun-2017
Python-Version: 3.7 Python-Version: 3.7
Post-History: 08-Sep-2017, 25-Nov-2017, 30-Nov-2017, 01-Dec-2017, 02-Dec-2017 Post-History: 08-Sep-2017, 25-Nov-2017, 30-Nov-2017, 01-Dec-2017, 02-Dec-2017, 06-Jan-2018
Resolution: https://mail.python.org/pipermail/python-dev/2017-December/151034.html Resolution: https://mail.python.org/pipermail/python-dev/2017-December/151034.html
Notice for Reviewers Notice for Reviewers
@ -576,15 +576,19 @@ Module level helper functions
- ``make_dataclass(cls_name, fields, *, bases=(), namespace=None)``: - ``make_dataclass(cls_name, fields, *, bases=(), namespace=None)``:
Creates a new Data Class with name ``cls_name``, fields as defined Creates a new Data Class with name ``cls_name``, fields as defined
in ``fields``, base classes as given in ``bases``, and initialized in ``fields``, base classes as given in ``bases``, and initialized
with a namespace as given in ``namespace``. This function is not with a namespace as given in ``namespace``. ``fields`` is an
strictly required, because any Python mechanism for creating a new iterable whose elements are either ``name``, ``(name, type)``, or
class with ``__annotations__`` can then apply the ``dataclass`` ``(name, type, Field)``. If just ``name`` is supplied,
function to convert that class to a Data Class. This function is ``typing.Any`` is used for ``type``. This function is not strictly
provided as a convenience. For example:: required, because any Python mechanism for creating a new class with
``__annotations__`` can then apply the ``dataclass`` function to
convert that class to a Data Class. This function is provided as a
convenience. For example::
C = make_dataclass('C', C = make_dataclass('C',
[('x', int), [('x', int),
('y', int, field(default=5))], 'y',
('z', int, field(default=5))],
namespace={'add_one': lambda self: self.x + 1}) namespace={'add_one': lambda self: self.x + 1})
Is equivalent to:: Is equivalent to::
@ -592,7 +596,8 @@ Module level helper functions
@dataclass @dataclass
class C: class C:
x: int x: int
y: int = 5 y: 'typing.Any'
z: int = 5
def add_one(self): def add_one(self):
return self.x + 1 return self.x + 1