Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flake 8: "multiple statements on one line (colon)" only for variable name starting with "if"

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?

like image 305
linusg Avatar asked Apr 11 '18 11:04

linusg


1 Answers

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 :)

like image 183
etene Avatar answered Oct 20 '22 08:10

etene