Repository navigation
Conversation
WeakRefList.__getitem__ called the result of list.__getitem__ unconditionally, which is a plain list of weakrefs when the index is a slice, raising TypeError: 'list' object is not callable. Handle slice indices by dereferencing each referenced object, so slicing a TraceNode's children returns TraceNode objects as its list type annotation implies.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Slicing a
WeakRefList(e.g.TraceNode.children[0:2]) raisedTypeError: 'list' object is not callable. Slicing now returns the referenced objects, consistent with integer indexing.Why
WeakRefList.__getitem__(guidance/trace/_trace.py:217-219 onmain) unconditionally treated the result oflist.__getitem__as a single weakref and called it. When the index is a slice,list.__getitem__returns a plain list of weakref objects, and calling a list raisesTypeError: 'list' object is not callable.TraceNode.childrenis aWeakRefList(guidance/trace/_trace.py:247 onmain), so any slice access on it crashed.Changes
guidance/trace/_trace.py:WeakRefList.__getitem__now checks whether the index is a slice. For a slice it dereferences every weakref in the sliced result, raisingReferenceErrorfor dead entries (same behavior as integer indexing) and returning the list of live objects. Integer indexing behavior is unchanged.Testing
Added regression test
test_weak_ref_list_sliceand slice assertions intest_trace_node(tests/unit/test_trace.py). On the unpatched tree both fail withTypeError: 'list' object is not callable.python -m pytest tests/unit/test_trace.py— 14 passedpython -m pytest tests/unit/— 1638 passed, 1 skipped, 220 xfailedruff format --check guidance/trace/_trace.py tests/unit/test_trace.py— 2 files already formattedruff check --select I,RUF022 guidance/trace/_trace.py tests/unit/test_trace.py— All checks passed