-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
] Improve flexibility of runtime-evaluated-decorators
#15204
[flake8-type-checking
] Improve flexibility of runtime-evaluated-decorators
#15204
Conversation
This also slightly changes semantics of the settings to support a common pattern used by e.g. `FastAPI`. We will resolve assignments so you can do things like this: ```python from __future__ import annotations import fastapi from mymodule import Foo app = fastapi.FastAPI("My App") @app.get("/home") def home() -> Foo: ... ``` And add `fastapi.FastAPI.*` to `runtime-evaluated-decorators` in order to prevent a `TC001` violation for `from mymodule import Foo`.
|
It seems I was a few minutes too slow. I replied on #15060 |
@MichaReiser No worries, the glob part is honestly the smaller part of this proposal. You can make it work without the glob support. It just seemed really annoying to me having to manually list all the decorators. With some frameworks you may very well be looking at multiple dozens of methods. The decorator as application configuration hook style is quite popular. So I can take it back out if we're sure we really don't want it. |
Thanks @Daverball My preference would be to start without it and add it if users run into it. We then also have the option to a) allow regex or b) do a prefix match |
flake8-type-checking
] Allow globs in runtime-evaluated-decorators
flake8-type-checking
] Improve flexibility of runtime-evaluated-decorators
The only thing I dislike about this approach, is that it forces you to write two separate entries for the same module and separate module case. I also considered adding an option to |
Can you expand on what you mean by this |
Sure, if you have a look at the new tests, you'll notice that I'm adding an entry for So |
Thanks, that makes sense, considering that Ruff can't see what |
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.
Nice, thank you. I think it would be great if we could extend the setting documentation to cover both using a framework decorator but also an instance method from a symbol exported from a user module
for member in reversed_tail.iter().rev() { | ||
// extend the qualified name with the attributes we accessed | ||
// e.g. for `app.get` when `app` resolves to `fastapi.FastAPI` | ||
// then we want to return `fastapi.FastAPI.get`. | ||
qualified_name = qualified_name.append_member(member); | ||
} |
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.
Nit: We could consider adding an extend
method` to reduce the code
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 struggled a bit to get the lifetimes correct. If there's a more idiomatic way to write the function, feel free to go ahead and fix it. But I definitely like how it simplified resolve_assignment
, I also noticed that the two match arms were redundant, so I consolidated them.
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.
The trick was to use into_iterator
so that the items have the type &'a str
and not &&'a str
. The alternative would have been to use iter().copied()
, I think, which dereferences &&'a str
to &'a str
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.
Thanks
This is an alternative proposal towards accomplishing the use-case #15060 is trying to provide, which doesn't require adding a new setting.
Summary
This slightly changes semantics of the setting to support a common pattern used by e.g.
FastAPI
. We will resolve assignments so you can do things like this:And add
fastapi.FastAPI.get
toruntime-evaluated-decorators
in order to prevent aTC001
violation forfrom mymodule import Foo
.Test Plan
cargo nextest run