From 4950ca414239d990b1b531e21ddf19f9ccaafd48 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Tue, 26 Mar 2024 00:22:59 +0530 Subject: [PATCH] Ignore `Q000`, `Q001` when string is inside forward ref (#10585) ## Summary This is not the holistic solution but just to fix that issue. fixes: #10546 ## Test Plan Add a regression test for it and check the snapshots. --- .../resources/test/fixtures/flake8_quotes/singles.py | 3 +++ .../src/rules/flake8_quotes/rules/check_string_quotes.rs | 6 ++++++ ...ake8_quotes__tests__require_doubles_over_singles.py.snap | 5 +++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_quotes/singles.py b/crates/ruff_linter/resources/test/fixtures/flake8_quotes/singles.py index 0639e885fcfd7..e9a96a105cb6e 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_quotes/singles.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_quotes/singles.py @@ -2,3 +2,6 @@ this_should_be_linted = u'double quote string' this_should_be_linted = f'double quote string' this_should_be_linted = f'double {"quote"} string' + +# https://github.com/astral-sh/ruff/issues/10546 +x: "Literal['foo', 'bar']" diff --git a/crates/ruff_linter/src/rules/flake8_quotes/rules/check_string_quotes.rs b/crates/ruff_linter/src/rules/flake8_quotes/rules/check_string_quotes.rs index a3850a854a33f..e5eb47a0dea8c 100644 --- a/crates/ruff_linter/src/rules/flake8_quotes/rules/check_string_quotes.rs +++ b/crates/ruff_linter/src/rules/flake8_quotes/rules/check_string_quotes.rs @@ -432,6 +432,12 @@ fn strings(checker: &mut Checker, sequence: &[TextRange]) { /// Generate `flake8-quote` diagnostics from a token stream. pub(crate) fn check_string_quotes(checker: &mut Checker, string_like: StringLike) { + // Ignore if the string is part of a forward reference. For example, + // `x: "Literal['foo', 'bar']"`. + if checker.semantic().in_string_type_definition() { + return; + } + // If the string is part of a f-string, ignore it. if checker .indexer() diff --git a/crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_doubles_over_singles.py.snap b/crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_doubles_over_singles.py.snap index 43d3e754b395d..81b0fdf4da700 100644 --- a/crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_doubles_over_singles.py.snap +++ b/crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_doubles_over_singles.py.snap @@ -33,6 +33,7 @@ singles.py:2:25: Q000 [*] Single quotes found but double quotes preferred 2 |+this_should_be_linted = u"double quote string" 3 3 | this_should_be_linted = f'double quote string' 4 4 | this_should_be_linted = f'double {"quote"} string' +5 5 | singles.py:3:25: Q000 [*] Single quotes found but double quotes preferred | @@ -50,5 +51,5 @@ singles.py:3:25: Q000 [*] Single quotes found but double quotes preferred 3 |-this_should_be_linted = f'double quote string' 3 |+this_should_be_linted = f"double quote string" 4 4 | this_should_be_linted = f'double {"quote"} string' - - +5 5 | +6 6 | # https://github.com/astral-sh/ruff/issues/10546