Repository navigation
Keep an import whose sibling if/else branch assigns a fallback to the same name - #364
Merged
Merged
Conversation
… same name Since assignments no longer count as uses, the common compat pattern `if ...: import tomllib` / `else: tomllib = None` removed the import, so `from compat import tomllib` failed. An import in one branch and an assignment of the same name in the other (following elif chains) is now a dispatch, like the same import in both branches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019P9bvuGwAyVCNUdsAYPB1V
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.
Regression from #350 (stores no longer count as uses), found in the second review of
claude/integration.Problem
mainkeptimport tomllibonly becausetomllib = Nonecounted as a use. After #350 it no longer does, so the import becamepass, andfrom compat import tomllibraisedImportErroron 3.11+. The dispatch rule only protected names imported in both branches.Fix
visit_Ifnow also treats as a dispatch a name imported in one branch and assigned (Assign/AnnAssignwith a value) directly in the sibling branch. Assignments are followed throughelifchains. This works in both directions, e.g.Missing = None/else: from typing_extensions import Missing.Tests
The new
if_dispatch/fallback_assignmentcase covers if/else, if/elif/else, and the reversed order. It also includes a negative case where the else branch assigns a different name, and that import is still removed. The case fails before this change. Full suite passes on 3.9 / 3.12 / 3.14, and pre-commit passes.🤖 Generated with Claude Code
https://claude.ai/code/session_019P9bvuGwAyVCNUdsAYPB1V
Generated by Claude Code