Skip to content

Commit 616381f

Browse files
RonnyPfannschmidtCursor AIclaude
committed
Use isinstance for subclass safety and unify path helpers
Switch iterators.py and path.py from exact type checks (type(x) is dict) to isinstance, so dict/list subclasses like ruamel.yaml's CommentedMap and CommentedSeq are handled correctly. The ~5ns/call overhead is negligible now that resolution is O(N) via structural sharing. Unify path_get/_path_get_abc and path_set/_path_set_abc into shared _step_get/_step_set_descend/_step_set_final helpers that handle both concrete types and ABC fallback in one call chain, eliminating the duplicated traversal and error-handling logic. Expand resolver.py module docstring to document structural sharing trade-offs: what's safe by default, what requires care, and the two mitigation strategies (materialize=True, copy-on-write). Co-authored-by: Cursor AI <[email protected]> Co-authored-by: Anthropic Claude Opus 4.6 <[email protected]>
1 parent 3810190 commit 616381f

3 files changed

Lines changed: 223 additions & 175 deletions

File tree

‎prance/util/iterators.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
__license__ = "MIT"
66
__all__ = ()
77

8-
_CONTAINER_TYPES = (dict, list, tuple)
9-
108

119
def item_iterator(value, path=()):
1210
"""
@@ -62,6 +60,9 @@ def reference_iterator(specs, path=()):
6260
"""
6361
Iterate through the given specs, returning only references.
6462
63+
Uses ``isinstance`` so that dict/list subclasses (e.g. ruamel.yaml's
64+
``CommentedMap``/``CommentedSeq``) are handled correctly.
65+
6566
The iterator returns three values:
6667
- The key, mimicking the behaviour of other iterators, although
6768
it will always equal '$ref'
@@ -77,18 +78,17 @@ def reference_iterator(specs, path=()):
7778
stack = [(path, specs)]
7879
while stack:
7980
current_path, value = stack.pop()
80-
vtype = type(value)
81-
if vtype is dict:
81+
if isinstance(value, dict):
8282
children = []
8383
for key, item in value.items():
8484
if key == "$ref":
8585
yield "$ref", item, current_path
86-
elif type(item) in _CONTAINER_TYPES:
86+
elif isinstance(item, (dict, list, tuple)):
8787
children.append((current_path + (key,), item))
8888
stack.extend(reversed(children))
89-
elif vtype is list or vtype is tuple:
89+
elif isinstance(value, (list, tuple)):
9090
children = []
9191
for idx, item in enumerate(value):
92-
if type(item) in _CONTAINER_TYPES:
92+
if isinstance(item, (dict, list, tuple)):
9393
children.append((current_path + (idx,), item))
9494
stack.extend(reversed(children))

0 commit comments

Comments
 (0)