-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Summary
Several of Ruff's rules will be more aggressive in the diagnostics and autofixes they emit if from __future__ import annotations is enabled in the file:
For this reason, it's often useful to have from __future__ import annotations at the top of your Python file if you're running Ruff. Ruff knows that it can do a lot more with your code in these situations!
Ruff provides a lint rule FA100 that detects situations where UP035 or UP045 would make autofix edits if from __future__ import annotations was present, and complains that you should probably add that import. But this rule has two problems:
- It does not have an autofix
- It doesn't tackle situations where other rules such as the
TCones might make edits to your code iffrom __future__ import annotationswere present in the module
Ruff provides a way to ensure that from __future__ import annotations is imported in every file in your project: you can use I002 in combination with the required-imports setting. But applying this to a large existing codebase might cause a huge diff with many unnecessary edits in it, which is probably undesirable.
I propose that we:
- Add a configuration option to make
FA100more expansive, so that it also triggers in situations where theTCrules might make changes to your code iffrom __future__ import annotationswas present.- I think this should be behind a configuration option, as the
TCrules are very opinionated: not all users will wantFA100to also apply to these situations. Alternatively, we could add a newFA*rule that sits alongsideFA100and flags these situations -- this comment argues in favour of doing that instead.
- I think this should be behind a configuration option, as the
- Check to see if there are other rules where Ruff would be more aggressive if
from __future__ import annotationswere present. Should we haveFA100also flag modules where these other rules would make edits iffrom __future__ import annotationswere present in the file? - Add an autofix to
FA100that auto-addsfrom __future__ import annotations