Skip to content

Standardize on absolute imports (PEP 8) and ensure repo package precedence - #74

Merged
xingyaoww merged 7 commits into
mainfrom
openhands/absolute-imports-pep8
Sep 3, 2025
Merged

xingyaoww merged 7 commits into
mainfrom
openhands/absolute-imports-pep8

Conversation

@xingyaoww

Copy link
Copy Markdown
Contributor

Summary

  • Migrate all relative imports to absolute imports across openhands.core and openhands.tools to follow PEP 8 guidance on import clarity and robustness.
  • Ensure this repository’s openhands namespace takes precedence over any preinstalled OpenHands code injected via PYTHONPATH (e.g., /openhands/code) during tests.

What changed

  • Converted relative imports to absolute in both packages:
    • openhands/core/**
    • openhands/tools/**
  • Namespace/package safety:
    • Added sitecustomize.py that ensures this repo’s path is preferred in sys.path when an external /openhands/code is present.
    • Updated openhands/init.py to prioritize the local path in the namespace package.
  • Fixed package version lookup in openhands/tools/init.py to use "openhands-tools".
  • Ran pre-commit (ruff format/lint, pyright) on modified files.

Why

  • Absolute imports are explicit and refactor-safe. PEP 8 recommends absolute imports, and the repo already uses Ruff+Pyright for quality gates. This change standardizes the codebase and avoids ambiguity between sibling modules.
  • The test environment included another openhands tree at /openhands/code, which caused import resolution to pick the wrong modules and break tests. The small path guard ensures our workspace modules are used.

Testing

  • Local: uv run pytest shows all unit tests green for core and tools; the integration test passes when run individually. Running the entire suite in one shot can hit a memory limit on this constrained runner during pytest session shutdown (MemoryError when writing cache). In CI the jobs are split (core/tools/integration) and run with fresh environments, so this should not occur. Also verified imports via direct Python sessions.
  • Pre-commit: Ran uv run pre-commit run --files <modified>; Ruff and Pyright pass.

PEP8 enforcement (pycodestyle)

  • The repo already enforces PEP 8 rules via Ruff (E-series) which covers pycodestyle errors and is significantly faster and more comprehensive. Adding pycodestyle would be redundant and slow down hooks. Recommendation: continue using Ruff; no additional pycodestyle hook added.

Backward compatibility

  • No public APIs changed; only import paths inside the codebase are now absolute. The openhands namespace still exports the same symbols. The sitecustomize and namespace path tweaks only affect local dev/test environments and do not alter runtime behavior when installed in isolation.

Checklist

  • Absolute imports throughout
  • Pre-commit (ruff, pyright) passes on touched files
  • Tests pass locally; CI will run split jobs as configured

Closes #66

@xingyaoww can click here to continue refining the PR

…nds.tools; ensure repo package precedence to avoid namespace collisions; fix lint/type issues.\n\n- Enforce PEP 8 import style (absolute imports) per PEP 8 guidance\n- Add sitecustomize.py to prefer this repo's openhands over preinstalled /openhands/code during tests\n- Adjust openhands/__init__.py to prioritize local path in namespace package\n- Update tools package __version__ lookup\n- Run pre-commit (ruff, pyright) on touched files\n\nCloses #66\n\nCo-authored-by: openhands <openhands@all-hands.dev>
…e ignores to avoid duplication with Ruff (E203,E501,W503,W291).\n\nCo-authored-by: openhands <openhands@all-hands.dev>
@xingyaoww
xingyaoww enabled auto-merge (squash) September 3, 2025 17:15
@xingyaoww
xingyaoww merged commit 662f254 into main Sep 3, 2025
@xingyaoww
xingyaoww deleted the openhands/absolute-imports-pep8 branch September 3, 2025 17:16
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…dence (OpenHands#74)

Co-authored-by: openhands <openhands@all-hands.dev>
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.

Absolute imports vs. relative imports

2 participants