Skip to content

Refactor __init__.py to avoid late imports (E402) #59

@aquarion

Description

@aquarion

The imports/lifestream/__init__.py file has late imports that trigger flake8 E402 warnings.

Current situation

The imports at the bottom of __init__.py must come after the testing mode check:

if not os.environ.get('LIFESTREAM_TESTING'):
    setup_logging()

# These trigger E402
from .db import EntryStore
from .foursquare_api import FoursquareAPI
from .utils import ...

These are currently suppressed with # noqa: F401, E402 comments.

Why it's this way

The re-exports provide backward compatibility - scripts do from lifestream import EntryStore etc.
The testing check needs to run before any submodule imports to prevent logging setup during tests.

Potential solutions

  1. Lazy imports: Use __getattr__ to defer imports until accessed
  2. Explicit imports in scripts: Update all scripts to import from submodules directly (from lifestream.db import EntryStore)
  3. Separate testing setup: Move testing detection to a separate bootstrap module

Impact

  • Low priority - current solution works
  • Would improve code structure and remove noqa comments

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions