Skip to content

Commit

Permalink
Allow objects to be deepcopied (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdomke authored Aug 13, 2024
1 parent a5b1595 commit e5603a2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Changelog

Versions follow `CalVer <http://www.calver.org/>`_ with the scheme ``YY.0M.Micro``.

Unreleased
----------
Added
~~~~~
* Allow ``BIC`` and ``IBAN`` objects to be deepcopied (thanks to `@binaryDiv <https://github.com/binaryDiv>`_
for pointing this out).

`2024.08.0`_ - 2024/08/13
-------------------------
Added
Expand Down
3 changes: 3 additions & 0 deletions schwifty/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)``"""
Expand Down
14 changes: 14 additions & 0 deletions tests/test_bic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import copy

import pytest
from pycountry import countries # type: ignore

Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions tests/test_iban.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import copy

import pytest
from pycountry import countries # type: ignore

Expand Down Expand Up @@ -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

0 comments on commit e5603a2

Please sign in to comment.