Skip to content

Commit 1e8ea8d

Browse files
refactor: remove IOIdentity (#479)
1 parent 47f3f7b commit 1e8ea8d

File tree

5 files changed

+27
-40
lines changed

5 files changed

+27
-40
lines changed

packages/ordeq/src/ordeq/_graph.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from itertools import chain
77
from typing import Generic, TypeVar, cast
88

9-
from ordeq._io import AnyIO, IOIdentity, _is_io
9+
from ordeq._io import AnyIO, _is_io
1010
from ordeq._nodes import Node, _is_view
1111
from ordeq._resource import Resource
1212

@@ -181,29 +181,25 @@ def __repr__(self) -> str:
181181

182182
# TODO: remove entire class
183183
@dataclass(frozen=True)
184-
class NodeIOGraph(Graph[IOIdentity | Node]):
185-
edges: dict[IOIdentity | Node, list[IOIdentity | Node]]
186-
ios: dict[IOIdentity, AnyIO]
184+
class NodeIOGraph(Graph[AnyIO | Node]):
185+
edges: dict[AnyIO | Node, list[AnyIO | Node]]
186+
ios: dict[AnyIO, AnyIO]
187187

188188
@classmethod
189189
def from_nodes(cls, nodes: Sequence[Node]) -> Self:
190190
return cls.from_graph(NodeGraph.from_nodes(nodes))
191191

192192
@classmethod
193193
def from_graph(cls, base: NodeGraph) -> Self:
194-
edges: dict[IOIdentity | Node, list[IOIdentity | Node]] = defaultdict(
195-
list
196-
)
197-
ios: dict[IOIdentity, AnyIO] = {}
194+
edges: dict[AnyIO | Node, list[AnyIO | Node]] = defaultdict(list)
195+
ios: dict[AnyIO, AnyIO] = {}
198196
for node in base.topological_ordering:
199197
for input_ in node.inputs:
200-
input_id = id(input_)
201-
ios[input_id] = input_
202-
edges[input_id].append(node)
198+
ios[input_] = input_
199+
edges[input_].append(node)
203200
for output in node.outputs:
204-
output_id = id(output)
205-
ios[output_id] = output
206-
edges[node].append(output_id)
201+
ios[output] = output
202+
edges[node].append(output)
207203
return cls(edges=edges, ios=ios)
208204

209205
@cached_property
@@ -214,7 +210,7 @@ def __repr__(self) -> str:
214210
# Hacky way to generate a deterministic repr of this class.
215211
# This should move to a separate named graph class.
216212
lines: list[str] = []
217-
names: dict[IOIdentity | Node, str] = {
213+
names: dict[AnyIO | Node, str] = {
218214
**{node: f"{node.type_name}:{node.ref}" for node in self.nodes},
219215
**{
220216
io: f"io-{i}"

packages/ordeq/src/ordeq/_io.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,6 @@ def __repr__(self):
747747
# Type aliases
748748
AnyIO: TypeAlias = Input[T] | Output[T]
749749

750-
# Type alias for IO identity retrieved using id(). This is used to uniquely
751-
# identify IO instances. We cannot rely on the __eq__ and __hash__ of IO
752-
# objects, as they may be overridden by the user.
753-
IOIdentity: TypeAlias = Annotated[int, "Identity of an IO object"]
754-
755750

756751
def _is_input(obj: object) -> TypeGuard[Input]:
757752
return isinstance(obj, Input)

packages/ordeq/src/ordeq/_process_ios.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
def _assign_io_fqns(*nodes: Node, io_fqns: IOFQNs) -> None:
88
for node in nodes:
99
for io in chain(node.inputs, node.outputs):
10-
io_id = id(io)
11-
if io_id in io_fqns:
12-
if len(io_fqns[io_id]) == 1:
13-
io._set_fqn(io_fqns[io_id][0]) # type: ignore[attr-defined]
14-
elif len(io_fqns[io_id]) > 1:
15-
io._set_name(io_fqns[io_id][0].name) # type: ignore[attr-defined]
10+
if io in io_fqns:
11+
if len(io_fqns[io]) == 1: # type: ignore[index]
12+
io._set_fqn(io_fqns[io][0]) # type: ignore[index]
13+
elif len(io_fqns[io]) > 1: # type: ignore[index]
14+
io._set_name(io_fqns[io][0].name) # type: ignore[index]
1615

1716

1817
def _process_ios(

packages/ordeq/src/ordeq/_resolve.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from ordeq._fqn import FQN, ModuleName, ObjectRef, is_object_ref
1313
from ordeq._hook import NodeHook, RunHook, RunnerHook
14-
from ordeq._io import AnyIO, IOIdentity, _is_io, _is_io_sequence
14+
from ordeq._io import AnyIO, _is_io, _is_io_sequence
1515
from ordeq._nodes import Node, _is_node
1616

1717
RunnableRef: TypeAlias = ObjectRef | ModuleName
@@ -164,19 +164,17 @@ def _resolve_refs_to_modules(
164164

165165

166166
def _resolve_module_to_ios(module: ModuleType) -> dict[str, AnyIO]:
167-
ios: dict[IOIdentity, tuple[AnyIO, str]] = {}
167+
ios: dict[AnyIO, str] = {}
168168
for name, obj in vars(module).items():
169169
if _is_io(obj):
170-
io_id = id(obj)
171170
# TODO: Should also resolve to IO sequence
172-
if io_id in ios:
173-
alias = ios[io_id][1]
171+
if obj in ios:
174172
raise ValueError(
175173
f"Module '{module.__name__}' contains duplicate keys "
176-
f"for the same IO ('{name}' and '{alias}')"
174+
f"for the same IO ('{name}' and '{ios[obj]}')"
177175
)
178-
ios[io_id] = (obj, name)
179-
return {name: io for io, name in ios.values()}
176+
ios[obj] = name
177+
return {name: io for io, name in ios.items()}
180178

181179

182180
def _resolve_package_to_ios(package: ModuleType) -> Catalog:

packages/ordeq/src/ordeq/_scan.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from typing import TypeAlias
44

55
from ordeq._fqn import FQN
6-
from ordeq._io import IOIdentity, _is_io
6+
from ordeq._io import AnyIO, _is_io
77
from ordeq._nodes import Node, _is_node
88

99
NodeFQNs: TypeAlias = dict[Node, list[FQN]]
10-
IOFQNs: TypeAlias = dict[IOIdentity, list[FQN]]
10+
IOFQNs: TypeAlias = dict[AnyIO, list[FQN]]
1111

1212

1313
def _scan_fqns(*modules: ModuleType) -> tuple[NodeFQNs, IOFQNs]:
@@ -16,16 +16,15 @@ def _scan_fqns(*modules: ModuleType) -> tuple[NodeFQNs, IOFQNs]:
1616
for module in modules:
1717
for name, obj in vars(module).items():
1818
if _is_io(obj):
19-
io_id = id(obj)
20-
if io_id in io_fqns:
21-
existing_fqn = io_fqns[io_id][0]
19+
if obj in io_fqns:
20+
existing_fqn = io_fqns[obj][0]
2221
if name != existing_fqn.name:
2322
raise ValueError(
2423
f"Module '{module.__name__}' aliases IO "
2524
f"'{existing_fqn.ref}' to '{name}'. "
2625
f"IOs cannot be aliased."
2726
)
28-
io_fqns[io_id].append(FQN(module.__name__, name))
27+
io_fqns[obj].append(FQN(module.__name__, name))
2928
elif _is_node(obj):
3029
if obj in node_fqns:
3130
existing = node_fqns[obj][0]

0 commit comments

Comments
 (0)