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

Function _replace

Lib/dataclasses.py:1773–1804  ·  view source on GitHub ↗
(self, /, **changes)

Source from the content-addressed store, hash-verified

1771
1772
1773def _replace(self, /, **changes):
1774 # We're going to mutate 'changes', but that's okay because it's a
1775 # new dict, even if called with 'replace(self, **my_changes)'.
1776
1777 # It's an error to have init=False fields in 'changes'.
1778 # If a field is not in 'changes', read its value from the provided 'self'.
1779
1780 for f in getattr(self, _FIELDS).values():
1781 # Only consider normal fields or InitVars.
1782 if f._field_type is _FIELD_CLASSVAR:
1783 continue
1784
1785 if not f.init:
1786 # Error if this field is specified in changes.
1787 if f.name in changes:
1788 raise TypeError(f'field {f.name} is declared with '
1789 f'init=False, it cannot be specified with '
1790 f'replace()')
1791 continue
1792
1793 if f.name not in changes:
1794 if f._field_type is _FIELD_INITVAR and f.default is MISSING:
1795 raise TypeError(f"InitVar {f.name!r} "
1796 f'must be specified with replace()')
1797 changes[f.name] = getattr(self, f.name)
1798
1799 # Create the new object, which calls __init__() and
1800 # __post_init__() (if defined), using all of the init fields we've
1801 # added and/or left in 'changes'. If there are values supplied in
1802 # changes that aren't fields, this will correctly raise a
1803 # TypeError.
1804 return self.__class__(**changes)

Callers 1

replaceFunction · 0.70

Calls 3

getattrFunction · 0.85
valuesMethod · 0.45
__class__Method · 0.45

Tested by

no test coverage detected