Skip to content

Commit

Permalink
Count expression length as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Mar 14, 2024
1 parent d607009 commit 1ef5e95
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def not_warnings_dot_deprecated(
)
def not_a_deprecated_function() -> None: ...

fbaz: str = f"51 character {foo} stringggggggggggggggggggggggggggggggg" # Error: PYI053
fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,8 @@ pub(crate) fn string_or_bytes_too_long(checker: &mut Checker, string: StringLike
checker.diagnostics.push(diagnostic);
}

/// Count the number of characters in an f-string. This accounts for implicitly concatenated
/// f-strings as well. For example, the following f-string has 12 characters as highlighted
/// by the caret symbols:
///
/// ```python
/// x = "one" f"one{expr}one" f"one" f"{expr}"
/// # ^^^ ^^^ ^^^ ^^^
/// ````
/// Count the number of visible characters in an f-string. This accounts for
/// implicitly concatenated f-strings as well.
fn count_f_string_chars(f_string: &ast::ExprFString) -> usize {
f_string
.value
Expand All @@ -90,10 +84,9 @@ fn count_f_string_chars(f_string: &ast::ExprFString) -> usize {
ast::FStringPart::FString(f_string) => f_string
.elements
.iter()
.map(|element| {
element
.as_literal()
.map_or(0, |literal| literal.chars().count())
.map(|element| match element {
ast::FStringElement::Literal(string) => string.chars().count(),
ast::FStringElement::Expression(expr) => expr.range().len().to_usize(),
})
.sum(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ PYI053.pyi:68:13: PYI053 [*] String and bytes literals longer than 50 characters
|
66 | def not_a_deprecated_function() -> None: ...
67 |
68 | fbaz: str = f"51 character {foo} stringggggggggggggggggggggggggggggggg" # Error: PYI053
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053
68 | fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053
|
= help: Replace with `...`

Safe fix
65 65 | )
66 66 | def not_a_deprecated_function() -> None: ...
67 67 |
68 |-fbaz: str = f"51 character {foo} stringggggggggggggggggggggggggggggggg" # Error: PYI053
68 |-fbaz: str = f"51 character {foo} stringgggggggggggggggggggggggggg" # Error: PYI053
68 |+fbaz: str = ... # Error: PYI053

0 comments on commit 1ef5e95

Please sign in to comment.