Skip to content

lint: Should add basic linter for Python import statements #13571

@EricCousineau-TRI

Description

@EricCousineau-TRI

Should formalize import statements
Relates #13572 (issue) in that this should be a idempotent projection to one format (ideal thing for linting)

Steps:

  • Styleguide PR
  • Lint implementation + unittests
  • Application to codebase

Some design thoughts:

  • From PEP008, should delineate builtin, third, and first-part sources
  • Should be extensible for Anzu
  • Should handle ensuring pydrake is imported before torch (for Anzu)
  • Should also handle mut and mut_* modules being imported first (or encourage it, as well)

My preferred styles and ordering (aside from the PEP8 sections):

# Basic import
import module
# Single symbol import
from module import SingleSymbol
# Module import from package
from package_1.sub_package import module
# import package_1.sub_package.module as module  # Nah. I think this is silly, trying too hard for line-lexical sorting.
# Abbreviated alias
import package_2.sub_package.module as m2
# Super line wrap
from package_3.sub_package.sub_sub_package.sub_sub_sub_package import (  # noqa
  LineLengthExceeded,
)
# Symbol imports
# - No linewrap needed
from yyy_module import YyySymbol1, YyySymbol2
# - Linewrap needed
from zzz_module import (
   ZzzSymbolA,
   ZzzSymbolB,
   ZzzSymbolC,
   ZzzSymbolD,
)

EDIT: On second thought, perhaps import pkg.sub as sub is better than from pkg import sub, just as a way to show that it's a module, not a symbol.

\cc @thduynguyen @RussTedrake

Metadata

Metadata

Type

No type

Projects

Status

On deck

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions