12

I'm using flake8 in Visual Studio Code, writing some code using Python 3.6 variable annotations. It worked without any problems so far, but I encountered a strange warning.

This works fine:

style: str = """
width: 100%;
...
"""
# Doing sth with `style`

This too:

img_style: str = """
width: 100%;
...
"""
# Doing sth with `img_style`

This however does not, it yields below warning:

iframe_style: str = """
width: 100%;
...
"""
# Doing sth with `iframe_style`

flake8 warning

Well, technically it does work fine; the code runs. But somehow flake8 is not happy with this. The multiline string and the code following is always the same.

When I omit the "f" (i_rame_style), I don't get a warning, too! So I guess for some reason flake8 thinks of a if foo: bar() here!?

What am I missing here? Is this a bug in flake8?

linusg
  • 6,289
  • 4
  • 28
  • 78
  • What if it's just a Visual Studio problem ? Since your code compile and runs as you said it, i don't see any reason why it would be flake's fault. – N.K Apr 11 '18 at 11:53
  • Seems to me that something, most probably VSC, is trying to be smart with a post-process split of common mistakes before sending it to flake. – zwer Apr 11 '18 at 11:57
  • I'm using just the `ms-python.python` extension, and I believe that's where flake8 support is built in. It uses the flake8 command line tool installed via pip after all. – linusg Apr 11 '18 at 11:58
  • 2
    i just tried it with plain flake8 and the error is the same... this might get interesting. – etene Apr 11 '18 at 11:58
  • @etene thanks! That would have been my next shot... As I said, I have no clue what's going wrong, but to me it seems like this is an issue with flake8, which produces these messages. – linusg Apr 11 '18 at 12:00
  • Yeah, `flake8` is wrong here. You can [look up the warning](https://lintlyci.github.io/Flake8Rules/rules/E701.html), and it's explicitly to stop people from writing `if` statements like `if x: do(y)` all on one line. It might be worth submitting an issue on their [gitlab page](https://gitlab.com/pycqa/flake8/issues) – Patrick Haugh Apr 11 '18 at 12:07

1 Answers1

11

Edit: The problem is in pycodestyle (pep8), which is called by flake8. The rest still stands.

Second edit: I've made some more research and the issue is fixed here. The fix hasn't been released yet, though.

Definitely looks like a flake8 bug to me:

flakebug.py:

innocuous: str = ""
ifstarting_string: str = ""
forfalse_positivetoo: str = ""
whilethis_lookslikeabug: str = ""
elsehaha: str = ""

In the shell:

$ # python3.6 -m pycodestyle flakebug.py gives the same results
$ python3.6 -m flake8 flakebug.py 
flakebug.py:2:18: E701 multiple statements on one line (colon)
flakebug.py:3:21: E701 multiple statements on one line (colon)
flakebug.py:4:24: E701 multiple statements on one line (colon)
flakebug.py:5:9: E701 multiple statements on one line (colon)

Looks like every line starting with a control flow statement triggers it. I'd wager it uses a regex like (if|else|while|for).*:.

I'll try to get to the bottom of this and update this answer if I can, meanwhile you could add some # noqa annotations and you'll be set :)

etene
  • 710
  • 4
  • 12
  • 1
    Thank you for testing! I almost forgot about `# noqa`, always trying to make flake super happy :D If this turns out to be a flake bug I'll open a bug report for sure later this day, In case nobody else did until then. You are most likely right with that regex thing... – linusg Apr 11 '18 at 12:12
  • 1
    Actually the bug is in pycodestyle (a.k.a. pep8), which flake8 uses. Watch out for this if you submit a bug ! I'll edit my answer to reflect this. – etene Apr 11 '18 at 12:14
  • And you're welcome ! I use flake8 daily so this is _really_ relevant to my interests. – etene Apr 11 '18 at 12:16
  • 1
    Good catch, finding that link from your second edit! It was merged in June 2017 tho, they could probably hurry up a little. Until then, I'm able to silence the warning. Thanks again, question answered! – linusg Apr 11 '18 at 12:32