[Python-ideas] PEP 8 and long string literals

David Mertz mertz at gnosis.cx
Mon Feb 25 14:20:27 EST 2019


I find the main pain point of line width limits to be string literals that
call out to some *other* code-like thing.  Long URLs, user messages, and
long SQL are three common examples.

It's actually less an issue with SQL since that is itself more readable
across multiple lines, plus SQL itself doesn't care about newlines
embedded.  So just using triple-quoted strings works best there.  However,
sometimes it's just a quick one-liner SQL I want to include, but that can
easily grow longer than 80 characters (or really even 50 chars if the SQL
is being assigned to something inside some nested logic, or being an
argument to a function call).

URLs can get long, and are whitespace sensitive.  I wrote something like
this just today:

    url = (f'{URL_BASE}'
           f'&arg1={row["column_name_1"]}'
           f'&something_else={1+row["that value"]}'
           f'&more_args={USER_ID}'
           f'&some_other_stuff={translate[row["foo"]]}'
           f'&yep_this={row["foo"]}'
           f'&arg17=123456')

I don't think that's terrible, and it's a lot nicer to be able to visually
separate what are really arguments to a REST call.  But it's definitely
super-easy to mess up this sort of thing, including by letting a little
whitespace sneak in where I don't want it.

But if the URL wasn't quite so convoluted (this example is pretty directly
taken from a real thing today, just a few names munged to protect the
client :-)), I might want it on one line.  And I'd probably want my linters
to stop nagging me about the fact I do that.

However, what I care about more than that is my editor.  It would be really
nice if my editor provided something like "vertical folding" for things
like this. I do not know of any editors that do that, but it would be easy
to imagine.  E.g. I might have an editor display:

    url = f'{URL_BASE}&arg1={row["column_*(..93 chars..)*rg17=123456'

Does anyone know of editors that do that sort of thing? Obviously, you'd
need some way to fold/unfold the hidden text.  But for many purposes, the
first and last few characters probably convey the purpose of the line.

On Mon, Feb 25, 2019 at 4:53 AM Jonathan Fine <jfine2358 at gmail.com> wrote:

> I've started this thread, because I think long string literals are
> somewhat special, and may have an easy resolution.
>
> According to PEP 8 a good reason to ignore the line-length (or any
> other) guideline is that "applying the guideline would make the code
> less readable, even for someone who is used to reading code that
> follows this PEP."
>
> PEP 8 also says "Limit all lines to a maximum of 79 characters."
>
> So perhaps it is enough, for long string literals, to add a note to
> PEP 8: When sensible to do so, lines containing a long string literal
> can exceed this length.
>
> It might also help to add to PEP 8 some Programming Recommendations
> for Long String Literals. (PEP 8 already has recommendations for
> Function and Variable Annotations.)
>
> Finally, tools such as pep8 and pylint would need some changes, to
> treat differently lines that contain a long string constant.
>
> --
> Jonathan
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190225/263852f8/attachment-0001.html>


More information about the Python-ideas mailing list