简体   繁体   中英

How to properly line-wrap a django statement to satisfy flake8?

This code gets E501 line too long (85 > 79 characters) from flake8:

qobjs &= Q(
    latest_visit__visitstatistics__worst_zscore__gte=worst_zscore_min_filter)

This code gets E251 unexpected spaces around keyword / parameter equals :

qobjs &= Q(
    latest_visit__visitstatistics__worst_zscore__gte=
    worst_zscore_min_filter)

What is the right thing to satisfy flake8?

EDIT : I ended up telling flake8 to ignore with noqa. I thought it didn't work, but I just had to read the docs more carefully.

Assuming you can't choose more sane variable names because these are machine generated, I wouldn't worry about it, and just tell pep8 (or pycodestyle , or whatever flake8 is actually using to check for PEP-8 conformance) to ignore the line:

qobjs &= Q(...)  # noqa

(Update: rather than ignore the line altogether, # noqa: E501 would let you ignore the line length, but still check for other problems.)

If you are still using the default max line width of 79, consider using something longer. PEP-8 really only requires that width for code in the standard library, and it explicitly states that teams may agree on a longer width.


The error you are getting is because the newline after = is counted as whitespace, as if you had typed Q(late...gte= worse...filter) . You could use explicit line continuation:

qobjs &= Q(
    latest_visit__visitstatistics__worst_zscore__gte=\
    worst_zscore_min_filter)

or use shorter temporary names:

x = 'latest_visit__visitstatistics__worst_zscore__gte'
y = worst_zscore_min_filter
qobjs &= Q(**{x: y})

but my preference would be to just stop trying to appease flake8 on code that isn't supposed to be human-readable in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM