-
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
] Adds implementation for TC006
#14511
base: main
Are you sure you want to change the base?
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
TC006 | 431 | 431 | 0 | 0 | 0 |
TC001 | 117 | 117 | 0 | 0 | 0 |
TC003 | 21 | 21 | 0 | 0 | 0 |
E501 | 1 | 1 | 0 | 0 | 0 |
TC002 | 1 | 1 | 0 | 0 | 0 |
The extra hits for the other rules make sense to me, since fixing In the case of |
#[test_case(Rule::RuntimeCastValue, Path::new("TC006.py"))] | ||
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> { | ||
let snapshot = format!("preview__{}_{}", rule_code.as_ref(), path.to_string_lossy()); | ||
let diagnostics = test_path( | ||
Path::new("flake8_type_checking").join(path).as_path(), | ||
&settings::LinterSettings { | ||
preview: PreviewMode::Enabled, | ||
..settings::LinterSettings::for_rule(rule_code) | ||
}, | ||
)?; | ||
assert_messages!(snapshot, diagnostics); | ||
Ok(()) | ||
} | ||
|
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 know this is a pattern that we have in other linter-groups too, but I don't think it's actually necessary to test a preview-rule. It's only necessary if the rule depends on any other preview-only behavior
let edit = quote_type_expression(type_expr, checker.semantic(), checker.stylist()).ok(); | ||
if let Some(edit) = edit.as_ref() { | ||
if checker | ||
.comment_ranges() | ||
.has_comments(type_expr, checker.source()) | ||
{ | ||
diagnostic.set_fix(Fix::unsafe_edit(edit.clone())); | ||
} else { | ||
diagnostic.set_fix(Fix::safe_edit(edit.clone())); | ||
} | ||
} |
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 clone calls here seem unnecessary
let edit = quote_type_expression(type_expr, checker.semantic(), checker.stylist()).ok(); | |
if let Some(edit) = edit.as_ref() { | |
if checker | |
.comment_ranges() | |
.has_comments(type_expr, checker.source()) | |
{ | |
diagnostic.set_fix(Fix::unsafe_edit(edit.clone())); | |
} else { | |
diagnostic.set_fix(Fix::safe_edit(edit.clone())); | |
} | |
} | |
let edit = quote_type_expression(type_expr, checker.semantic(), checker.stylist()).ok(); | |
if let Some(edit) = edit { | |
if checker | |
.comment_ranges() | |
.has_comments(type_expr, checker.source()) | |
{ | |
diagnostic.set_fix(Fix::unsafe_edit(edit)); | |
} else { | |
diagnostic.set_fix(Fix::safe_edit(edit)); | |
} | |
} |
Thanks for contributing this rule. The code changes look good to me. I want to wait for updated ecosystem checks. I don't see why this PR should result in any new diagnostics other than TC006. |
def f(): | ||
import typing as t | ||
|
||
t.cast(int, 3.0) # TC006 |
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.
Could we add a test where parts of the type are quoted?
t.cast(int, 3.0) # TC006 | |
t.cast(Literal["3.0", '3'], 3.0) # TC006 |
I'm a bit confused by the ecosystem checks. I understand that we run ruff exactly once and don't apply any fixes. That makes it unclear to me why there would be any changed diagnostics other than for TC006. |
Now that
TCH
has been renamed toTC
and the redirect fromTCH006
toTCH010
can no longer bite us, I've added an implementation for this very simple rule.Summary
TC006
checks for non-string literal arguments totyping.cast
which adds unnecessary runtime overhead, since the function doesn't do anything at runtime.This PR has a very tiny overlap with my other PR for
TCH007
/TCH008
inflake8_type_checking/helpers.rs
for adding thequote_type_expression
function, but the two changes don't really depend on one another, so they can be merged in any order.Test Plan
cargo nextest run