Skip to content

fix(core): break circular import between organize_options and config - #1804

Draft
mujisashi wants to merge 1 commit into
curdriceaurora:mainfrom
mujisashi:fix/organize-options-circular-import
Draft

mujisashi wants to merge 1 commit into
curdriceaurora:mainfrom
mujisashi:fix/organize-options-circular-import

Conversation

@mujisashi

Copy link
Copy Markdown

Description

fo crashes at startup on current main — every command, including fo --help:

File ".../core/organize_options.py", line 11, in <module>
    from file_organizer.services.auto_tagging.styles import (
File ".../services/__init__.py", line 17, in <module>
    from file_organizer.services.smart_suggestions import (
...
File ".../config/methodology.py", line 25, in <module>
    from file_organizer.core.organize_options import OrganizationMethodology
ImportError: cannot import name 'OrganizationMethodology' from partially initialized module
'file_organizer.core.organize_options' (most likely due to a circular import)

No existing issue; found while running the CLI locally.

Root cause

#1783 (path-aware prompts & auto-tagging) added a module-scope import of services.auto_tagging.styles to core/organize_options.py. Importing anything under file_organizer.services first executes services/__init__.py, which eagerly imports smart_suggestions → models → models.text_model → config → config.manager → config.methodology, and config.methodology imports OrganizationMethodology back from organize_options — which at that point hasn't reached the class definition yet.

The CLI's import order (cli.main → cli.organize → organization_service → file_ops → core.types → core.plan → organize_options) enters the cycle at organize_options, so it fails. The test suite didn't catch it because conftest imports file_organizer.config first, which happens to load the modules in a cycle-safe order.

What changed

  • src/file_organizer/core/organize_options.py: move the normalize_tag_prompt / validate_tag_style import into OrganizeOptions.__post_init__, its only use site, with a comment explaining why it's deferred. styles.py itself depends only on tag_normalize, so the deferred import is cheap. This also removes a core → services module-load dependency from the domain contract.
  • tests/core/test_import_cycles.py (new): cold-imports core.organize_options, core.plan, and cli.main each in a fresh subprocess (same pattern as test_core_lazy_imports.py), so the cycle can't be masked by import order inside the test process.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • New regression test confirmed RED against the pre-fix organize_options.py (all 3 parametrized cold imports fail with the ImportError above), GREEN after the fix.
  • fo --help runs again; cold imports of organize_options, config.methodology, config, services, cli.main, core.plan all succeed.
  • tests/core, tests/cli, tests/config, tests/integration/test_setup_wizard_core.py, tests/integration/test_cli_setup.py: 1944 passed, 2 skipped.
  • ruff check / ruff format --check on changed files: clean.
  • mypy could not be run locally: it aborts on a syntax error inside the installed PyQt6 stubs (QtGui.pyi) before reaching project files — environment issue, unrelated to this change.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (n/a)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules (n/a)

🤖 Generated with Claude Code

organize_options imported services.auto_tagging.styles at module load,
which runs services/__init__ and eventually reaches config.methodology,
which imports OrganizationMethodology back from the half-initialized
organize_options. This crashed `fo` at startup. Defer the styles import
to OrganizeOptions.__post_init__, its only use site.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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