From e5603a28e7e2fb534bd165c31b621c2ebf94c553 Mon Sep 17 00:00:00 2001 From: Martin Domke Date: Tue, 13 Aug 2024 11:14:07 +0200 Subject: [PATCH] Allow objects to be deepcopied (#218) --- CHANGELOG.rst | 7 +++++++ schwifty/common.py | 3 +++ tests/test_bic.py | 14 ++++++++++++++ tests/test_iban.py | 14 ++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 535eb7c..6d439e2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,13 @@ Changelog Versions follow `CalVer `_ with the scheme ``YY.0M.Micro``. +Unreleased +---------- +Added +~~~~~ +* Allow ``BIC`` and ``IBAN`` objects to be deepcopied (thanks to `@binaryDiv `_ + for pointing this out). + `2024.08.0`_ - 2024/08/13 ------------------------- Added diff --git a/schwifty/common.py b/schwifty/common.py index 99a5f8b..ab3b81a 100644 --- a/schwifty/common.py +++ b/schwifty/common.py @@ -31,6 +31,9 @@ def __eq__(self, other: Any) -> bool: def __lt__(self, other: Any) -> bool: return str(self) < str(other) + def __deepcopy__(self, memo: dict[str, Any] | None = None) -> Self: + return self.__class__(str(self)) + @property def compact(self) -> str: """str: Compact representation of the code. It's preferable to call ``str(obj)``""" diff --git a/tests/test_bic.py b/tests/test_bic.py index 964e8e6..e517219 100644 --- a/tests/test_bic.py +++ b/tests/test_bic.py @@ -1,5 +1,7 @@ from __future__ import annotations +import copy + import pytest from pycountry import countries # type: ignore @@ -251,3 +253,15 @@ class Model(BaseModel): loaded = Model.model_validate_json(dumped) assert loaded == model + + +def test_deepcopy() -> None: + bic = BIC("GENODEM1GLS") + + bic_deepcopy = copy.deepcopy(bic) + assert id(bic) != id(bic_deepcopy) + assert bic == bic_deepcopy + + bic_copy = copy.copy(bic) + assert id(bic) != id(bic_copy) + assert bic == bic_copy diff --git a/tests/test_iban.py b/tests/test_iban.py index 336c2bb..9f7feff 100644 --- a/tests/test_iban.py +++ b/tests/test_iban.py @@ -1,5 +1,7 @@ from __future__ import annotations +import copy + import pytest from pycountry import countries # type: ignore @@ -419,3 +421,15 @@ class Model(BaseModel): ) def test_convert_bban_spec_to_regex(spec: str, regex: str) -> None: assert convert_bban_spec_to_regex(spec) == regex + + +def test_copy() -> None: + iban = IBAN("BE68 5390 0754 7034") + + iban_deepcopy = copy.deepcopy(iban) + assert id(iban) != id(iban_deepcopy) + assert iban == iban_deepcopy + + iban_copy = copy.copy(iban) + assert id(iban) != id(iban_copy) + assert iban == iban_copy