Skip to content

Commit

Permalink
Merge pull request #6 from harryfyx/harryfyx-patch-1
Browse files Browse the repository at this point in the history
Fix copy function.
  • Loading branch information
LarsHill authored Oct 27, 2022
2 parents c19b3c7 + 09caaa3 commit 12ec382
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion metadict/metadict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import copy
import keyword
import re
import warnings
Expand Down Expand Up @@ -162,7 +163,7 @@ def copy(self) -> 'MetaDict':
def __copy__(self) -> 'MetaDict':
cls = self.__class__
result = cls.__new__(cls)
result.__dict__.update(self.__dict__)
result.__dict__.update({k: copy.copy(v) for k, v in self.__dict__.items()})
return result

@classmethod
Expand Down
18 changes: 18 additions & 0 deletions tests/test_metadict.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,21 @@ def test_warning_protected_key():
('100', False), (100, False), ((1, 2, 3), False)])
def test_complies_variable_syntax(name: Any, expected: bool):
assert complies_variable_syntax(name) == expected


def test_copy():
cfg = MetaDict(a=1)
cfg2 = cfg.copy()
cfg2.a = 2
assert cfg.a == 1
assert cfg2.a == 2


def test_copy_recursive():
cfg = MetaDict()
cfg2 = MetaDict(a=cfg)
cfg.a = cfg2
cfg3 = cfg.copy()
assert cfg3.a == cfg2
assert cfg3.a.a == cfg
assert cfg3.a.a.a == cfg2

0 comments on commit 12ec382

Please sign in to comment.