mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
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.
This commit is contained in:
parent
25d93053da
commit
e0a6034cbb
8 changed files with 563 additions and 1 deletions
|
@ -937,6 +937,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
|
|||
(Ruff, "024") => (RuleGroup::Preview, rules::ruff::rules::MutableFromkeysValue),
|
||||
(Ruff, "025") => (RuleGroup::Preview, rules::ruff::rules::UnnecessaryDictComprehensionForIterable),
|
||||
(Ruff, "026") => (RuleGroup::Preview, rules::ruff::rules::DefaultFactoryKwarg),
|
||||
(Ruff, "027") => (RuleGroup::Preview, rules::ruff::rules::MissingFStringSyntax),
|
||||
(Ruff, "100") => (RuleGroup::Stable, rules::ruff::rules::UnusedNOQA),
|
||||
(Ruff, "200") => (RuleGroup::Stable, rules::ruff::rules::InvalidPyprojectToml),
|
||||
#[cfg(feature = "test-rules")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue