Skip to content

Commit 75f7fe3

Browse files
committed
use common hex_id() func
1 parent 2a87a1e commit 75f7fe3

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

src/idom/core/component.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from functools import wraps
66
from typing import Any, Callable, Dict, Tuple, Union
77

8+
from .utils import hex_id
89
from .vdom import VdomDict
910

1011

@@ -74,6 +75,6 @@ def __repr__(self) -> str:
7475
else:
7576
items = ", ".join(f"{k}={v!r}" for k, v in args.items())
7677
if items:
77-
return f"{self._function.__name__}({hex(id(self))}, {items})"
78+
return f"{self._function.__name__}({hex_id(self)}, {items})"
7879
else:
79-
return f"{self._function.__name__}({hex(id(self))})"
80+
return f"{self._function.__name__}({hex_id(self)})"

src/idom/core/layout.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .component import AbstractComponent
2626
from .events import EventHandler
2727
from .hooks import LifeCycleHook
28-
from .utils import CannotAccessResource, HasAsyncResources, async_resource
28+
from .utils import CannotAccessResource, HasAsyncResources, async_resource, hex_id
2929
from .vdom import validate_vdom
3030

3131

@@ -251,7 +251,7 @@ def _render_model_event_handlers_without_old_state(
251251

252252
model_event_handlers = new_state.model["eventHandlers"] = {}
253253
for event, handler in handlers_by_event.items():
254-
target = hex(id(handler))[2:]
254+
target = hex_id(handler)
255255
new_state.targets_by_event[event] = target
256256
self._event_handlers[target] = handler
257257
model_event_handlers[event] = {
@@ -285,10 +285,10 @@ def _render_model_children(
285285
for index, child in enumerate(raw_children):
286286
if isinstance(child, dict):
287287
child_type = DICT_TYPE
288-
key = child.get("key") or hex(id(child))[2:]
288+
key = child.get("key") or hex_id(child)
289289
elif isinstance(child, AbstractComponent):
290290
child_type = COMPONENT_TYPE
291-
key = getattr(child, "key", None) or hex(id(child))[2:]
291+
key = getattr(child, "key", None) or hex_id(child)
292292
else:
293293
child = str(child)
294294
child_type = STRING_TYPE
@@ -336,13 +336,13 @@ def _render_model_children_without_old_state(
336336
new_children = new_state.model["children"] = []
337337
for index, child in enumerate(raw_children):
338338
if isinstance(child, dict):
339-
key = child.get("key") or hex(id(child))
339+
key = child.get("key") or hex_id(child)
340340
child_state = _ModelState(new_state, index, key, None)
341341
self._render_model(None, child_state, child)
342342
new_children.append(child_state.model)
343343
new_state.children_by_key[key] = child_state
344344
elif isinstance(child, AbstractComponent):
345-
key = getattr(child, "key", "") or hex(id(child))
345+
key = getattr(child, "key", "") or hex_id(child)
346346
life_cycle_hook = LifeCycleHook(child, self)
347347
child_state = _ModelState(new_state, index, key, life_cycle_hook)
348348
self._render_component(None, child_state, child)

src/idom/core/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
from async_generator import asynccontextmanager
2323

2424

25+
def hex_id(obj: Any) -> str:
26+
return format(id(obj), "x")
27+
28+
2529
_Rsrc = TypeVar("_Rsrc")
2630
_Self = TypeVar("_Self", bound="HasAsyncResources")
2731

tests/test_core/test_component.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import idom
2+
from idom.core.utils import hex_id
23

34

45
def test_component_repr():
@@ -8,7 +9,7 @@ def MyComponent(a, *b, **c):
89

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

11-
expected = f"MyComponent({hex(id(mc1))}, a=1, b=(2, 3), c={{'x': 4, 'y': 5}})"
12+
expected = f"MyComponent({hex_id(mc1)}, a=1, b=(2, 3), c={{'x': 4, 'y': 5}})"
1213
assert repr(mc1) == expected
1314

1415
# not enough args supplied to function

tests/test_core/test_layout.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import idom
88
from idom.core.layout import LayoutEvent, LayoutUpdate
9+
from idom.core.utils import hex_id
910
from tests.general_utils import HookCatcher, assert_same_items
1011

1112

@@ -21,7 +22,7 @@ def MyComponent():
2122

2223
my_component = MyComponent()
2324
layout = idom.Layout(my_component)
24-
assert str(layout) == f"Layout(MyComponent({hex(id(my_component))}))"
25+
assert str(layout) == f"Layout(MyComponent({hex_id(my_component)}))"
2526

2627

2728
def test_layout_expects_abstract_component():

0 commit comments

Comments
 (0)