-
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
TC003/TC002/TC001 False positive on manual TYPE_CHECKING
with __future__.annotations
#15681
Comments
In the original flake8 plugin I treat all We could be more narrow and just detect this specific case, where in addition to the above condition, if the qualified name doesn't match a typing module, |
The only other thing I'll say, is that the fixes for the TC range currently don't support this "avoid importing I.e. when the The other caveat is, that the fix for TC005 would need to remove It's also worth noting that ruff supports |
@carljm @AlexWaygood what are our plans for Red Knot around supporting non- |
The recent discussion I talked about can be found here: https://discuss.python.org/t/specify-type-checking-false-without-typing-import/76766 |
FWIW, both mypy and Pyright seems to support non-
|
Yup, although if you look at the linked discussion, Eric opposes the addition of this form to the typing spec, so I'm guessing the only reason pyright currently supports it, is so he can offer better feature parity with mypy. The discussion didn't really reach a consensus about how to support this use-case. Although given, that mypy and pyright have the biggest install bases currently, I don't see a strong reason for ruff to hold out on support for this, even if it ends up being phased out again, like the previous I personally think |
I'm not sure. My vague memory is that I had commented somewhere that we should support this, and there was some pushback (I don't recall from who) so we deferred it. I can't seem to find that discussion now. I don't have strong feelings in either direction, but my inclination is that we should support it in order to lower migration barriers from mypy/pyright, because I don't think there is much cost in supporting it. |
TYPE_CHECKING
with __future__.annotations
This one? #14759 (comment) |
I tried to find that comment thread yesterday but failed — thanks @sharkdp! I still have the same opinion as I did then, that treating all variables named |
Unfortunately that's not really a viable solution for library authors. You can't really ask that all your downstream users adjust their type checker configuration for your special The use-case is pretty clear. People want there to be a portable way for not making every downstream user pay the cost of the While shipping separate stub files achieves that goal, it can be a pain to maintain. So I have sympathy for people that want there to be a standard way, to do the I do however agree with you, that this solution feels icky (especially since it messes with runtime type checking) and I think we can provide a better solution eventually, where we don't have to rely on But until we have a better solution, it would be nice to give people a way that's at least supported by the most popular type checkers right now, even if it's not part of the typing spec. |
I agree with @Daverball here. I agree that in principle it's icky to special case a variable name, but I think the practical harm is very small, the use case is real, and the version that requires explicit configuration is not usable for libraries. So I would support exploring standardizing nicer ways to do this, but pending that I think we should match mypy and pyright. |
Yeah, that all seems reasonable to me; I'm happy to go with that. I agree that @Daverball makes a really good point about libraries. |
Description
If you manually set
TYPE_CHECKING
toFalse
and use it with__future__.annotations
, ruff will raise TC003/TC002/TC001 falsepositives on the imports in yourTYPE_CHECKING
block as it seems to only check fortyping.TYPE_CHECKING
.Command:
ruff check test.py --isolated --fix --select TC
Version:
0.9.2
Keywords: "TYPE_CHECKING", "TC003", "future.annotations", "TC002", "TC001"
Sample Code
test.py
The text was updated successfully, but these errors were encountered: