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.

Lint `f` strings better!

See original GitHub issue

Rule request

Thesis

Currently we just do not allow people to use f strings. But, what if our user just ignores this violation? Now it is possible to do very bad things inside f strings and we do not cover it at all.

I suggest to add TooComplexFormattedStringViolation that will be throw in case of: 0. f'{one_name}' should raise: because we only a single name inside f string

  1. Too complex expression inside f string: like calling a lot of function, making conditions, etc
  2. Too many expressions used in f string
  3. f strings that dont have any values inside like: f'abc'
  4. Things like assert f'a {value}' should raise proper violations (WPS444 in this case), but assert f'{value}' should not

This is just a discussion at the moment.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
AlwxSincommented, Mar 9, 2020

From my experience, one can easily read code like this

f'smth {value}'
f'smth {dict_value["key"]}'
f'smth {list_value[0]}'
f'smth {user.get_full_name()}'
def get_full_name(self):
    return f'{self.first_name} {self.second_name} {self.last_name}'.strip()

Anything more complex is hard to read like

f'{reverse('url-name')}?{"&".join("user="+uid for uid in user_ids)}'

So:

  • value of a variable itself
  • value of a dict key by key lookup
  • value of a list item by index lookup
  • return value of a function or a method without arguments
1reaction
AlwxSincommented, Apr 6, 2020

@ruwaid4 no more than I said before 🤷

Read more comments on GitHub >

github_iconTop Results From Across the Web

consider-using-f-string / C0209 - Pylint
Formatted string literals (f-strings) give a concise, consistent syntax that can replace most use cases for the % formatting operator, str.format() and string....
Read more >
Pylint: How to fix "c0209: formatting a regular string which ...
In this post, you will see 3 different ways to fix this "formatting a regular string which could be a f-string (consider-using-f-string)" error....
Read more >
PyLint vs f-strings - python - Stack Overflow
1. f-string is introduced in Python 3.6, which python version do you run pylint with? · To be 100% honest. I am not...
Read more >
Python 3's f-Strings: An Improved String Formatting Syntax ...
As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less...
Read more >
PEP 498 – Literal String Interpolation
In Python source code, an f-string is a literal string, prefixed with 'f', ... Guido stated [5] that any solution to better string...
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