From 9baab8672a0fe5e06538f9e6aaf0fc5ca437f5c9 Mon Sep 17 00:00:00 2001 From: Dylan <53534755+dylwil3@users.noreply.github.com> Date: Tue, 20 Aug 2024 04:53:22 -0500 Subject: [PATCH] [`flake8-pyi`] Skip type annotations in `string-or-bytes-too-long` (`PYI053`) (#13002) --- .../resources/test/fixtures/flake8_pyi/PYI053.pyi | 3 +++ .../src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs | 4 ++++ ..._linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi b/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi index a711b7e9156d9..b25a02db20c7b 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi +++ b/crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI053.pyi @@ -66,3 +66,6 @@ def not_warnings_dot_deprecated( def not_a_deprecated_function() -> None: ... fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053 + +# see https://github.com/astral-sh/ruff/issues/12995 +def foo(bar: typing.Literal["a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]):... \ No newline at end of file diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs index 8f50173f25583..1d7a85b2470e2 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs @@ -59,6 +59,10 @@ pub(crate) fn string_or_bytes_too_long(checker: &mut Checker, string: StringLike return; } + if semantic.in_annotation() { + return; + } + let length = match string { StringLike::String(ast::ExprStringLiteral { value, .. }) => value.chars().count(), StringLike::Bytes(ast::ExprBytesLiteral { value, .. }) => value.len(), diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap index 19ca04f611b51..501c5a310067b 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI053_PYI053.pyi.snap @@ -152,6 +152,8 @@ PYI053.pyi:68:13: PYI053 [*] String and bytes literals longer than 50 characters 67 | 68 | fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053 +69 | +70 | # see https://github.com/astral-sh/ruff/issues/12995 | = help: Replace with `...` @@ -161,3 +163,6 @@ PYI053.pyi:68:13: PYI053 [*] String and bytes literals longer than 50 characters 67 67 | 68 |-fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053 68 |+fbaz: str = ... # Error: PYI053 +69 69 | +70 70 | # see https://github.com/astral-sh/ruff/issues/12995 +71 71 | def foo(bar: typing.Literal["a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]):...