astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
30.89k stars 1.02k forks source link

`__all__` related rules #12962

Open Goldziher opened 3 weeks ago

Goldziher commented 3 weeks ago
  1. I would like to ask for a rule that requires an __all__ list to be declared in each file where there are non-private members, and all the non-private members are listed in it.
  2. Another helpful rule is to detect declarations in __all__ that are unused 3. Another rule would be to automatically order __all__
  3. Another rule might be to enforce private module members when no exported, or require they are in __all__ if used outside
AlexWaygood commented 3 weeks ago

3. Another rule would be to automatically order __all__

This already exists as unsorted-dunder-all in our ruff category!

Goldziher commented 3 weeks ago
  1. Another rule would be to automatically order __all__

This already exists as unsorted-dunder-all in our ruff category!

oh I didn't notice. well that's nice.

MichaReiser commented 3 weeks ago
  1. Another helpful rule is to detect declarations in all that are unused

This would require multifile analysis, which Ruff doesn't support today. Even then, the rule would be inherently unsafe for libraries where a symbol in __all__ might be imported by another application. But I do agree that knowing about "dead code" in general would be useful.

I would like to ask for a rule that requires an all list to be declared in each file where there are non-private members, and all the non-private members are listed in it.

Can you help me understand the motivation for explicitly requiring __all__ when all members are public? Or is __all__ only required if there's at least one private and one public member in the file?

Another rule might be to enforce private module members when no exported, or require they are in all if used outside

This also requires multifile analysis

Goldziher commented 3 weeks ago

Can you help me understand the motivation for explicitly requiring all when all members are public? Or is all only required if there's at least one private and one public member in the file?

Yes. It's a way to enforce modules with explicit public exports.