Skip to content

Keep an import whose sibling if/else branch assigns a fallback to the same name - #364

Merged
hakancelikdev merged 1 commit into
claude/integrationfrom
fix/if-fallback-assignment
Sep 25, 2026
Merged

hakancelikdev merged 1 commit into
claude/integrationfrom
fix/if-fallback-assignment

Conversation

@hakancelikdev

Copy link
Copy Markdown
Owner

Regression from #350 (stores no longer count as uses), found in the second review of claude/integration.

Problem

import sys
if sys.version_info >= (3, 11):
    import tomllib
else:
    tomllib = None

main kept import tomllib only because tomllib = None counted as a use. After #350 it no longer does, so the import became pass, and from compat import tomllib raised ImportError on 3.11+. The dispatch rule only protected names imported in both branches.

Fix

visit_If now also treats as a dispatch a name imported in one branch and assigned (Assign / AnnAssign with a value) directly in the sibling branch. Assignments are followed through elif chains. This works in both directions, e.g. Missing = None / else: from typing_extensions import Missing.

Tests

The new if_dispatch/fallback_assignment case 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

… 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
@hakancelikdev
hakancelikdev merged commit c46268f into claude/integration Sep 25, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants