MCPcopy Index your code
hub / github.com/RustPython/RustPython / make_dataclass

Function make_dataclass

Lib/dataclasses.py:1627–1751  ·  view source on GitHub ↗

Return a new dynamically created dataclass. The dataclass name will be 'cls_name'. 'fields' is an iterable of either (name), (name, type) or (name, type, Field) objects. If type is omitted, use the string 'typing.Any'. Field objects are created by the equivalent of calling 'field(

(cls_name, fields, *, bases=(), namespace=None, init=True,
                   repr=True, eq=True, order=False, unsafe_hash=False,
                   frozen=False, match_args=True, kw_only=False, slots=False,
                   weakref_slot=False, module=None, decorator=dataclass)

Source from the content-addressed store, hash-verified

1625
1626
1627def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
1628 repr=True, eq=True, order=False, unsafe_hash=False,
1629 frozen=False, match_args=True, kw_only=False, slots=False,
1630 weakref_slot=False, module=None, decorator=dataclass):
1631 """Return a new dynamically created dataclass.
1632
1633 The dataclass name will be 'cls_name'. 'fields' is an iterable
1634 of either (name), (name, type) or (name, type, Field) objects. If type is
1635 omitted, use the string 'typing.Any'. Field objects are created by
1636 the equivalent of calling 'field(name, type [, Field-info])'.::
1637
1638 C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))
1639
1640 is equivalent to::
1641
1642 @dataclass
1643 class C(Base):
1644 x: 'typing.Any'
1645 y: int
1646 z: int = field(init=False)
1647
1648 For the bases and namespace parameters, see the builtin type() function.
1649
1650 The parameters init, repr, eq, order, unsafe_hash, frozen, match_args, kw_only,
1651 slots, and weakref_slot are passed to dataclass().
1652
1653 If module parameter is defined, the '__module__' attribute of the dataclass is
1654 set to that value.
1655 """
1656
1657 if namespace is None:
1658 namespace = {}
1659
1660 # While we're looking through the field names, validate that they
1661 # are identifiers, are not keywords, and not duplicates.
1662 seen = set()
1663 annotations = {}
1664 defaults = {}
1665 for item in fields:
1666 if isinstance(item, str):
1667 name = item
1668 tp = _ANY_MARKER
1669 elif len(item) == 2:
1670 name, tp, = item
1671 elif len(item) == 3:
1672 name, tp, spec = item
1673 defaults[name] = spec
1674 else:
1675 raise TypeError(f'Invalid field: {item!r}')
1676
1677 if not isinstance(name, str) or not name.isidentifier():
1678 raise TypeError(f'Field names must be valid identifiers: {name!r}')
1679 if keyword.iskeyword(name):
1680 raise TypeError(f'Field names must not be keywords: {name!r}')
1681 if name in seen:
1682 raise TypeError(f'Field name duplicated: {name!r}')
1683
1684 seen.add(name)

Callers 15

test_generic_dynamicMethod · 0.85
__init__.pyFile · 0.85
test_simpleMethod · 0.85
test_baseMethod · 0.85
test_base_dataclassMethod · 0.85
test_init_varMethod · 0.85
test_class_varMethod · 0.85
test_other_paramsMethod · 0.85

Calls 8

setFunction · 0.85
isinstanceFunction · 0.85
lenFunction · 0.85
new_classMethod · 0.80
decoratorFunction · 0.70
isidentifierMethod · 0.45
addMethod · 0.45
getMethod · 0.45

Tested by 15

test_generic_dynamicMethod · 0.68
test_simpleMethod · 0.68
test_baseMethod · 0.68
test_base_dataclassMethod · 0.68
test_init_varMethod · 0.68
test_class_varMethod · 0.68
test_other_paramsMethod · 0.68
test_no_typesMethod · 0.68