-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
False-positive TCH002 for runtime-required type annotations #14140
Comments
Thanks for the nice write up! This sounds related to #13713 |
Did you try using |
Sorry, I was on vacation, so I could not answer quickly. Yes, I tested this (and checked again today) and got the same result as I described in the issue description UPD: Let me check it with the 0.7.4 release |
Checked again against https://github.com/astral-sh/ruff/releases/tag/0.7.4 The same results were obtained after adding the following to the project.toml:
|
To clarify, I believe you want: [tool.ruff.lint.flake8-type-checking]
runtime-evaluated-decorators = [
"injector.inject",
] Notice the lower-case |
I checked again with this one. Still working not as intended: I have specified pyproject.toml as
Checked for both versions of the declaration:
In a scenario where only typing annotation is used, e.g., Inject[ServiceA], the error still exists.
This one still generates false positives. |
Sorry for bumping it up; the reference task (#13713) has been closed. But this error (with runtime-required) annotation is still reproducible. I checked it with Ruff 0.11.0
The expected behaviour of Injector type annotation is the fact annotation itself and everything under it I available in the runtime |
Thanks for the reminder. Here's a reproduction in the playground, which might be a little easier for others to try out. @Daverball do you know if this is resolved by any of your open PRs? I know you've been working on these rules and would know better than me. |
No, but it's tangentially related to one of my open issues, so I plan on tackling this issue eventually: #16412 We would definitely need a new setting, so we can treat certain generics as always runtime required. What makes this particular case a lot trickier however, is that generally it would not be enough just to treat this generic and anything it encloses as runtime required, since most naïve uses of runtime type information will use A workaround for this that currently works is to define a no-op decorator and add it to |
In our project, we are using https://injector.readthedocs.io/en/latest/ library to implement dependency injection.
For proper workflow, it requires the following reference of injecting types:
(link to the documentation - https://injector.readthedocs.io/en/latest/api.html#injector.Inject)
Both methods (type annotation and regular annotation) require that the context (
Inject
itself or any type underneath) be available during runtime.According to the documentation, I added the required annotations to the
flake8-type-checking
configuration.But it does not work as expected and returns false positive warnings.
You can reproduce it using the following sanitized sample:
After running a ruff check for this file, I get the following response:
This issue is reproducible not only for exactly this library but for any typing annotations evaluated at runtime in similar fashion
The text was updated successfully, but these errors were encountered: