Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NamedTuple defined with Python 3.6 type hints is reported with E701 #635

Closed
futursolo opened this issue Mar 26, 2017 · 11 comments
Closed

NamedTuple defined with Python 3.6 type hints is reported with E701 #635

futursolo opened this issue Mar 26, 2017 · 11 comments

Comments

@futursolo
Copy link

NamedTuple defined with Python 3.6 type hints is reported with E701 when the name of the members in the tuple starts with indent keywords.

Examples:

from typing import NamedTuple, Callable


class MyNamedTuple(NamedTuple):
    format_str: str
    if_result: bool
    while_matched: Callable[..., str]

When testing the code above against pycodestyle --show-source test.py, it shows:

test.py:5:15: E701 multiple statements on one line (colon)
    format_str: str
              ^
test.py:6:14: E701 multiple statements on one line (colon)
    if_result: bool
             ^
test.py:7:18: E701 multiple statements on one line (colon)
    while_matched: Callable[..., str]
                 ^
@sigmavirus24
Copy link
Member

Can you please provide the version of pycodestyle and the version of python it's installed on?

@futursolo
Copy link
Author

I am running pycodestyle 2.3.1 on Python 3.6.0.

@eddie-dunn
Copy link

Using the latest version of pep8 available from PyPI.

test.py:

"""Docstring"""
from typing import NamedTuple


class Foo(NamedTuple):  # pylint: disable=too-few-public-methods
    """Docstring."""
    var1: bool
    var2: str = ''
$ pep8 test.py
test.py:7:9: E701 multiple statements on one line (colon)
test.py:8:9: E701 multiple statements on one line (colon)
$ pep8 --version
1.7.0
$ python --version
Python 3.6.1

I get that you might not always be able to keep up with changes in Python, but this is super annoying since pep8 apparently doesn't support # noqa for E701 (see #180) and our CI system auto rejects code submissions if they are not PEP8 compliant.

By comparison, neither flake8 nor pylint have any problems with the code example above.

@sigmavirus24
Copy link
Member

pep8 has been renamed to pycodestyle. Please try the latest release of pycodestyle instead of using pep8. That's why flake8 works (it uses pycodestyle).

@eddie-dunn
Copy link

eddie-dunn commented Jun 9, 2017

Ah, fair enough, my bad. I mistakenly thought the old pep8 executable was the same version as pycodestyle, which is clearly not the case.

With that said, pycodestyle 2.3.1 works fine for my example, but not for the code @futursolo reported. Merging the two examples:

test.py

"""Docstring"""
from typing import NamedTuple


class Foo(NamedTuple):
    """Docstring."""
    format_str: str
    my_var: str
    if_result: bool
    other_var: bool
$ pycodestyle test.py
test.py:7:15: E701 multiple statements on one line (colon)
test.py:9:14: E701 multiple statements on one line (colon)
$ pycodestyle --version
2.3.1
$ python --version
Python 3.6.1

I.e., format_str: str and if_result: bool are wrong but my_var: str and other_var: bool aren't? Something weird is going on.

EDIT: Ah, I get it; this is about variable names beginning with keywords that would normally signify an indented block. If the variable names begin with if, else, for, while, try, except etc, we get the E701 error.

eddie-dunn pushed a commit to eddie-dunn/pycodestyle that referenced this issue Jun 9, 2017
If a Python 3 class variable begins with an indent keyword, i.e.,

class Class:
    with_foo: int

pycodestyle will erroneously output the error 'E701 multiple statements
on one line'. This patch tightens the check so that even variables
beginning with indent keywords are allowed.

Resolves PyCQA#635
@eddie-dunn
Copy link

I would say the problem is in the regex on this line.

Changing it to

STARTSWITH_INDENT_STATEMENT_REGEX = re.compile(
    r'^\s*({0})'.format('|'.join((s + ' ').replace(' ', r'\s+') for s in (
        'def', 'async def',
        'for', 'async for',
        'if', 'elif', 'else:',
        'try:', 'except', 'finally:',
        'with', 'async with',
        'class',
        'while',
    )))
)

will still make it pass the existing tests.

@eddie-dunn
Copy link

Solved by #640

@gothraven
Copy link

gothraven commented Jul 23, 2018

I'm having the same problem with

$> python --version
Python 3.7.0

$> flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.7.0 on Darwin
"""Docstring"""
from typing import Dict


class Foo(NamedTuple):
    """Docstring."""
    forty_eight_hours_ago: str
    format_params: Dict[str, str]
$> flake8 test.py
test.py:6:9: E701 multiple statements on one line (colon)
test.py:7:9: E701 multiple statements on one line (colon)

@LefterisJP
Copy link

I am also seeing this with Python 3.6.3, flake8 3.5.0 and pycodestyle being 2.3.1
https://travis-ci.org/raiden-network/raiden/jobs/414275990

LefterisJP added a commit to LefterisJP/raiden that referenced this issue Aug 9, 2018
It's to avoid an false positive flake8 throws with pycodestyle 2.3.1 in
travis: https://travis-ci.org/raiden-network/raiden/jobs/414275990

There is an issue in the upstream repo of pycodestyle for this:
PyCQA/pycodestyle#635

[ci integration]
@futursolo
Copy link
Author

@LefterisJP

This issue is resolved by a later version of pycodestyle. But flake8 pinned the version of pycodestyle to this bugged version.
Wait for the next version of flake8 and it should be fixed.

LefterisJP added a commit to LefterisJP/raiden that referenced this issue Aug 9, 2018
We get a false positive flake8 throws with pycodestyle 2.3.1 in
travis: https://travis-ci.org/raiden-network/raiden/jobs/414275990

There is an issue in the upstream repo of pycodestyle for this:
PyCQA/pycodestyle#635

[ci integration]
@LefterisJP
Copy link

Yeah I figured it out in the meantime. Thanks for the hint @futursolo. Added E701 to the ignored warnings for now for our project and waiting till flake8 makes another release.

LefterisJP added a commit to raiden-network/raiden that referenced this issue Aug 10, 2018
We get a false positive flake8 throws with pycodestyle 2.3.1 in
travis: https://travis-ci.org/raiden-network/raiden/jobs/414275990

There is an issue in the upstream repo of pycodestyle for this:
PyCQA/pycodestyle#635

[ci integration]
calin-iorgulescu added a commit to calin-iorgulescu/hangups that referenced this issue Nov 22, 2018
calin-iorgulescu added a commit to calin-iorgulescu/hangups that referenced this issue Nov 22, 2018
calin-iorgulescu added a commit to calin-iorgulescu/hangups that referenced this issue Nov 22, 2018
LefterisJP added a commit to LefterisJP/raiden that referenced this issue Feb 7, 2019
With a new version of flake8 we can now stop ignoring E701 that was
introduced to this bug: PyCQA/pycodestyle#635
LefterisJP added a commit to raiden-network/raiden that referenced this issue Feb 7, 2019
With a new version of flake8 we can now stop ignoring E701 that was
introduced to this bug: PyCQA/pycodestyle#635
hackaugusto pushed a commit to hackaugusto/raiden that referenced this issue Feb 21, 2019
With a new version of flake8 we can now stop ignoring E701 that was
introduced to this bug: PyCQA/pycodestyle#635
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants