Skip to content

Commit

Permalink
added to __repr__ in pure py version the module name, consistently wi…
Browse files Browse the repository at this point in the history
…th C extension
  • Loading branch information
Marco-Sulla committed Nov 28, 2021
1 parent 3941c20 commit 6da8e8b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
7 changes: 6 additions & 1 deletion frozendict/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ def __repr__(self, *args, **kwargs):
"""

body = super().__repr__(*args, **kwargs)
name = self.__class__.__name__
klass = self.__class__

if klass == frozendict or klass == coold:
name = f"frozendict.{klass.__name__}"
else:
name = klass.__name__

return f"{name}({body})"

Expand Down
27 changes: 21 additions & 6 deletions test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def fd_items(fd_dict):
def fd_empty():
return frozendict_class()

@pytest.fixture
def module_prefix():
try:
sc = subclass
except NameError:
sc = False

if sc:
return ""

return "frozendict."

##############################################################################
# main tests

Expand Down Expand Up @@ -151,19 +163,22 @@ def test_items(fd, fd_dict):
def test_fromkeys(fd, fd_giulia):
assert frozendict_class.fromkeys(["Marco", "Giulia"], "Sulla") == fd_giulia

def test_repr(fd, fd_dict):
def test_repr(fd, fd_dict, module_prefix):
classname = frozendict_class.__name__
repr_fd = repr(fd)
expected_repr = f"{classname}({repr(fd_dict)})"
prefix = module_prefix
expected_repr = f"{prefix}{classname}({repr(fd_dict)})"
assert repr_fd == expected_repr

def test_str(fd, fd_dict):
def test_str(fd, fd_dict, module_prefix):
classname = frozendict_class.__name__
assert str(fd) == f"{classname}({repr(fd_dict)})"
prefix = module_prefix
assert str(fd) == f"{prefix}{classname}({repr(fd_dict)})"

def test_format(fd, fd_dict):
def test_format(fd, fd_dict, module_prefix):
classname = frozendict_class.__name__
assert format(fd) == f"{classname}({repr(fd_dict)})"
prefix = module_prefix
assert format(fd) == f"{prefix}{classname}({repr(fd_dict)})"

def test_iter(fd):
items = []
Expand Down
2 changes: 2 additions & 0 deletions test/subclass_only.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
subclass = True

def test_empty_sub(fd_empty):
assert fd_empty == frozendict_class({}) == frozendict_class([]) == frozendict_class({}, **{})

Expand Down
10 changes: 5 additions & 5 deletions test/test_coold_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def __new__(cls, *args, **kwargs):
curr_path = Path(__file__)
curr_dir = curr_path.parent

with open(curr_dir / "subclass_only.py") as f:
subclass_only_code = f.read()

exec(subclass_only_code)

common_path = curr_dir / "common.py"

with open(common_path) as f:
common_code = f.read()

exec(common_code)

with open(curr_dir / "subclass_only.py") as f:
subclass_only_code = f.read()

exec(subclass_only_code)
10 changes: 5 additions & 5 deletions test/test_frozendict_c_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def __new__(cls, *args, **kwargs):
curr_path = Path(__file__)
curr_dir = curr_path.parent

with open(curr_dir / "subclass_only.py") as f:
subclass_only_code = f.read()

exec(subclass_only_code)

common_path = curr_dir / "common.py"

with open(common_path) as f:
common_code = f.read()

exec(common_code)

with open(curr_dir / "subclass_only.py") as f:
subclass_only_code = f.read()

exec(subclass_only_code)
10 changes: 5 additions & 5 deletions test/test_frozendict_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def __new__(cls, *args, **kwargs):

common_path = curr_dir / "common.py"

with open(common_path) as f:
common_code = f.read()

exec(common_code)

with open(curr_dir / "subclass_only.py") as f:
subclass_only_code = f.read()

exec(subclass_only_code)

with open(common_path) as f:
common_code = f.read()

exec(common_code)

0 comments on commit 6da8e8b

Please sign in to comment.