astral-sh / ruff

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

Make E741 not apply to stub files #10569

Open AlexWaygood opened 5 months ago

AlexWaygood commented 5 months ago

I think it would make sense to have E741 (ambiguous-variable-name) not apply to stub files.

Unlike with runtime .py files, stub authors generally have no control over the names of variables that appear in a stub. A well-written stub should aim to faithfully represent the interface of the equivalent .py file as it exists at runtime, and that includes any ambiguously named variables in the runtime module. Even if the source code for the stubs is located in the same repository as the runtime module being stubbed, you likely won't be able to change an ambiguously named variable in the stub without also changing the corresponding runtime module, and tools like stubtest will generally help stub authors ensure consistency between the runtime module and the stub.

Typeshed has always ignored this rule for .pyi files in our ruff config (and before that, in our .flake8 config), and I can't think of a situation in which it would be useful when applied to stubs.

Would we need some kind of deprecation period for this change? Or could we just make it?

(F403 and F405 (regarding * imports) similarly flag things that are out of the control of stub authors. But these rules highlight places where ruff's inference capabilities will be severely limited due to the specific idioms being used, so I think we should probably keep these enabled by default, even for .pyi files.)

charliermarsh commented 4 months ago

I think this makes sense! We could ship it as part of v0.5.0 if you want?

AlexWaygood commented 2 weeks ago

We've now made this change in preview mode; we'll be stabilising it in Ruff 0.7. Thanks @calumy!