question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[New Rule] Replace f-string without any text with using value directly instead

See original GitHub issue

Explanation

Analyze f-strings. If f-string has no text and only a passed value, then use that value directly.

Example 1

# Bad

foo = "bar"
my_text = f"{foo}"
# Good

foo = "bar"
my_text = foo

Example 2

# Bad

def my_func(foo: str):
    my_text = f"{foo}"
# Good
def my_func(foo: str):
    my_text = foo

Example 3

# Bad

my_number = 123
my_text = f"{foo}"
# Good

my_number = 123
my_text = str(foo)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinThomacommented, Feb 10, 2022

Aparently f-strings are ast.JoinedStr and the parameters are automatically parsed. Variables are ast.FormattedValue. So we can recognize when we have an f-string.

1reaction
AIGeneratedUsernamecommented, Dec 20, 2021

Maybe I misunderstand. Why would simplify need to perform type checking to know that this pattern is incorrect?

You are right. Probably no type checking is required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Search and replacing strings without using Regex but loops ...
I found a way to do string search and replace without using Regex but when I want to replace more than one instance...
Read more >
Using Excel REPLACE and SUBSTITUTE functions - Ablebits
An Excel REPLACE formula always returns a text string, not number. In the screenshot above, notice the left alignment of the returned text...
Read more >
SUBSTITUTE function - Microsoft Support
Use SUBSTITUTE when you want to replace specific text in a text string; use REPLACE when you want to replace any text that...
Read more >
How to Replace a String in Python
In Python, the .replace() method and the re.sub() function are often used to clean up text by removing strings or substrings or replacing...
Read more >
SUBSTITUTE - Google Docs Editors Help
Replaces existing text with new text in a string. ... If a number is desired, try using the VALUE function in conjunction with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found