Dataclass helpers - libvcs._internal.dataclasses¶

dataclasses utilities.

Note

This is an internal API not covered by versioning policy.

class libvcs._internal.dataclasses.SkipDefaultFieldsReprMixin[source]¶

Bases: object

Skip default fields in dataclass() object representation.

Notes

Credit: Pietro Oldrati, 2022-05-08, Unilicense

https://stackoverflow.com/a/72161437/1396928

Examples

>>> @dataclasses.dataclass()
... class Item:
...     name: str
...     unit_price: float = 1.00
...     quantity_on_hand: int = 0
...
>>> @dataclasses.dataclass(repr=False)
... class ItemWithMixin(SkipDefaultFieldsReprMixin):
...     name: str
...     unit_price: float = 1.00
...     quantity_on_hand: int = 0
...
>>> Item('Test')
Item(name='Test', unit_price=1.0, quantity_on_hand=0)
>>> ItemWithMixin('Test')
ItemWithMixin(name=Test)
>>> Item('Test', quantity_on_hand=2)
Item(name='Test', unit_price=1.0, quantity_on_hand=2)
>>> ItemWithMixin('Test', quantity_on_hand=2)
ItemWithMixin(name=Test, quantity_on_hand=2)

If you want to copy/paste the __repr__() directly, you can omit the repr=False:

>>> @dataclasses.dataclass
... class ItemWithMixin(SkipDefaultFieldsReprMixin):
...     name: str
...     unit_price: float = 1.00
...     quantity_on_hand: int = 0
...     __repr__ = SkipDefaultFieldsReprMixin.__repr__
...
>>> ItemWithMixin('Test')
ItemWithMixin(name=Test)
>>> ItemWithMixin('Test', unit_price=2.00)
ItemWithMixin(name=Test, unit_price=2.0)
>>> item = ItemWithMixin('Test')
>>> item.unit_price = 2.05
>>> item
ItemWithMixin(name=Test, unit_price=2.05)
__repr__()[source]¶

Omit default fields in object representation.

Return type:

str

Parameters:

self (DataclassInstance)

__dict__ = mappingproxy({'__module__': 'libvcs._internal.dataclasses', '__doc__': "Skip default fields in :func:`~dataclasses.dataclass` object representation.\n\n    See Also\n    --------\n    :func:`object representation <repr()>`\n\n    Notes\n    -----\n    Credit: Pietro Oldrati, 2022-05-08, Unilicense\n\n    https://stackoverflow.com/a/72161437/1396928\n\n    Examples\n    --------\n    >>> @dataclasses.dataclass()\n    ... class Item:\n    ...     name: str\n    ...     unit_price: float = 1.00\n    ...     quantity_on_hand: int = 0\n    ...\n\n    >>> @dataclasses.dataclass(repr=False)\n    ... class ItemWithMixin(SkipDefaultFieldsReprMixin):\n    ...     name: str\n    ...     unit_price: float = 1.00\n    ...     quantity_on_hand: int = 0\n    ...\n\n    >>> Item('Test')\n    Item(name='Test', unit_price=1.0, quantity_on_hand=0)\n\n    >>> ItemWithMixin('Test')\n    ItemWithMixin(name=Test)\n\n    >>> Item('Test', quantity_on_hand=2)\n    Item(name='Test', unit_price=1.0, quantity_on_hand=2)\n\n    >>> ItemWithMixin('Test', quantity_on_hand=2)\n    ItemWithMixin(name=Test, quantity_on_hand=2)\n\n    If you want to copy/paste the :meth:`~.__repr__()`\n    directly, you can omit the ``repr=False``:\n\n    >>> @dataclasses.dataclass\n    ... class ItemWithMixin(SkipDefaultFieldsReprMixin):\n    ...     name: str\n    ...     unit_price: float = 1.00\n    ...     quantity_on_hand: int = 0\n    ...     __repr__ = SkipDefaultFieldsReprMixin.__repr__\n    ...\n\n    >>> ItemWithMixin('Test')\n    ItemWithMixin(name=Test)\n\n    >>> ItemWithMixin('Test', unit_price=2.00)\n    ItemWithMixin(name=Test, unit_price=2.0)\n\n    >>> item = ItemWithMixin('Test')\n    >>> item.unit_price = 2.05\n\n    >>> item\n    ItemWithMixin(name=Test, unit_price=2.05)\n    ", '__repr__': <function SkipDefaultFieldsReprMixin.__repr__>, '__dict__': <attribute '__dict__' of 'SkipDefaultFieldsReprMixin' objects>, '__weakref__': <attribute '__weakref__' of 'SkipDefaultFieldsReprMixin' objects>, '__annotations__': {}})¶
__module__ = 'libvcs._internal.dataclasses'¶
__weakref__¶

list of weak references to the object