Opened on 10/07/2017 at 11:25:12 PM

Closed on 01/25/2018 at 09:04:12 PM

Last modified on 01/31/2018 at 10:10:10 AM

#5844 closed change (fixed)

[flake8-eyeo] Detect (more) redundant parentheses

Reported by: sebastian Assignee: rhowell
Priority: P3 Milestone:
Module: Automation Keywords:
Cc: kvas, rhowell Blocked By:
Blocking: Platform: Unknown / Cross platform
Ready: yes Confidential: no
Tester: Unknown Verified working: no
Review URL(s):

https://codereview.adblockplus.org/29602819

Description (last modified by sebastian)

Background

Currently, flake8-eyeo (our own plugin for the flake8 Python code checker) already detects parentheses used in statements (A111), which are redundant in Python, e.g:

if (x == y):
    pass

We currently go to quite some length, to consider all legit uses of parentheses in statements (e.g. newlines, sub-expressions and tuples), while we don't address any redundant usage of parentheses outside of if and while statements at all.

In a recent code review one particular error-prone case (which isn't automatically detected yet) was identified. That is that in order to create a tuple with a single element, a trailing comma is required, while otherwise the parentheses indicate a sub-expression:

(1, 2)      # -> tuple
(1 + 2) * 3 # -> int (parentheses semantically required)
(1)         # -> int (parentheses redundant)
(1,)        # -> tuple

However, these are just two examples where redundant usage of parentheses cause inconsistent code or even potential bugs. While on the other hand there are also cases where usage of parentheses, that aren't syntactically or semantically required, facilitate the understanding of the code. So how could we distinguish those two in an as abstract/generic as possible way, rather than explicitly targeting each individual pattern, which isn't a realistic approach (in particular on the parser/tokenizer level)?

In order to eliminate any case in which removal of parentheses would cause invalid code or different behavior, we can just recompile the code for each pair of parentheses with this pair removed, and ignore any case where the compilation fails or the generated AST differs from the original. This is much simpler and more reliable than considering all syntactical requirements and semantical implications ourselves.

What we are left with at this point are occurrences of parentheses, in the token stream, that are neither syntactically nor semantically required. If we map these to their corresponding AST nodes we can fairly easily implement any exception we want to grant for readability.

What to change

Make flake8-eyeo log an A111 error for each usage of parentheses that isn't syntactically or semantically required, with following exceptions (where parentheses can facilitate readability):

  • Tuple literals
  • Nested arithmetic, bitwise or boolean operations
  • Expressions, assigned to variables or used in keyword arguments, if they include comparisons

Attachments (0)

Change History (12)

comment:1 Changed on 10/07/2017 at 11:29:12 PM by sebastian

  • Description modified (diff)

comment:2 Changed on 11/09/2017 at 05:24:13 PM by rhowell

  • Owner set to rhowell

comment:3 Changed on 11/10/2017 at 12:27:34 AM by rhowell

  • Review URL(s) modified (diff)
  • Status changed from new to reviewing

comment:4 Changed on 01/25/2018 at 08:42:41 PM by abpbot

A commit referencing this issue has landed:
Issue 5844 - Remove redundant parentheses

comment:5 Changed on 01/25/2018 at 08:45:46 PM by abpbot

A commit referencing this issue has landed:
Issue 5844 - Remove redundant parentheses in buildtools

comment:6 Changed on 01/25/2018 at 08:52:42 PM by abpbot

A commit referencing this issue has landed:
Issue 5844 - Remove redundant parentheses

comment:7 Changed on 01/25/2018 at 08:55:58 PM by abpbot

A commit referencing this issue has landed:
Issue 5844 - Remove redundant parentheses

comment:8 Changed on 01/25/2018 at 08:58:24 PM by abpbot

A commit referencing this issue has landed:
Issue 5844 - Remove redundant parentheses

comment:9 Changed on 01/25/2018 at 09:03:42 PM by abpbot

A commit referencing this issue has landed:
Issue 5844 - Detect (more) redundant parentheses

comment:10 Changed on 01/25/2018 at 09:04:12 PM by sebastian

  • Resolution set to fixed
  • Status changed from reviewing to closed

comment:11 Changed on 01/25/2018 at 09:04:23 PM by sebastian

  • Component changed from Sitescripts to Automation

comment:12 Changed on 01/31/2018 at 10:10:10 AM by abpbot

A commit referencing this issue has landed:
Issue 5844 - Remove redundant parentheses in buildtools

Add Comment

Modify Ticket

Change Properties
Action
as closed .
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from rhowell.
 
Note: See TracTickets for help on using tickets.