Skip to content

Sample code for: Python Circular Imports: Why They Happen and How to Fix Them - #830

Open
stephengruppetta wants to merge 1 commit into
masterfrom
python-circular-imports
Open

stephengruppetta wants to merge 1 commit into
masterfrom
python-circular-imports

Conversation

@stephengruppetta

Copy link
Copy Markdown
Contributor

Sample code for Python Circular Imports: Why They Happen and How to Fix Them (CMS Post 2247).

Seven self-contained projects, one per stage of the tutorial. Each carries the same shop/ package plus a main.py, so a reader can run any stage without editing files in place.

Path Result
broken/ ImportError (on purpose)
step1_remove_dependency/ 2004.99 on 3.14, NameError on 3.13 and earlier
step2_guard_annotation/ 2004.99
workaround1_defer_import/ 1004.99
workaround2_bottom_import/ 1004.99
workaround3_attribute_error/ AttributeError (on purpose)
workaround3_import_module/ 1004.99

All seven verified on CPython 3.14.6. Passes ruff format --check and ruff check under the repo config.

Two # noqa directives, both where the tutorial is deliberately showing code a linter objects to: F821 in step1_remove_dependency/ (the annotation is unresolvable until Step 2 guards the import) and E402 in workaround2_bottom_import/ (the bottom import is the technique being demonstrated). Both are explained in the folder README.


🤖 Generated with Claude Code

…Fix Them

Seven self-contained projects, one per stage of the tutorial. Each has
the same shop/ package plus a main.py, so a reader can run any stage
without editing files in place.

Two of them fail on purpose: broken/ is the circular import the tutorial
sets out to fix, and workaround3_attribute_error/ shows the module-level
attribute access failing.

Verified on CPython 3.14.6. Passes ruff format and ruff check under the
repo config, with two noqa directives where the tutorial is deliberately
showing code a linter objects to.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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