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

E231: Check misses trailing comma in function or class calls that do not end with whitespace #980

Open
lfiedler opened this issue Feb 24, 2021 · 5 comments

Comments

@lfiedler
Copy link

Trailing commas in function calls, e.g. print(1,2,) that do not end with a white space are not flagged by pycodestyle.
From reading the corresponding PEP8 I would expect that pycodestyle would flag this.

Here's a small example:

def f(a, b):
    return a+b


class A:
    def __init__(self, a):
        self.a = a


if __name__ == "__main__":
    A(f(1, 2,),)
    print(f(1, 2,))
    print(1, 2,)

Although this is valid python code I'd expect pycodestyle to flag all of the three last lines, but it doesn't.
Is this behaviour expected?

Environment:

  • Python 3.8.5
  • conda 4.9.2
  • pycodestyle 2.6.0
@asottile
Copy link
Member

pycodestyle is an old tool and pep8 changes a lot so they're not always in sync (and even when they could be, sometimes pep8 has flip-flopped on an issue or is directly contradictory with itself so it's impossible to follow to a T). this particular part of pep8 is relatively new (2017) and so it's not surprising that pycodestyle has nothing for it.

additionally it's tricky to get correct, in your example print(1, 2,) it's hard to know whether this is a tuple or function call (since pycodestyle does not access the parse tree for performance reasons).

you might be interested in some tools which are built around handling these such as add-trailing-comma or black -- both of which are probably better suited to handle this than pycodestyle is

@lfiedler
Copy link
Author

Thanks for the reply and also for the hints towards the plugin. I'll have a look.

Ironically, I was stumbling over this while using flake8, which, I am assuming, uses pycodestyle, in conjunction with black (which produces these kind of lines under certain circumstances, at least for the version I am using. But that does not belong here.).

@lfiedler
Copy link
Author

I am not sure I understand your remark about print(1, 2,): Even if this was a tuple, i.e. (1, 2,) this shouldn't be allowed.

@asottile
Copy link
Member

consider print(1,)

@lfiedler
Copy link
Author

I see, thanks.

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

2 participants