Skip to content

Invalid fix for PYI053 for f-strings #10307

@dhruvmanila

Description

@dhruvmanila

Given:

def foo(
    arg2: str = f'51 character {x} stringgggggggggggggggggggggggggggggggggggggggggggg',
) -> None: ...

Here, the number of characters after the replacement field is > 50 which is what we flag and fix:

src/PYI053.pyi:3:35: PYI053 [*] String and bytes literals longer than 50 characters are not permitted
  |
1 | def foo(
2 |     arg1: str = '51 character stringggggggggggggggggggggggggggggggg',
3 |     arg2: str = f'51 character {x} stringgggggggggggggggggggggggggggggggggggggggggggg',
  |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI053
4 | ) -> None: ...
  |
  = help: Replace with `...`

ℹ Safe fix
1 1 | def foo(
2 2 |     arg1: str = '51 character stringggggggggggggggggggggggggggggggg',
3   |-    arg2: str = f'51 character {x} stringgggggggggggggggggggggggggggggggggggggggggggg',
  3 |+    arg2: str = f'51 character {x}...',
4 4 | ) -> None: ...

Found 1 error.
[*] 1 fixable with the --fix option.

This is not ideal and it raises an interesting question - how to count the characters for an f-string containing replacement fields?

  1. f"one{expr}one" (6 total or 3 and 3)
  2. "one" f"one{expr}one" (9 total or 3, 3, 3)
  3. f"one{expr}one" f"one{expr}one" (12 total or 3, 3, 3, 3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationslinterRelated to the linter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions