Skip to content

Commit

Permalink
fix: prevent inifinite recursion in CommandTree.__repr__
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Nov 13, 2022
1 parent 75a56e9 commit a5a776c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions mecha/config.py
Expand Up @@ -162,5 +162,21 @@ def resolve(

return self

def __repr__(self) -> str:
args = ", ".join(
f"{name}="
+ (
"{" + ", ".join(f"{k!r}: ..." for k in value) + "}"
if name == "children"
else repr(value)
)
for name in self.__class__.__fields__
if (value := getattr(self, name))
)
return f"{self.__class__.__name__}({args})"

def __str__(self) -> str:
return self.__repr__()


CommandTree.update_forward_refs()
12 changes: 12 additions & 0 deletions tests/test_spec.py
Expand Up @@ -26,3 +26,15 @@ def test_parsers(snapshot: SnapshotFixture, mc: Mecha):
undefined_parsers = all_used_parser - all_defined_parsers

assert not undefined_parsers


def test_repr(snapshot: SnapshotFixture, mc: Mecha):
assert (
str(mc.spec.tree.get("execute", "if", "score"))
== "CommandTree(type='literal', children={'target': ...})"
)

assert (
str(mc.spec.tree.get("execute", "if", "score", "target"))
== "CommandTree(type='argument', parser='minecraft:score_holder', properties={'amount': 'single'}, children={'targetObjective': ...})"
)

0 comments on commit a5a776c

Please sign in to comment.