Multiline string indentation
See original GitHub issueRule request
Thesis
Flake8 has no opinion about multiline strings at all. Everything in this example is correct:
def f():
a = """
text
text
"""
a = """
text
text
"""
a = """text
text
"""
a = """text
text"""
a = """
text
text
"""
return a
And so on.
Let’s choose the only option.
The best option
I vote for the second one:
def f():
something = """
text
text
"""
Because it matches with braces style by PEP-8:
def f():
something = (
text,
text,
)
Reasoning
Consistency and readability. Let’s not allow to break block indentation in such a rough way.
Solution
The motivation behind allowing such awful things is newlines and indentation preserving in multiline strings. However, you should never rely on it because that variable could be moved in constants, function turned into a method, part of the code wrapped into with
statement, and a lot more ways how indentation for statement can be changed. So, always use textwrap.dedent
, textwrap.indent
, and str.strip
to get expected indentation and newlines for a multiline string.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (10 by maintainers)
Top GitHub Comments
I would say these two are different violation. The first one is the best practice, the second one is consistency.
Thanks a lot!
Nope, looks quite unreadable.