Order an import-time call after the names it needs at runtime#50
Merged
Conversation
A module-level assignment that calls a local definition was sorted only by the names it references syntactically. `APP = App()` references `App`, but instantiating it runs `App.__init__`, which may read a module-level function defined later in the file. The assignment was hoisted above that function and raised `NameError` at import. Collect the module-global names referenced anywhere in a class or function body (including deferred references inside method bodies), and when a constant assignment calls such a definition, treat that definition's transitive runtime references as dependencies of the assignment. Definitions gain no edges from this, so it never forges a cycle between a class or function and its siblings. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
8f21c9e to
ad1e04d
Compare
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.
Problem
A module-level assignment that calls a local definition is sorted only by the names it references syntactically.
APP = App()referencesApp, but instantiating it runsApp.__init__, which may read a module-level function defined later in the file. The function is left below the assignment, which is hoisted above it and raisesNameErrorat import.Fix
Collect the module-global names referenced anywhere in a class or function body (including deferred references inside method bodies), resolving each through its own scope so a shadowing local is excluded. When a constant assignment calls such a definition, the definition's transitive runtime references become dependencies of the assignment, so the topological sort keeps the call after everything it needs. Definitions gain no new edges, so it never forges a cycle; a bare alias (
x = method) is unaffected.Tests
Adds
test_runtime_call_dependencywith importable fixtures (guarded bytest_fixture_files_execute). Full suite,ruff check, and the self-sort check are clean.🤖 Generated with Claude Code