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

[Enhancement]: textwrap.dedent() to prettify a multiline string #328

Open
opk12 opened this issue Feb 8, 2024 · 1 comment
Open

[Enhancement]: textwrap.dedent() to prettify a multiline string #328

opk12 opened this issue Feb 8, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@opk12
Copy link

opk12 commented Feb 8, 2024

Overview

Is it in-scope to suggest wrapping a multiline string literal inside textwrap.dedent() to keep it readable and consistently indented?

Proposal

$ cat a.py
#!/usr/bin/env python3

import textwrap

class C:
    def make_with_dedent(self):
        literal = textwrap.dedent("""\
            This is a multiline
            string literal.
        """)
        return literal

    def make_without_dedent(self):
        literal = """\
This is another multiline
string literal.
        """
        return literal

print(C().make_with_dedent())
print(C().make_without_dedent())

$ ./a.py 
This is a multiline
string literal.

This is another multiline
string literal.
        

A big thanks to alonzodemo at OpenStreetMap for teaching me the trick.

@opk12 opk12 added the enhancement New feature or request label Feb 8, 2024
@dosisod
Copy link
Owner

dosisod commented Feb 9, 2024

Personally I like using code without dedent() since there is less whitespace, and it saves me an extra import.

With that being said, reliably detecting when to emit an error for this will be hard. Since Refurb is built on Mypy, and Mypy stores string nodes in a normalized form, "\n" and """<newline>""" are identical, making it hard to detect when multi-line strings are being used.

I'm not against adding this, but doing so will be difficult for the reason above, and because it may be hard to gauge when using dedent() will improve readability (for example, very long/wide multi-line strings).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants