-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-type-checking
] Allows TC001-004 to quote more expressions
#14787
base: main
Are you sure you want to change the base?
[flake8-type-checking
] Allows TC001-004 to quote more expressions
#14787
Conversation
crates/ruff_workspace/src/options.rs
Outdated
/// to inconsistent style with `typing.cast` calls where sometimes the | ||
/// type expression is quoted or partially quoted and other times not, | ||
/// if you'd like to consistently quote type expressions, you should | ||
/// instead consider enabling [ruff_linter::registry::Rule::RuntimeCastValue]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this is not the right way to cross reference another rule, do I have to manually link to the rule's page? It seems like all the other references to other settings in here manually link via fragments, rather than relying on the docs to create the correct cross reference.
f5e57a4
to
3c961da
Compare
|
4fc3c55
to
c05eb50
Compare
c05eb50
to
112f3a3
Compare
Interesting, thanks for working on this! My overall reaction is that this is a good idea. Do we need another configuration knob, though? What's the profile of somebody who wants their annotations to be quoted if it removes the need for a runtime import, but doesn't want the first argument to I get that the It looks like your patch already does the right thing for all of these, but if you haven't already added some, it would be great to add tests for them, because it would obviously be incorrect to quote either of them: from typing import Annotated, TypeAlias
from expensive import Foo
from expensive2 import Foo2
Y: TypeAlias = Annotated[Foo, "metadata"]
some_object = Y()
class Z(Annotated[Foo2, "metadata"]): ... |
I do think we want some additional knobs, but I do agree that having three separate toggleable switches is awkward, that's the main reason I called this half-baked. I think a sliding scale could make sense, at the low end you would only quote casts, then runtime evaluated annotations too and finally annotated type aliases at the high end. I do agree that there's likely nobody that would have e.g. annotations turned on but casts turned off, but I do think it's likely that there are people that want to have casts turned on, but annotations and type aliases turned off. Especially type aliases is something some people might not want to ever turn on if they have a bunch of them in a single module that is used by other modules at runtime. They could just turn off TC001-004 in that module, but that seems awkward. Maybe we add a new setting and deprecate It might also make it easier to argue for making Then we can emit a warning for people that have preview turned on and still use the old setting. People that have preview turned off will return the old default of
I do plan to add more tests. I added just enough to make sure that the type alias part works correctly, since that is the most difficult one to get right. |
@AlexWaygood @MichaReiser Any inputs on the idea of using a sliding scale for configuring what kind of type expressions we're allowed to quote? This would be similar in spirit to |
Sorry for the delayed response. It makes me sad to have to deprecate a configuration setting, because it's quite disruptive for our users. However, I think your plan makes sense; I can't think of a better one. I agree that there is probably a group of people who want to quote their We can bikeshed over the names of the sliding scale, but your overall plan sounds good to me. We'll probably have to do the deprecation as part of the next minor release (I think probably in early January, but not sure), but there's no harm in working on a PR now. It'll make the minor release less frantic if it's already ready and has already been reviewed :-) |
One loosely related thing I have been thinking about is the Before I had this idea I still had intended to deprecate this setting and add a new one in order to allow the strategy to be one of We could have a separate setting for |
Hmm, I kinda think the status quo of telling people to use the isort setting if they want to have the |
@AlexWaygood My concern are imports that will be flagged by TC004 for runtime use at the same time as the missing Which I assume is also the reason why TC001-004 have been repurposed by ruff to do the quoting of the annotation and moving of the import in a single step, rather than use a separate TC2xx rule to flag annotations like the flake8 plugin does. That being said, the |
Running Ruff once with |
@AlexWaygood Sorry, I was being a bit unclear. My concern is the redundant work that ruff does for the consecutive fixes and potentially moving the imports to a slightly different location in the code, since the fixes for TC001-004 always move imports to the top of the file/to a type checking block just below all the imports at the top, regardless of where they were originally defined. So it's more that it might create a bit of mess, that you will then have to revert, not so much that it will result in broken code or end up in an unfixable state. |
I'm still not convinced that the current situation is that bad. (This conversation is getting a bit too hypothetical -- it's hard to say without concrete examples to look at where this actually creates a mess. And the redundant work that Ruff does doesn't concern me much unless we can demonstrate that there's a measurable speedup from doing something differently -- which I'm sceptical of :) But this all feels like a bit of a tangent anyway. Even if it is desirable to make the way these violations are fixed be configurable, this could easily be done with a separate configuration option. |
It definitely is a tangent yes. The only reason I brought it up, is, that if there ended up being a need for an additional setting, that may inform the design of this one, in order to minimize confusion. But I'm happy to punt on this and only let it be an issue once it actually becomes one. |
This idea came up during the discussion of #14676
This is not yet fully baked, but far enough along to clearly see the vision, so I would appreciate some feedback, before I put more work into this @AlexWaygood @MichaReiser
Summary
This adds additional settings to allow TC001-004 to be more liberal about moving imports into type checking blocks.
Test Plan
cargo nextest run