Skip to content

Move inline imports to top of file across codebase #62

@JordanCoin

Description

@JordanCoin

Problem

Multiple files have import statements inside functions and exception handlers instead of at the top of the file.

Worst offenders:

  • cli.py — 32 inline imports
  • pipeline/extract.py — 17 inline imports
  • browser.py — 8 inline imports
  • security.py — 5 inline imports
  • pipeline/ocr.py — 4 inline imports
  • gateways/fax.py — 4 inline imports
  • crossref.py — 4 inline imports

22 files total with inline imports.

Why it matters

  • Makes dependencies hard to see at a glance
  • Many are redundant (module already imported at the top)
  • Not standard Python style (PEP 8: imports at top)
  • Makes it harder for new contributors to understand the codebase

Exception

Lazy imports for heavy optional dependencies (torch, spaCy, GLiNER) inside functions are fine — those are intentional to avoid import-time crashes when the dep isn't installed. Only move imports that are for stdlib or always-available packages.

Fix

Move stdlib inline imports (sys, json, re, os, asyncio, etc.) to the top of each file. Leave optional heavy deps as lazy imports with a comment explaining why.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions