Skip to content

Order an import-time call after the names it needs at runtime#50

Merged
bboe merged 1 commit into
mainfrom
runtime-call-dependency
Jun 15, 2026
Merged

Order an import-time call after the names it needs at runtime#50
bboe merged 1 commit into
mainfrom
runtime-call-dependency

Conversation

@bboe

@bboe bboe commented Jun 15, 2026

Copy link
Copy Markdown
Member

Problem

A module-level assignment that calls a local definition is 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 function is left below the assignment, which is hoisted above it and raises NameError at 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_dependency with importable fixtures (guarded by test_fixture_files_execute). Full suite, ruff check, and the self-sort check are clean.

🤖 Generated with Claude Code

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>
@bboe bboe force-pushed the runtime-call-dependency branch from 8f21c9e to ad1e04d Compare June 15, 2026 21:16
@bboe bboe merged commit e192be1 into main Jun 15, 2026
9 checks passed
@bboe bboe deleted the runtime-call-dependency branch June 15, 2026 21:18
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.

1 participant