diff options
| author | Adam Johnson <me@adamj.eu> | 2024-08-22 19:27:23 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2025-02-16 19:32:36 +0100 |
| commit | c07ba43c4bc94272c3d4a13c8f468b94db1e655c (patch) | |
| tree | 1edce103f65719119c143ec3a4f60d425451a604 /django/utils/copy.py | |
| parent | 2d34ebe49a25d0974392583d5bbd954baf742a32 (diff) | |
Refs #35704 -- Used copy.replace() in Operation.reduce() methods.
Diffstat (limited to 'django/utils/copy.py')
| -rw-r--r-- | django/utils/copy.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/django/utils/copy.py b/django/utils/copy.py new file mode 100644 index 0000000000..dd0cd729b0 --- /dev/null +++ b/django/utils/copy.py @@ -0,0 +1,17 @@ +from django.utils.version import PY313 + +if PY313: + from copy import replace +else: + # Backport of copy.replace() from Python 3.13. + def replace(obj, /, **changes): + """Return a new object replacing specified fields with new values. + + This is especially useful for immutable objects, like named tuples or + frozen dataclasses. + """ + cls = obj.__class__ + func = getattr(cls, "__replace__", None) + if func is None: + raise TypeError(f"replace() does not support {cls.__name__} objects") + return func(obj, **changes) |
