Skip to content

Refactor conditional backend import in python to remove circular import dependency at package initialization time#406

Merged
K20shores merged 8 commits intomainfrom
370-python-files
Jul 8, 2025
Merged

Refactor conditional backend import in python to remove circular import dependency at package initialization time#406
K20shores merged 8 commits intomainfrom
370-python-files

Conversation

@K20shores
Copy link
Copy Markdown
Collaborator

The __init__.py file would get a circular import if the imports in __init__.py were re-ordered. This is because of the conditional import logic we need to do of the pybind11 musica targets for cpu vs gpu. This, I think, should fix that circular import and allow our python format actions to be turned on again

Closes #370

@K20shores K20shores linked an issue Jul 7, 2025 that may be closed by this pull request
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jul 7, 2025

@K20shores K20shores changed the title moving backend import logic to its own file Refactor conditional backend import in python to remove circular import dependency at package initialization time Jul 7, 2025
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jul 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.15%. Comparing base (1948485) to head (7116b57).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #406   +/-   ##
=======================================
  Coverage   87.15%   87.15%           
=======================================
  Files          43       43           
  Lines        3870     3870           
=======================================
  Hits         3373     3373           
  Misses        497      497           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@K20shores K20shores marked this pull request as ready for review July 7, 2025 21:43
@K20shores K20shores requested review from boulderdaze and Copilot July 7, 2025 21:43

This comment was marked as outdated.

@K20shores K20shores requested a review from Copilot July 7, 2025 21:59

This comment was marked as outdated.

@K20shores K20shores requested a review from Copilot July 7, 2025 22:08

This comment was marked as outdated.

Copy link
Copy Markdown
Collaborator

@boulderdaze boulderdaze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@K20shores K20shores requested a review from Copilot July 7, 2025 22:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the conditional import logic to eliminate a circular dependency at package initialization by centralizing backend selection and dynamically loading core symbols.

  • Introduce musica/backend.py for runtime CPU/GPU backend selection.
  • Update types.py, __init__.py, and cuda.py to use backend.get_backend() instead of direct imports.
  • Refactor all musica/mechanism_configuration/*.py modules and tests to load C++ symbols from the chosen backend.

Reviewed Changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated no comments.

Show a summary per file
File Description
musica/backend.py New module that detects GPU dependencies and selects the backend
musica/types.py Replace direct C++ imports with dynamic backend.get_backend()
musica/init.py Simplify package exports and remove inline backend logic
musica/cuda.py Updated to use new backend module
musica/test/test_util_full_mechanism.py Tests updated to reference mc.* instead of direct imports
musica/mechanism_configuration/*.py All config classes now import C++ symbols via the dynamic backend
Comments suppressed due to low confidence (3)

musica/init.py:9

  • The module-level all omits 'version', so version won't be exposed on wildcard imports. Consider adding 'version' to all for consistency with previous exports.
__all__ = [

musica/backend.py:7

  • Add unit tests for _safe_find_spec, _gpu_deps_installed, and get_backend() to verify that CPU vs GPU backends are selected correctly under different environment conditions.
def _safe_find_spec(name):

musica/types.py:26

  • [nitpick] The alias mc for the mechanism configuration backend may be ambiguous to readers; consider a more descriptive name like mechanism_config to improve readability.
mc = _backend._mechanism_configuration

@K20shores K20shores merged commit 81aa63b into main Jul 8, 2025
69 checks passed
@K20shores K20shores deleted the 370-python-files branch July 8, 2025 14:08
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.

Python files

4 participants