Picky Parentheses is a flake8 plugin that
nitpicks all things parentheses, brackets and braces.
The plugin has two components:
A checker that warns about redundant parentheses (with some exceptions).
A checker for parentheses, brackets, and braces alignment.
This component is very opinionated but has its own error codes so you can
easily disable it.
This is a plugin for flake8. It supports Python 3.7 - 3.14.
Refer to the documentation of flake8 on how to run it on your code:
https://flake8.pycqa.org/en/latest/
Two common options are to either install the plugin and then run flake8:
Only operators and comments are allowed after a closing bracket on a new line.
# BAD
a = func(
1, 2, 3, 4, 5
) + 6
# GOOD
a = (
func(
1, 2, 3, 4, 5
)
+ 6
)
Details and Exceptions
The redundant parentheses checker uses Python's tokenize and ast module to
try to remove each pair of parentheses and see if the code still compiles and
yields the same AST (i.e., is semantically equivalent).
If it does, a flake (lint error) is reported. However, there are some notable
exceptions to this rule:
Parentheses for tuple literals.
Python 3.14+ allows to omit parentheses for multi-error except/except*-expressions without as-clause.
This exception means that while parentheses in such cases is redundant, they're still allowed by this linter.
A single pair or parentheses in expressions to highlight operator
precedence.
Even if these parentheses are redundant, they help to divide parts of
expressions and show sequence of actions.
Parts of slices.
Multi-line1) expression, if and for parts in comprehensions.
Multi-line1) keyword arguments or argument defaults.
String concatenation over several lines in lists, tuples, and function arguments.
Exception type 1:
a = ("a",) # GOOD
a = "a", # GOOD
a = ("a") # BAD
a = (("a"),) # BAD
a = (("a",)) # BAD
foo(("a",)) # GOOD
foo("a",) # BAD
Exception type 2:
a = (1 + 2) + 3 # GOOD
a = (1 + 2) % 3 # GOOD
a = 1 and (2 + 3) # GOOD
a = (1 / 2) * 3 # GOOD
a = not (1 + 2) # GOOD
a = (not 1) + 2 # GOOD
a = 1 + (2 if a else 3) # GOOD
a = foo(*(a if b else c)) # GOOD
a = foo(*(a + b)) # GOOD
a = foo(**(a + b)) # GOOD
a = (1 + 2) # BAD
a = 1 + (2) # BAD
a = ((not 1)) + 2 # BAD
a = foo(*(a)) # BAD
a = foo(**(a)) # BAD
Exception type 3:
foo[(1 + 2):10] # GOOD
foo[1:(1 + 2)] # GOOD
foo[1:5:(1 + 1)] # GOOD
foo[:(-bar)] # GOOD
foo[(1):] # BAD
foo[:(1)] # BAD
Exception type 4:
# GOOD
a = (
b for b in c
if (
some_thing == other_thing
or whatever_but_long
)
)
# GOOD
a = [
b for b in c
if (b
in d)
]
# BAD
a = (
b for b in c
if (b in d)
)
# GOOD
a = (
b for b in (c
+ d)
)
# BAD
a = (
b for b in (c + d)
)
# GOOD
a = (
(
1
+ b
)
for b in c
)
# BAD
a = (
(1 + b) for b in c
)
# GOOD
a = {
(
"foo%s"
% b
): (
b
* 2
)
for b in c
}
Exception type 5:
# GOOD
foo(bar=(a
in b))
# BAD
foo(bar=(a in b))
# GOOD
def foo(bar=(a
is b)):
...
# BAD
def foo(bar=(a is b)):
...
Exception type 6:
# GOOD
[
"a",
(
"b"
"c"
),
"d",
]
# This helps to avoid forgetting a comma at the end of a string spanning
# multiple lines. Compare with:
[
"a",
"b"
"c",
"d",
]
# Was the comma after "b" forgotten or was the string supposed to be "bc"?
# BAD
[
(
"a" "b"
),
]
# This also applies to function calls:
# GOOD
func(
(
"a"
"b"
),
"c",
)
Footnotes:
Multi-line means that either
the expression spans multiple lines, e.g.,
(a
+ b)
or the first part of the expression is on a new line (e.g., if a name is very long), e.g.,
(
veeeeeeeeeeery_looooooong_name
)
but also
(
a
+ b
)
Multi-line expressions do not include
(a + b
)
Additional Notes
This plugin was developed to improve the code quality of Neo4j Python projects.
Works well with
Use together with flake8-picky-parentheses: extensions, drivers, servers and companion tools.