Skip to content

Ban all relative imports in Python linting configuration #99

@junhaoliao

Description

@junhaoliao

Request

Context

As discussed in y-scope/clp#1640, we should ban all relative imports in favor of absolute imports for better code explicitness and maintainability.

Rationale

Per PEP8 Import section:

Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages)

While explicit relative imports are acceptable for complex package layouts, our projects don't have deeply nested modules that would necessitate this compromise.

Benefits

  1. IDEs and humans can easily catch and modify import paths when files are moved
  2. Linters can catch inconsistent ways of writing the same code
  3. Better error messages and more explicit code

Related

Possible implementation

Update exports/lint-configs/python/ruff.toml to set:

[lint.flake8-tidy-imports]
# Disallow all relative imports.
ban-relative-imports = "all"

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions