From 0c2c30d9ac854ec58aa61512acb60d81de3eff97 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Tue, 27 Feb 2024 10:48:01 +0800 Subject: [PATCH] fix: `|=` does not work as expected on TOMLDocumentFixes #331Signed-off-by: Frost Ming * fix: `|=` does not work as expected on TOMLDocument Fixes #331 Signed-off-by: Frost Ming --- CHANGELOG.md | 4 ++++ tomlkit/_types.py | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f82970f..810d53b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fixed + +- Support `|` and `|=` operator for tables, and support `+` and `+=` operator for arrays. ([#331](https://github.com/sdispater/tomlkit/issues/331)) + ## [0.12.3] - 2023-11-15 ### Fixed diff --git a/tomlkit/_types.py b/tomlkit/_types.py index cc1847b..8eeb75e 100644 --- a/tomlkit/_types.py +++ b/tomlkit/_types.py @@ -43,9 +43,27 @@ def _new(self: WT, value: Any) -> WT: class _CustomList(MutableSequence, list): """Adds MutableSequence mixin while pretending to be a builtin list""" + def __add__(self, other): + new_list = self.copy() + new_list.extend(other) + return new_list + + def __iadd__(self, other): + self.extend(other) + return self + class _CustomDict(MutableMapping, dict): """Adds MutableMapping mixin while pretending to be a builtin dict""" + def __or__(self, other): + new_dict = self.copy() + new_dict.update(other) + return new_dict + + def __ior__(self, other): + self.update(other) + return self + class _CustomInt(Integral, int): """Adds Integral mixin while pretending to be a builtin int""" @@ -54,7 +72,7 @@ class _CustomFloat(Real, float): def wrap_method( - original_method: Callable[Concatenate[WT, P], Any] + original_method: Callable[Concatenate[WT, P], Any], ) -> Callable[Concatenate[WT, P], Any]: def wrapper(self: WT, *args: P.args, **kwargs: P.kwargs) -> Any: result = original_method(self, *args, **kwargs)