Add dunder methods based on the fields defined in the class. Examines PEP 526 __annotations__ to determine fields. If init is true, an __init__() method is added to the class. If repr is true, a __repr__() method is added. If order is true, rich comparison dunder methods are added.
(cls=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)
| 1413 | |
| 1414 | |
| 1415 | def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False, |
| 1416 | unsafe_hash=False, frozen=False, match_args=True, |
| 1417 | kw_only=False, slots=False, weakref_slot=False): |
| 1418 | """Add dunder methods based on the fields defined in the class. |
| 1419 | |
| 1420 | Examines PEP 526 __annotations__ to determine fields. |
| 1421 | |
| 1422 | If init is true, an __init__() method is added to the class. If repr |
| 1423 | is true, a __repr__() method is added. If order is true, rich |
| 1424 | comparison dunder methods are added. If unsafe_hash is true, a |
| 1425 | __hash__() method is added. If frozen is true, fields may not be |
| 1426 | assigned to after instance creation. If match_args is true, the |
| 1427 | __match_args__ tuple is added. If kw_only is true, then by default |
| 1428 | all fields are keyword-only. If slots is true, a new class with a |
| 1429 | __slots__ attribute is returned. |
| 1430 | """ |
| 1431 | |
| 1432 | def wrap(cls): |
| 1433 | return _process_class(cls, init, repr, eq, order, unsafe_hash, |
| 1434 | frozen, match_args, kw_only, slots, |
| 1435 | weakref_slot) |
| 1436 | |
| 1437 | # See if we're being called as @dataclass or @dataclass(). |
| 1438 | if cls is None: |
| 1439 | # We're called with parens. |
| 1440 | return wrap |
| 1441 | |
| 1442 | # We're called as @dataclass without parens. |
| 1443 | return wrap(cls) |
| 1444 | |
| 1445 | |
| 1446 | def fields(class_or_instance): |
searching dependent graphs…