fix: use set with backtracking for tree circular dependency detection#10811
Merged
radoering merged 2 commits intoMar 31, 2026
Merged
Conversation
Reviewer's GuideRefactors the dependency tree display to track visited packages using a backtracking set instead of a shared list, preventing false circular dependency warnings, and adds a regression test for shared sibling dependencies. Class diagram for tree display methods using a backtracking setclassDiagram
class ShowCommand {
+display_package_tree(io, package, dependency, installed_packages)
-_display_tree(io, dependency, installed_packages, packages_in_tree, previous_tree_bar, level) void
-_write_tree_line(io, line) void
}
class IO {
+output Output
}
class Output {
+supports_utf8() bool
}
class Dependency {
+name NormalizedName
}
class Package {
+name NormalizedName
}
class NormalizedName
ShowCommand --> IO : uses
ShowCommand --> Dependency : traverses
ShowCommand --> Package : root package
ShowCommand --> NormalizedName : tracks in packages_in_tree
ShowCommand : -packages_in_tree set~NormalizedName~
ShowCommand : _display_tree uses backtracking on packages_in_tree
ShowCommand : circular detection via membership in packages_in_tree
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The manual add/discard backtracking on
packages_in_treeassumes there are no early returns or exceptions in the recursive branch; consider wrapping the recursion in atry/finallyor using a helper/context manager to ensure the set is always restored correctly if the function gains additional exit paths later. - Now that
packages_in_treeis aset, it might be clearer to rename it to something likeancestorsorvisited_ancestorsto better convey that it tracks the current ancestor chain rather than an ordered tree path.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The manual add/discard backtracking on `packages_in_tree` assumes there are no early returns or exceptions in the recursive branch; consider wrapping the recursion in a `try`/`finally` or using a helper/context manager to ensure the set is always restored correctly if the function gains additional exit paths later.
- Now that `packages_in_tree` is a `set`, it might be clearer to rename it to something like `ancestors` or `visited_ancestors` to better convey that it tracks the current ancestor chain rather than an ordered tree path.
## Individual Comments
### Comment 1
<location path="src/poetry/console/commands/show.py" line_range="623" />
<code_context>
level + 1,
)
+ packages_in_tree.discard(dependency.name)
+
def _write_tree_line(self, io: IO, line: str) -> None:
</code_context>
<issue_to_address>
**issue (bug_risk):** Guard the add/discard pair with a try/finally to avoid leaking state on errors.
Since `packages_in_tree` is shared and mutated in place across recursive calls, any exception in `_display_tree` will bypass the `discard`, leaving stale entries in the set and causing later branches to be misidentified as circular. Please wrap the recursive call in a `try/finally` so the `discard` always runs:
```python
packages_in_tree.add(dependency.name)
try:
self._display_tree(
io,
dependency,
installed_packages,
packages_in_tree,
tree_bar,
level + 1,
)
finally:
packages_in_tree.discard(dependency.name)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
packages_in_tree was a shared mutable list — siblings' subtrees would pollute each other, causing false circular dependency warnings. Changed to a set (correct data structure for membership checks) with backtracking (discard after recursion) to isolate sibling branches. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
d93db23 to
4febe90
Compare
radoering
approved these changes
Mar 31, 2026
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
packages_in_tree was a shared mutable list — siblings' subtrees would pollute each other, causing false circular dependency warnings. Changed to a set (correct data structure for membership checks) with backtracking (discard after recursion) to isolate sibling branches.
Pull Request Check List
Resolves: #issue-number-here
Summary by Sourcery
Fix circular dependency detection in the
poetry show --treecommand to avoid false positives when dependencies are shared between sibling nodes.Bug Fixes:
Tests: