ruff
e0a6034c - Implement RUF027: `Missing F-String Syntax` lint (#9728)

Commit
1 year ago
Implement RUF027: `Missing F-String Syntax` lint (#9728) <!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary Fixes #8151 This PR implements a new rule, `RUF027`. ## What it does Checks for strings that contain f-string syntax but are not f-strings. ### Why is this bad? An f-string missing an `f` at the beginning won't format anything, and instead treat the interpolation syntax as literal. ### Example ```python name = "Sarah" dayofweek = "Tuesday" msg = "Hello {name}! It is {dayofweek} today!" ``` It should instead be: ```python name = "Sarah" dayofweek = "Tuesday" msg = f"Hello {name}! It is {dayofweek} today!" ``` ## Heuristics Since there are many possible string literals which contain syntax similar to f-strings yet are not intended to be, this lint will disqualify any literal that satisfies any of the following conditions: 1. The string literal is a standalone expression. For example, a docstring. 2. The literal is part of a function call with keyword arguments that match at least one variable (for example: `format("Message: {value}", value = "Hello World")`) 3. The literal (or a parent expression of the literal) has a direct method call on it (for example: `"{value}".format(...)`) 4. The string has no `{...}` expression sections, or uses invalid f-string syntax. 5. The string references variables that are not in scope, or it doesn't capture variables at all. 6. Any format specifiers in the potential f-string are invalid. ## Test Plan I created a new test file, `RUF027.py`, which is both an example of what the lint should catch and a way to test edge cases that may trigger false positives.
Author
Parents
Loading