Skip to content

refactor: move all imports to module top-level#10

Merged
Abishek9342 merged 1 commit into
devfrom
pr/fix-lazy-imports
Mar 26, 2026
Merged

refactor: move all imports to module top-level#10
Abishek9342 merged 1 commit into
devfrom
pr/fix-lazy-imports

Conversation

@sairaamv

@sairaamv sairaamv commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • src/__init__.pyimportlib.metadata moved to top (Python ≥3.9 always has it)
  • src/regex_scanner.pydatetime and time (stdlib) moved to top
  • src/ai_scanner.pyRegexScanner moved to top (confirmed no circular dep)
  • src/utils.pyclick and requests (required deps) moved to top; all three from inquirer import ... calls inside functions collapsed into a single top-level try/except ImportError with a _HAS_INQUIRER flag
  • src/providers/bedrock.pyboto3 (optional dep) moved to top-level try/except with a _HAS_BOTO3 guard; raises a clear ImportError message when missing
  • tests/test_regex_scanner.pytempfile and os moved to top

Why

Imports inside functions are a Python anti-pattern:

  • Hides dependencies (harder to audit what a module needs)
  • Costs a dict lookup every call instead of once at import time
  • Breaks static analysis tools (linters, type checkers, IDEs)

Optional deps (boto3, inquirer) are handled via top-level try/except ImportError — the standard Python idiom.

Test plan

  • uv run --with pytest pytest tests/ -v150 passed
  • No new imports added — only relocated

🤖 Generated with Claude Code


Open with Devin

All package and sub-module imports now appear at the top of each file
rather than inside functions, following Python best practices.

- src/__init__.py: move importlib.metadata import to top
- src/regex_scanner.py: move datetime and time to top
- src/ai_scanner.py: move RegexScanner import to top (no circular dep)
- src/utils.py: move click, requests to top; consolidate inquirer into
  a single top-level try/except with _HAS_INQUIRER flag
- src/providers/bedrock.py: move boto3 to top-level try/except with
  _HAS_BOTO3 guard for clear ImportError on missing optional dep
- tests/test_regex_scanner.py: move tempfile, os to top

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@Abishek9342 Abishek9342 merged commit b10c286 into dev Mar 26, 2026
4 checks passed
@Abishek9342 Abishek9342 deleted the pr/fix-lazy-imports branch March 26, 2026 11:11
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.

2 participants