question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forbid tuples without parenthesis

See original GitHub issue

Rule request

Thesis

# bad
a = 1, 2

# good
a = (1, 2)

Reasoning

It shocked me to learn that in Python tuples are defined by commas and not parenthesis. This rule request will find a very likely bug that is a = 1,, a one element tuple. Also it improves readability.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sobolevncommented, Jan 10, 2022

The AST is identical for these two cases:

>>> import ast
>>> code1 = 'a = (1, 2)'
>>> code2 = 'a = 1, 2'
>>> for node in ast.walk(ast.parse(code1)):
...    print(ast.dump(node))
... 
Module(body=[Assign(targets=[Name(id='a', ctx=Store())], value=Tuple(elts=[Constant(value=1), Constant(value=2)], ctx=Load()))], type_ignores=[])
Assign(targets=[Name(id='a', ctx=Store())], value=Tuple(elts=[Constant(value=1), Constant(value=2)], ctx=Load()))
Name(id='a', ctx=Store())
Tuple(elts=[Constant(value=1), Constant(value=2)], ctx=Load())
Store()
Constant(value=1)
Constant(value=2)
Load()

>>> for node in ast.walk(ast.parse(code2)):
...    print(ast.dump(node))
... 
Module(body=[Assign(targets=[Name(id='a', ctx=Store())], value=Tuple(elts=[Constant(value=1), Constant(value=2)], ctx=Load()))], type_ignores=[])
Assign(targets=[Name(id='a', ctx=Store())], value=Tuple(elts=[Constant(value=1), Constant(value=2)], ctx=Load()))
Name(id='a', ctx=Store())
Tuple(elts=[Constant(value=1), Constant(value=2)], ctx=Load())
Store()
Constant(value=1)
Constant(value=2)
Load()

So you either need:

  • CST (unstable)
  • Token-based solution (really hard to do)
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - When are parentheses required around a tuple?
The combining of expressions to create a tuple using the comma token is termed an expression_list . The rules of operator precedence do...
Read more >
How to Print a Tuple Without Parentheses in Python? - Finxter
You can print a tuple without parentheses by combining the string.join() method on the separator string ', ' with a generator expression to...
Read more >
Avoid accidental tuples best practice | CodeReview.doctor
According to the documentation: it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the...
Read more >
Print a tuple without parentheses in Python | bobbyhadz
Use the str.join() method to print a tuple without parentheses, e.g. result = ','.join(my_tuple) . The str.join ...
Read more >
Tuples in Python
Visually, tuples are defined with parentheses () instead of square brackets [] like lists. Functionally tuples, unlike lists, are immutable — ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found