mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 06:41:23 +00:00
Ignore builtins when detecting missing f-strings (#9849)
## Summary Reported on Discord: if the name maps to a builtin, it's not bound locally, so is very unlikely to be intended as an f-string expression.
This commit is contained in:
parent
df7fb95cbc
commit
a662c2447c
3 changed files with 20 additions and 13 deletions
|
@ -34,7 +34,7 @@ def tripled_quoted():
|
||||||
multi_line = a = """b { # comment
|
multi_line = a = """b { # comment
|
||||||
c} d
|
c} d
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def single_quoted_multi_line():
|
def single_quoted_multi_line():
|
||||||
a = 4
|
a = 4
|
||||||
# RUF027
|
# RUF027
|
||||||
|
@ -66,6 +66,7 @@ def negative_cases():
|
||||||
c = """ {b} """
|
c = """ {b} """
|
||||||
d = "bad variable: {invalid}"
|
d = "bad variable: {invalid}"
|
||||||
e = "incorrect syntax: {}"
|
e = "incorrect syntax: {}"
|
||||||
|
f = "uses a builtin: {max}"
|
||||||
json = "{ positive: false }"
|
json = "{ positive: false }"
|
||||||
json2 = "{ 'positive': false }"
|
json2 = "{ 'positive': false }"
|
||||||
json3 = "{ 'positive': 'false' }"
|
json3 = "{ 'positive': 'false' }"
|
||||||
|
|
|
@ -124,7 +124,13 @@ fn should_be_fstring(
|
||||||
.filter_map(|element| element.as_expression())
|
.filter_map(|element| element.as_expression())
|
||||||
{
|
{
|
||||||
if let ast::Expr::Name(ast::ExprName { id, .. }) = element.expression.as_ref() {
|
if let ast::Expr::Name(ast::ExprName { id, .. }) = element.expression.as_ref() {
|
||||||
if kw_idents.contains(id.as_str()) || semantic.lookup_symbol(id).is_none() {
|
if kw_idents.contains(id.as_str()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if semantic
|
||||||
|
.lookup_symbol(id)
|
||||||
|
.map_or(true, |id| semantic.binding(id).kind.is_builtin())
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
has_name = true;
|
has_name = true;
|
||||||
|
|
|
@ -175,7 +175,7 @@ RUF027.py:34:22: RUF027 [*] Possible f-string without an `f` prefix
|
||||||
35 | | c} d
|
35 | | c} d
|
||||||
36 | | """
|
36 | | """
|
||||||
| |_______^ RUF027
|
| |_______^ RUF027
|
||||||
37 |
|
37 |
|
||||||
38 | def single_quoted_multi_line():
|
38 | def single_quoted_multi_line():
|
||||||
|
|
|
|
||||||
= help: Add `f` prefix
|
= help: Add `f` prefix
|
||||||
|
@ -188,7 +188,7 @@ RUF027.py:34:22: RUF027 [*] Possible f-string without an `f` prefix
|
||||||
34 |+ multi_line = a = f"""b { # comment
|
34 |+ multi_line = a = f"""b { # comment
|
||||||
35 35 | c} d
|
35 35 | c} d
|
||||||
36 36 | """
|
36 36 | """
|
||||||
37 37 |
|
37 37 |
|
||||||
|
|
||||||
RUF027.py:41:9: RUF027 [*] Possible f-string without an `f` prefix
|
RUF027.py:41:9: RUF027 [*] Possible f-string without an `f` prefix
|
||||||
|
|
|
|
||||||
|
@ -276,20 +276,20 @@ RUF027.py:52:9: RUF027 [*] Possible f-string without an `f` prefix
|
||||||
54 54 | def alternative_formatter(src, **kwargs):
|
54 54 | def alternative_formatter(src, **kwargs):
|
||||||
55 55 | src.format(**kwargs)
|
55 55 | src.format(**kwargs)
|
||||||
|
|
||||||
RUF027.py:85:7: RUF027 [*] Possible f-string without an `f` prefix
|
RUF027.py:86:7: RUF027 [*] Possible f-string without an `f` prefix
|
||||||
|
|
|
|
||||||
83 | "always ignore this: {a}"
|
84 | "always ignore this: {a}"
|
||||||
84 |
|
85 |
|
||||||
85 | print("but don't ignore this: {val}") # RUF027
|
86 | print("but don't ignore this: {val}") # RUF027
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF027
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF027
|
||||||
|
|
|
|
||||||
= help: Add `f` prefix
|
= help: Add `f` prefix
|
||||||
|
|
||||||
ℹ Unsafe fix
|
ℹ Unsafe fix
|
||||||
82 82 |
|
83 83 |
|
||||||
83 83 | "always ignore this: {a}"
|
84 84 | "always ignore this: {a}"
|
||||||
84 84 |
|
85 85 |
|
||||||
85 |-print("but don't ignore this: {val}") # RUF027
|
86 |-print("but don't ignore this: {val}") # RUF027
|
||||||
85 |+print(f"but don't ignore this: {val}") # RUF027
|
86 |+print(f"but don't ignore this: {val}") # RUF027
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue