Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline f-strings and prefixing all with f #13675

Open
PatrickJordanNutanix opened this issue Oct 8, 2024 · 3 comments
Open

Multiline f-strings and prefixing all with f #13675

PatrickJordanNutanix opened this issue Oct 8, 2024 · 3 comments
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule

Comments

@PatrickJordanNutanix
Copy link

We have soft rule in our code base that means we should prefix all multiline f-strings with an f even if no interpolation is happening on part of the string. For example, instead of doing this:

long_string = (
  "This is a long string to demonstrate that when it wraps on to the next "
  f"line, all lines should be prefixed with an {f_prefix} to improve "
  "readability and effectively treat them along the same lines as a trailing "
  "comma, even if interpolation only happens on a subset of the lines."
)

We should be doing this:

long_string = (
  f"This is a long string to demonstrate that when it wraps on to the next "
  f"line, all lines should be prefixed with an {f_prefix} to improve "
  f"readability and effectively treat them along the same lines as a trailing "
  f"comma, even if interpolation only happens on a subset of the lines."
)

Is there an existing rule that can catch this linting error? If not, can I request one be added please?

@MichaReiser
Copy link
Member

Does this rule of yours only apply if one of the string literals contain an expression or for all implicit concatenated strings?

E.g. what about

long_string = (
  "This is a long string to demonstrate that when it wraps on to the next "
  "line, all lines should be prefixed with an ... to improve "
  "readability and effectively treat them along the same lines as a trailing "
  "comma, even if interpolation only happens on a subset of the lines."
)

@PatrickJordanNutanix
Copy link
Author

Does this rule of yours only apply if one of the string literals contain an expression or for all implicit concatenated strings?

Yes, only if at some point a variable is being interpolated into it. If it's a multiline string with no interpolation, then we do not prefix with an f.

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Oct 13, 2024
@MichaReiser
Copy link
Member

Thanks. I now understand your use case. We could phrase it in a more general way and say that you want a common prefix for all string literals in implicit concatenated strings. The rule would then also enforce that all strings are raw-strings

a = (
	r"flag"
	"this"
)

# instead write
a = (
	r"flag"
	r"this"
)

# or
a = (
	"flag"
	"this"
)

Considering that this is an opinionated stylistic rule, we should wait for #1774 to avoid enabling the rule by default for everyone using select=ALL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

2 participants