From 85a7edcc708c3bd9b3f7cf6bc483afdb66217a97 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 30 Jan 2024 20:28:52 -0800 Subject: [PATCH] Recategorize `runtime-string-union` to `TCH010` (#9721) ## Summary This rule was added to `flake8-type-checking` as `TC010`. We're about to stabilize it, so we might as well use the correct code. See: https://github.com/astral-sh/ruff/issues/9573. --- .../fixtures/flake8_type_checking/TCH006_1.py | 18 -------- .../fixtures/flake8_type_checking/TCH006_2.py | 16 -------- .../fixtures/flake8_type_checking/TCH010_1.py | 18 ++++++++ .../fixtures/flake8_type_checking/TCH010_2.py | 16 ++++++++ crates/ruff_linter/src/codes.rs | 2 +- crates/ruff_linter/src/rule_redirects.rs | 1 + .../src/rules/flake8_type_checking/mod.rs | 4 +- ...sts__runtime-string-union_TCH006_1.py.snap | 12 ------ ...sts__runtime-string-union_TCH006_2.py.snap | 41 ------------------- ...sts__runtime-string-union_TCH010_1.py.snap | 12 ++++++ ...sts__runtime-string-union_TCH010_2.py.snap | 41 +++++++++++++++++++ ruff.schema.json | 3 +- 12 files changed, 93 insertions(+), 91 deletions(-) delete mode 100644 crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_1.py delete mode 100644 crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_2.py create mode 100644 crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_1.py create mode 100644 crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_2.py delete mode 100644 crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_1.py.snap delete mode 100644 crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_2.py.snap create mode 100644 crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_1.py.snap create mode 100644 crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_2.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_1.py b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_1.py deleted file mode 100644 index 7e8df4ce10c17a..00000000000000 --- a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_1.py +++ /dev/null @@ -1,18 +0,0 @@ -from __future__ import annotations - -from typing import TypeVar - - -x: "int" | str # TCH006 -x: ("int" | str) | "bool" # TCH006 - - -def func(): - x: "int" | str # OK - - -z: list[str, str | "int"] = [] # TCH006 - -type A = Value["int" | str] # OK - -OldS = TypeVar('OldS', int | 'str', str) # TCH006 diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_2.py b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_2.py deleted file mode 100644 index 0c7d5915b7f168..00000000000000 --- a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH006_2.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import TypeVar - - -x: "int" | str # TCH006 -x: ("int" | str) | "bool" # TCH006 - - -def func(): - x: "int" | str # OK - - -z: list[str, str | "int"] = [] # TCH006 - -type A = Value["int" | str] # OK - -OldS = TypeVar('OldS', int | 'str', str) # TCH006 diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_1.py b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_1.py new file mode 100644 index 00000000000000..d640994930ad84 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_1.py @@ -0,0 +1,18 @@ +from __future__ import annotations + +from typing import TypeVar + + +x: "int" | str # TCH010 +x: ("int" | str) | "bool" # TCH010 + + +def func(): + x: "int" | str # OK + + +z: list[str, str | "int"] = [] # TCH010 + +type A = Value["int" | str] # OK + +OldS = TypeVar('OldS', int | 'str', str) # TCH010 diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_2.py b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_2.py new file mode 100644 index 00000000000000..09fae9a5e6c931 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH010_2.py @@ -0,0 +1,16 @@ +from typing import TypeVar + + +x: "int" | str # TCH010 +x: ("int" | str) | "bool" # TCH010 + + +def func(): + x: "int" | str # OK + + +z: list[str, str | "int"] = [] # TCH010 + +type A = Value["int" | str] # OK + +OldS = TypeVar('OldS', int | 'str', str) # TCH010 diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index 750495772d9b05..6ba3d90e664bc7 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -841,7 +841,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Flake8TypeChecking, "003") => (RuleGroup::Stable, rules::flake8_type_checking::rules::TypingOnlyStandardLibraryImport), (Flake8TypeChecking, "004") => (RuleGroup::Stable, rules::flake8_type_checking::rules::RuntimeImportInTypeCheckingBlock), (Flake8TypeChecking, "005") => (RuleGroup::Stable, rules::flake8_type_checking::rules::EmptyTypeCheckingBlock), - (Flake8TypeChecking, "006") => (RuleGroup::Stable, rules::flake8_type_checking::rules::RuntimeStringUnion), + (Flake8TypeChecking, "010") => (RuleGroup::Stable, rules::flake8_type_checking::rules::RuntimeStringUnion), // tryceratops (Tryceratops, "002") => (RuleGroup::Stable, rules::tryceratops::rules::RaiseVanillaClass), diff --git a/crates/ruff_linter/src/rule_redirects.rs b/crates/ruff_linter/src/rule_redirects.rs index 7115a252a9078e..12408c56bd2df5 100644 --- a/crates/ruff_linter/src/rule_redirects.rs +++ b/crates/ruff_linter/src/rule_redirects.rs @@ -99,5 +99,6 @@ static REDIRECTS: Lazy> = Lazy::new(|| { ("T003", "FIX003"), ("T004", "FIX004"), ("RUF011", "B035"), + ("TCH006", "TCH010"), ]) }); diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/mod.rs b/crates/ruff_linter/src/rules/flake8_type_checking/mod.rs index ef105e99ff29fc..ed513d3236c6cf 100644 --- a/crates/ruff_linter/src/rules/flake8_type_checking/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_type_checking/mod.rs @@ -35,8 +35,8 @@ mod tests { #[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TCH004_8.py"))] #[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("TCH004_9.py"))] #[test_case(Rule::RuntimeImportInTypeCheckingBlock, Path::new("quote.py"))] - #[test_case(Rule::RuntimeStringUnion, Path::new("TCH006_1.py"))] - #[test_case(Rule::RuntimeStringUnion, Path::new("TCH006_2.py"))] + #[test_case(Rule::RuntimeStringUnion, Path::new("TCH010_1.py"))] + #[test_case(Rule::RuntimeStringUnion, Path::new("TCH010_2.py"))] #[test_case(Rule::TypingOnlyFirstPartyImport, Path::new("TCH001.py"))] #[test_case(Rule::TypingOnlyStandardLibraryImport, Path::new("TCH003.py"))] #[test_case(Rule::TypingOnlyStandardLibraryImport, Path::new("init_var.py"))] diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_1.py.snap b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_1.py.snap deleted file mode 100644 index 4ab8798d518c2c..00000000000000 --- a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_1.py.snap +++ /dev/null @@ -1,12 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs ---- -TCH006_1.py:18:30: TCH006 Invalid string member in `X | Y`-style union type - | -16 | type A = Value["int" | str] # OK -17 | -18 | OldS = TypeVar('OldS', int | 'str', str) # TCH006 - | ^^^^^ TCH006 - | - - diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_2.py.snap b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_2.py.snap deleted file mode 100644 index 8a2023ed441895..00000000000000 --- a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH006_2.py.snap +++ /dev/null @@ -1,41 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs ---- -TCH006_2.py:4:4: TCH006 Invalid string member in `X | Y`-style union type - | -4 | x: "int" | str # TCH006 - | ^^^^^ TCH006 -5 | x: ("int" | str) | "bool" # TCH006 - | - -TCH006_2.py:5:5: TCH006 Invalid string member in `X | Y`-style union type - | -4 | x: "int" | str # TCH006 -5 | x: ("int" | str) | "bool" # TCH006 - | ^^^^^ TCH006 - | - -TCH006_2.py:5:20: TCH006 Invalid string member in `X | Y`-style union type - | -4 | x: "int" | str # TCH006 -5 | x: ("int" | str) | "bool" # TCH006 - | ^^^^^^ TCH006 - | - -TCH006_2.py:12:20: TCH006 Invalid string member in `X | Y`-style union type - | -12 | z: list[str, str | "int"] = [] # TCH006 - | ^^^^^ TCH006 -13 | -14 | type A = Value["int" | str] # OK - | - -TCH006_2.py:16:30: TCH006 Invalid string member in `X | Y`-style union type - | -14 | type A = Value["int" | str] # OK -15 | -16 | OldS = TypeVar('OldS', int | 'str', str) # TCH006 - | ^^^^^ TCH006 - | - - diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_1.py.snap b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_1.py.snap new file mode 100644 index 00000000000000..bf78da6c607ae6 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_1.py.snap @@ -0,0 +1,12 @@ +--- +source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs +--- +TCH010_1.py:18:30: TCH010 Invalid string member in `X | Y`-style union type + | +16 | type A = Value["int" | str] # OK +17 | +18 | OldS = TypeVar('OldS', int | 'str', str) # TCH010 + | ^^^^^ TCH010 + | + + diff --git a/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_2.py.snap b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_2.py.snap new file mode 100644 index 00000000000000..f03037ad829336 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_type_checking/snapshots/ruff_linter__rules__flake8_type_checking__tests__runtime-string-union_TCH010_2.py.snap @@ -0,0 +1,41 @@ +--- +source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs +--- +TCH010_2.py:4:4: TCH010 Invalid string member in `X | Y`-style union type + | +4 | x: "int" | str # TCH010 + | ^^^^^ TCH010 +5 | x: ("int" | str) | "bool" # TCH010 + | + +TCH010_2.py:5:5: TCH010 Invalid string member in `X | Y`-style union type + | +4 | x: "int" | str # TCH010 +5 | x: ("int" | str) | "bool" # TCH010 + | ^^^^^ TCH010 + | + +TCH010_2.py:5:20: TCH010 Invalid string member in `X | Y`-style union type + | +4 | x: "int" | str # TCH010 +5 | x: ("int" | str) | "bool" # TCH010 + | ^^^^^^ TCH010 + | + +TCH010_2.py:12:20: TCH010 Invalid string member in `X | Y`-style union type + | +12 | z: list[str, str | "int"] = [] # TCH010 + | ^^^^^ TCH010 +13 | +14 | type A = Value["int" | str] # OK + | + +TCH010_2.py:16:30: TCH010 Invalid string member in `X | Y`-style union type + | +14 | type A = Value["int" | str] # OK +15 | +16 | OldS = TypeVar('OldS', int | 'str', str) # TCH010 + | ^^^^^ TCH010 + | + + diff --git a/ruff.schema.json b/ruff.schema.json index 49ee661d7c86bc..2d73566f2db206 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -3687,7 +3687,8 @@ "TCH003", "TCH004", "TCH005", - "TCH006", + "TCH01", + "TCH010", "TD", "TD0", "TD00",