ruff
9b89bf7d - Implement pylint import-outside-toplevel rule (C0415) (#5180)

Commit
1 year ago
Implement pylint import-outside-toplevel rule (C0415) (#5180) ## Summary Implements pylint C0415 (import-outside-toplevel) — imports should be at the top level of a file. The great debate I had on this implementation is whether "top-level" is one word or two (`toplevel` or `top_level`). I opted for 2 because that seemed to be how it is used in the codebase but the rule string itself uses one-word "toplevel." 🤷 I'd be happy to change it as desired. I suppose this could be auto-fixed by moving the import to the top-level, but it seems likely that the author's intent was to actually import this dynamically, so I view the main point of this rule is to force some sort of explanation, and auto-fixing might be annoying. For reference, this is what "pylint" reports: ``` > pylint crates/ruff/resources/test/fixtures/pylint/import_outside_top_level.py ************* Module import_outside_top_level ... crates/ruff/resources/test/fixtures/pylint/import_outside_top_level.py:4:4: C0415: Import outside toplevel (string) (import-outside-toplevel) ``` ruff would now report: ``` import_outside_top_level.py:4:5: PLC0415 `import` should be used only at the top level of a file | 3 | def import_outside_top_level(): 4 | import string # [import-outside-toplevel] | ^^^^^^^^^^^^^ PLC0415 | ``` Contributes to https://github.com/astral-sh/ruff/issues/970. ## Test Plan Snapshot test.
Author
Parents
Loading