Brian Bruggeman
Brian Bruggeman

Reputation: 5324

Why is `default` a flake8 error in this python class?

import typing
from dataclasses import dataclass


@dataclass
class Parameter:
    default: typing.Any = None

I'm getting a flake8 error on line 7:

    default: typing.Any = None

The error is:

    E704 multiple statements on one line (def)

Is this a bug in flake8's parsing?

Upvotes: 0

Views: 883

Answers (1)

abarnert
abarnert

Reputation: 365717

So, there's good news and bad news.

  • flake8 is just a wrapper around a set of other tools, including pycodestyle.
  • The good news is that, as far as I can tell, this problem is in pycodestyle 2.3.1, and has been fixed in 2.4.0.
  • The bad news is that the current version of flake8, 3.5.0, claims to be incompatible with pycodestyle 2.4.0. (See the flake8 FAQ for why they don't even try to be future-compatible with the wrapped tools.)
  • The good news is that, if you're willing to install off GitLab or GitHub, the current flake8 master supports pycodestyle 2.4.x (and also pyflakes 2.0.x).
  • The bad news is that there isn't yet a post-3.5.0 milestone assigned, which means nobody's claiming the current tip-of-tree is tested and stable and ready for release, and most likely they haven't actually finished testing with 2.4.0, they've just changed the versions because that's what they intend to support.

If I install pycodestyle 2.3.1 and flake8 3.5.0 (and pyflakes 1.6.0) in a clean 3.7 environment, I can reproduce this warning.

If I force pip to install pycodestyle 2.4.0 even though flake8 complains about it, the warning goes away.

If I install flake8 off GitLab, it drags in pycodestyle 2.4.0 (and pyflakes 2.0.0), and again the warning goes away.

This isn't too surprising, given that 2.4.0 claims to add Python 3.7 support (even though its PyPI entry still only lists 3.6), and fixes multiple bugs related to E704, like this one, but I couldn't find the specific relevant change from a quick scan.


Unfortunately, since flake8 hasn't actually been tested with 2.4.0 yet, it's possible that fixing things this way (whether forcibly installing incompatible versions, or installing the bleeding-edge version off GitLab) will break other things.

In which case the only real options are:

  • Do it anyway and cross your fingers.
  • Run pycodestyle manually instead of via flake8. (This means its output won't be file-by-file merged with the other tools, it won't understand the more powerful noqa flags added by flake8, etc.)
  • Wait for flake8 to reach a milestone that officially supports pycodestyle 2.4.0 (or help them do it).
  • Find the fix you need in the pycodestyle repo and backport it to 2.3.1.
  • Disable this warning (per-line, per-file, or globally).

Upvotes: 1

Related Questions