-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[flake8-type-checking
] Fixes quote_type_expression
#14614
Conversation
@MichaReiser Could you take a quick look at this to see if this is heading in a viable direction? |
|
I don't fully understand the problem yet, so sorry if I'm way off here. I understand that we want to mutate the type annotation expression, and the last step is to write it into a quoted string. However, the problem is that the I wonder if we, instead, should change the We could then use This seems to me like an overall less-invasive change. |
That sounds like a good idea, but is somewhat orthogonal to this change. So it's something we should change either way, regardless of how we remove the forward references. Rewriting the AST was my first instinct for a proof of concept, since it seemed really easy. But I don't think it's safe unless I create a copy of all the nodes in the expression first, since otherwise other rules will encounter the transformed AST before the file has actually been fixed. And doing that copy seems really slow and inefficient, traversing the expression twice is bad enough. I haven't found any uses of I think there's additional potential future benefits to extracting some of the core functionality of |
I'm not that concerned about performance:
Possibly, but I'd prefer a more local fix if possible and we can revisit a more flexible |
@MichaReiser Fair enough, I will go the Are there already some utilities for copying parts of the AST? |
It performs a deep clone of that expression. Many
ruff/crates/ruff_linter/src/rules/flake8_bugbear/rules/setattr_with_constant.rs Lines 49 to 61 in fb94b71
|
@MichaReiser One other flaw of I'm not sure which way defaulting to Missing required parentheses would be a bug magnet, that could change the semantics of generated expressions, but generating redundant parentheses seems kind of bad too. It doesn't seem possible to provide a higher level entry point, where we automatically calculate the precedence based on the parent node, since you would need to know which of the attributes on the parent node the expression is attached to, since typically not all of them share the same precedence. So letting people optionally specify the precedence seems like the next best thing. |
I don't mind adding |
Fixes: #14538
Fixes: #14554
Summary
This achieves a more feature complete version of wrapping annotations in quotes while removing nested forward references and handling nested quotes correctly.
This is currently just a proof of concept and the implementation could do with some refactoring and cleaning up. This should also additional test cases for regression tests, but there is already one existing test case showcasing that we now can correctly handle nested quotes.
It's also not currently making use of the parsed annotations cache. Instead of passing the semantic model, locator and stylizer separately, we probably should just pass the checker to these helper functions, then we can use the cached annotation parsing lookup.
Test Plan
cargo nextest run