ruff
b80d8ff6 - [`ruff`] Detect duplicate entries in `__all__` (`RUF068`) (#22114)

Commit
98 days ago
[`ruff`] Detect duplicate entries in `__all__` (`RUF068`) (#22114) Hello, This MR adds a new rule and its fix, `RUF069`, `DuplicateEntryInDunderAll`. I'm using `RUF069` because we already have [RUF068](https://github.com/astral-sh/ruff/pull/20585) and [RUF069](https://github.com/astral-sh/ruff/pull/21079#issuecomment-3493839453) in the works. The rule job is to prevent users from accidentally adding duplicate entries to `__all__`, which, for example, can result from copy-paste mistakes. It deals with the following syntaxes: ```python __all__: list[str] = ["a", "a"] __all__: typing.Any = ("a", "a") __all__.extend(["a", "a"]) __all__ += ["a", "a"] ``` But it does not keep track of `__all__` contents, meaning the following code snippet is a false negative: ```python class A: ... __all__ = ["A"] __all__.extend(["A"]) ``` ## Violation Example ```console RUF069 `__all__` contains duplicate entries --> RUF069.py:2:17 | 1 | __all__ = ["A", "A", "B"] | ^^^ help: Remove duplicate entries from `__all__` 1 | __all__ = ["A", "B"] - __all__ = ["A", "A", "B"] ``` ## Ecosystem Report The `ruff-ecosystem` results contain seven violations in four projects, all of them seem like true positives, with one instance appearing to be an actual bug. This [code snippet](https://github.com/python/typeshed/blob/90d855985be5aae9bc76e77b0f3d4b6738c38347/stubs/reportlab/reportlab/lib/rltempfile.pyi#L4) from `reportlab` contains the same entry twice instead of exporting both functions. ```python def get_rl_tempdir(*subdirs: str) -> str: ... def get_rl_tempfile(fn: str | None = None) -> str: ... __all__ = ("get_rl_tempdir", "get_rl_tempdir") ``` Closes [#21945](https://github.com/astral-sh/ruff/issues/21945) --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
Author
Parents
Loading