Skip to content

Commit

Permalink
fix Component.__repr__ bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Mar 3, 2021
1 parent 8010e0d commit 3e8b9ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
hooks:
- id: flake8
- repo: https://github.com/kynan/nbstripout
rev: master
rev: 0.3.9
hooks:
- id: nbstripout
files: ".ipynb"
Expand Down
14 changes: 9 additions & 5 deletions idom/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ def render(self) -> Any:

def __repr__(self) -> str:
sig = inspect.signature(self._function)
args = sig.bind(*self._args, **self._kwargs).arguments
items = ", ".join(f"{k}={v!r}" for k, v in args.items())
if items:
return f"{self._function.__name__}({hex(id(self))}, {items})"
try:
args = sig.bind(*self._args, **self._kwargs).arguments
except TypeError:
return f"{self._function.__name__}(...)"
else:
return f"{self._function.__name__}({hex(id(self))})"
items = ", ".join(f"{k}={v!r}" for k, v in args.items())
if items:
return f"{self._function.__name__}({hex(id(self))}, {items})"
else:
return f"{self._function.__name__}({hex(id(self))})"
9 changes: 6 additions & 3 deletions tests/test_core/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ def test_component_repr():
def MyComponent(a, *b, **c):
pass

m_e = MyComponent(1, 2, 3, x=4, y=5)
mc1 = MyComponent(1, 2, 3, x=4, y=5)

expected = f"MyComponent({hex(id(m_e))}, a=1, b=(2, 3), c={{'x': 4, 'y': 5}})"
assert repr(m_e) == expected
expected = f"MyComponent({hex(id(mc1))}, a=1, b=(2, 3), c={{'x': 4, 'y': 5}})"
assert repr(mc1) == expected

# not enough args supplied to function
assert repr(MyComponent()) == "MyComponent(...)"


async def test_simple_component():
Expand Down

0 comments on commit 3e8b9ea

Please sign in to comment.