chore: eliminate sys.path.insert hacks — migrate to package-relative imports#271
chore: eliminate sys.path.insert hacks — migrate to package-relative imports#271sheepdestroyer wants to merge 5 commits into
Conversation
Co-authored-by: sheepdestroyer <1377479+sheepdestroyer@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Revert 'from .circuit_breaker' to 'from router.circuit_breaker' in main.py: relative imports break when main.py is imported directly as 'main' (tests use 'from main import ...' with PYTHONPATH=.:router) - Add try/except ImportError fallback to 'from scripts.chat_helpers' across all 7 scripts (4 in scripts/, 3 in verification/) so they remain runnable via both package import and direct execution
There was a problem hiding this comment.
Sorry @sheepdestroyer, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (22)
📝 WalkthroughWalkthroughThe change standardizes package-qualified imports across router modules, scripts, and tests, adds conditional import fallbacks for standalone scripts, and updates CI commands to use the repository root and router configuration path. ChangesPackage import migration
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Code Review
This pull request refactors import paths and removes manual sys.path modifications across several scripts and verification helpers. While this cleans up the import structure, the reviewer identified multiple instances where removing sys.path or using relative imports as fallbacks will break direct script execution (resulting in ModuleNotFoundError or ImportError due to a lack of parent package context). The feedback recommends implementing robust fallback blocks that append the necessary directories to sys.path only when the primary import fails.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | ||
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "router")) | ||
|
|
||
| from router.main import estimate_prompt_tokens, METADATA_OVERHEAD |
There was a problem hiding this comment.
Removing the sys.path modification completely breaks direct execution of this script (e.g., python scripts/benchmark_tokens.py), raising a ModuleNotFoundError: No module named 'router'. To preserve direct execution support without environment variables, add a fallback block that appends the parent directory to sys.path if the import fails.
| from router.main import estimate_prompt_tokens, METADATA_OVERHEAD | |
| try: | |
| from router.main import estimate_prompt_tokens, METADATA_OVERHEAD | |
| except ImportError: | |
| sys.path.append(str(Path(__file__).resolve().parent.parent)) | |
| from router.main import estimate_prompt_tokens, METADATA_OVERHEAD |
| try: | ||
| from scripts.chat_helpers import parse_chat_response | ||
| except ImportError: | ||
| from ..chat_helpers import parse_chat_response |
There was a problem hiding this comment.
When this script is run directly or imported by another script run directly (e.g., python scripts/verification/verify_ollama_routing.py), the package context is lost (__package__ is None). As a result, the relative import fallback from ..chat_helpers import parse_chat_response will fail with ImportError: attempted relative import with no known parent package. To prevent this crash, use a fallback that appends the repository root to sys.path instead of a relative import.
| try: | |
| from scripts.chat_helpers import parse_chat_response | |
| except ImportError: | |
| from ..chat_helpers import parse_chat_response | |
| try: | |
| from scripts.chat_helpers import parse_chat_response | |
| except ImportError: | |
| import sys | |
| from pathlib import Path | |
| sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) | |
| from scripts.chat_helpers import parse_chat_response |
| from pathlib import Path | ||
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent)) | ||
|
|
||
| from router.circuit_breaker import get_breaker |
There was a problem hiding this comment.
Removing the sys.path modification completely breaks direct execution of this script (e.g., python scripts/verification/verify_breaker.py), raising a ModuleNotFoundError: No module named 'router'. To preserve direct execution support, add a fallback block that appends the repository root to sys.path if the import fails.
| from router.circuit_breaker import get_breaker | |
| try: | |
| from router.circuit_breaker import get_breaker | |
| except ImportError: | |
| import sys | |
| from pathlib import Path | |
| sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) | |
| from router.circuit_breaker import get_breaker |
| try: | ||
| from scripts.chat_helpers import parse_chat_response | ||
| except ImportError: | ||
| from ..chat_helpers import parse_chat_response |
There was a problem hiding this comment.
When this script is run directly (e.g., python scripts/verification/verify_canonical_endpoints.py), the package context is lost, causing the relative import fallback from ..chat_helpers import parse_chat_response to fail with ImportError: attempted relative import with no known parent package. To prevent this crash, use a fallback that appends WORKDIR to sys.path instead of a relative import.
| try: | |
| from scripts.chat_helpers import parse_chat_response | |
| except ImportError: | |
| from ..chat_helpers import parse_chat_response | |
| try: | |
| from scripts.chat_helpers import parse_chat_response | |
| except ImportError: | |
| import sys | |
| sys.path.append(str(WORKDIR)) | |
| from scripts.chat_helpers import parse_chat_response |
| try: | ||
| from scripts.chat_helpers import parse_chat_response | ||
| except ImportError: | ||
| from ..chat_helpers import parse_chat_response |
There was a problem hiding this comment.
When this script is run directly (e.g., python scripts/verification/verify_ollama_routing.py), the package context is lost, causing the relative import fallback from ..chat_helpers import parse_chat_response to fail with ImportError: attempted relative import with no known parent package. To prevent this crash, use a fallback that appends WORKDIR to sys.path instead of a relative import.
| try: | |
| from scripts.chat_helpers import parse_chat_response | |
| except ImportError: | |
| from ..chat_helpers import parse_chat_response | |
| try: | |
| from scripts.chat_helpers import parse_chat_response | |
| except ImportError: | |
| import sys | |
| sys.path.append(str(WORKDIR)) | |
| from scripts.chat_helpers import parse_chat_response |
…e imports This commit: - Removes sys.path.insert hacks from scripts/ - Adds router/__init__.py to make router a package - Updates router/main.py and router/agy_proxy.py to use package-aware imports - Updates tests and mocks to use absolute package paths - Updates CI workflow to use standard PYTHONPATH=. - Ensures both CLI and package execution modes work correctly Co-authored-by: sheepdestroyer <1377479+sheepdestroyer@users.noreply.github.com>
Gemini Code Assist correctly identified that the PR's removal of sys.path hacks broke direct execution of all 9 scripts. When Python runs a script, it REPLACES sys.path[0] (the CWD) with the script's directory — so cross- package imports (router.*, scripts.chat_helpers) fail with ModuleNotFoundError. Two failure patterns, two fixes: 1. Scripts in scripts/ importing scripts.chat_helpers: try/except with bare 'from chat_helpers' fallback (found in sys.path[0]) 2. Scripts in scripts/verification/ importing scripts.chat_helpers or router.*: try/except with sys.path.insert to repo root (parents[2]) Verified: all 9 scripts now run successfully via both 'python scripts/X.py' (direct) and package imports (from scripts.X import ...)
|
Closing in favor of a fresh PR with all review fixes applied. See #272. |
What
This PR removes vestigial
sys.path.inserthacks from scripts inscripts/, migrating to standard package-relative imports with ImportError fallbacks. It also adds a missing__init__.pyto therouter/directory and normalizes structured content parsing inchat_helpers.py.Fixes #266
Changes
Import cleanup (9 scripts)
sys.path.inserthacks from all scripts inscripts/andverification/from scripts.chat_helpers import parse_chat_response+ ImportError fallbacktry/except ImportErrorpattern to 7 scripts importingchat_helpers— ensures they remain runnable via bothpython scripts/foo.py(direct) and package importsRouter package
router/__init__.pyto make router a proper Python packagerouter/main.pyto usefrom router.circuit_breaker import get_breaker(package-aware absolute import)from .circuit_breakerwas tested but reverted — it fails when test modules importmain.pydirectly viafrom main import ...withPYTHONPATH=.:routerchat_helpers.py
_normalize_chat_content()helper that handles structured content payloads from alt providers (lists of dicts, nested content, etc.)parse_chat_response()to use the new normalizer for bothcontentandreasoning_contentupgrade-prod.sh
.envvalidationMisc
asyncio,time) from test filesPreviously PR #270 — closed and reopened with review fixes applied.
Summary by CodeRabbit
Bug Fixes
Tests