ruff/crates/ruff_linter/src
plredmond da882b6657
F401 - Recommend adding unused import bindings to __all__ (#11314)
Followup on #11168 and resolve #10391

# User facing changes

* F401 now recommends a fix to add unused import bindings to to
`__all__` if a single `__all__` list or tuple is found in `__init__.py`.
* If there are no `__all__` found in the file, fall back to recommending
redundant-aliases.
* If there are multiple `__all__` or only one but of the wrong type (non
list or tuple) then diagnostics are generated without fixes.
* `fix_title` is updated to reflect what the fix/recommendation is.

Subtlety: For a renamed import such as `import foo as bees`, we can
generate a fix to add `bees` to `__all__` but cannot generate a fix to
produce a redundant import (because that would break uses of the binding
`bees`).

# Implementation changes

* Add `name` field to `ImportBinding` to contain the name of the
_binding_ we want to add to `__all__` (important for the `import foo as
bees` case). It previously only contained the `AnyImport` which can give
us information about the import but not the binding.
* Add `binding` field to `UnusedImport` to contain the same. (Naming
note: the field `name` field already existed on `UnusedImport` and
contains the qualified name of the imported symbol/module)
* Change `fix_by_reexporting` to branch on the size of `dunder_all:
Vec<&Expr>`
* For length 0 call the edit-producing function `make_redundant_alias`.
  * For length 1 call edit-producing function `add_to_dunder_all`.
  * Otherwise, produce no fix.
* Implement the edit-producing function `add_to_dunder_all` and add unit
tests.
* Implement several fixture tests: empty `__all__ = []`, nonempty
`__all__ = ["foo"]`, mis-typed `__all__ = None`, plus-eq `__all__ +=
["foo"]`
* `UnusedImportContext::Init` variant now has two fields: whether the
fix is in `__init__.py` and how many `__all__` were found.

# Other changes

* Remove a spurious pattern match and instead use field lookups b/c the
addition of a field would have required changing the unrelated pattern.
* Tweak input type of `make_redundant_alias`

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2024-05-14 17:02:33 -07:00
..
checkers Move UP034 to use TokenKind instead of Tok (#11424) 2024-05-14 17:28:04 +00:00
comments Rename ruff crate to ruff_linter (#7529) 2023-09-20 08:38:27 +02:00
cst Wrap expressions in parentheses when negating (#10346) 2024-03-11 18:20:55 -04:00
docstrings [pydocstyle-D405] Allow using parameters as a sub-section header (#9894) 2024-02-08 21:54:32 -05:00
fix F401 - Recommend adding unused import bindings to __all__ (#11314) 2024-05-14 17:02:33 -07:00
importer Add Tokens newtype wrapper, TokenKind iterator (#11361) 2024-05-14 16:45:04 +00:00
message Feature: Add SARIF output support (#9078) 2023-12-13 00:33:19 -05:00
registry Avoid parsing the root configuration twice (#10625) 2024-05-01 09:28:30 +00:00
rules F401 - Recommend adding unused import bindings to __all__ (#11314) 2024-05-14 17:02:33 -07:00
settings Add Python 3.13 to list of allowed Python versions (#11411) 2024-05-13 16:35:41 +00:00
snapshots [ruff] Implement redirected-noqa (RUF101) (#11052) 2024-04-27 02:08:11 +00:00
codes.rs [flake8-pyi] Implement PYI062 (duplicate-literal-member) (#11269) 2024-05-07 19:28:06 +01:00
directives.rs Rename AnyStringKind -> AnyStringFlags (#11405) 2024-05-13 13:18:07 +00:00
doc_lines.rs Use TokenKind in doc_lines_from_tokens (#11418) 2024-05-14 16:56:14 +00:00
fs.rs Struct not tuple for compiled per-file ignores (#10864) 2024-04-11 13:47:57 -06:00
lib.rs Split add_noqa process into distinctive edit generation and edit application stages (#11265) 2024-05-10 23:16:52 +00:00
line_width.rs Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
linter.rs Use TokenKind in doc_lines_from_tokens (#11418) 2024-05-14 16:56:14 +00:00
logging.rs Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
noqa.rs Split add_noqa process into distinctive edit generation and edit application stages (#11265) 2024-05-10 23:16:52 +00:00
packaging.rs Nested namespace packages support (#10541) 2024-03-24 22:53:32 -04:00
pyproject_toml.rs Update pyproject-toml to 0.8 (#8351) 2023-10-30 10:05:37 +00:00
registry.rs Move W605 to the AST checker (#11402) 2024-05-13 16:13:06 +00:00
renamer.rs Avoid debug assertion around NFKC renames (#11249) 2024-05-02 10:59:39 -07:00
rule_redirects.rs linter: Enable test-rules for test build (#11201) 2024-04-30 08:06:47 +02:00
rule_selector.rs linter: Enable test-rules for test build (#11201) 2024-04-30 08:06:47 +02:00
source_kind.rs CLI: Color entire line in Diffs (#10183) 2024-03-01 13:53:45 +01:00
test.rs Add Tokens newtype wrapper, TokenKind iterator (#11361) 2024-05-14 16:45:04 +00:00
upstream_categories.rs Fix pylint upstream categories not showing in docs (#10441) 2024-03-18 01:27:39 +00:00