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

(🎁) Remove redundant parenthesis in getitem/indexing operator #3679

Open
KotlinIsland opened this issue May 4, 2023 · 4 comments · May be fixed by #3680
Open

(🎁) Remove redundant parenthesis in getitem/indexing operator #3679

KotlinIsland opened this issue May 4, 2023 · 4 comments · May be fixed by #3680
Labels
T: enhancement New feature or request

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented May 4, 2023

Given:

[][(1)]
[][(1,)]
[][(1,),]
[][(1, 2)]
[][(1, 2),]

😢 Current behavior

No modifications

😲 Desired behavior

[][1]
[][1,]
[][(1,),]
[][1, 2]
[][(1, 2),]
@KotlinIsland KotlinIsland added the T: enhancement New feature or request label May 4, 2023
@KotlinIsland KotlinIsland changed the title (🎁) Remove redundant parenthesis and/or comma in getitem/indexing operator (🎁) Remove redundant parenthesis in getitem/indexing operator May 4, 2023
@JelleZijlstra
Copy link
Collaborator

We can't remove the comma; that would be a change in the AST and would break code.

>>> [1][0]
1
>>> [1][0,]
<stdin>:1: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple

We could remove the parentheses, though.

@dlamblin
Copy link

dlamblin commented May 9, 2023

AFAIK (1) != (1,) … because the left hand side is an expression evaluating 1 and the right hand side is a tuple containing one. So (1)==1 but (1,)!=1.

Python 3.10.6>>> 1==(1)
True
>>> 1==(1,)
False
>>> (1)==(1,)
False
>>> type((1,))
<class 'tuple'>
>>> type((1))
<class 'int'>
>>>

@KotlinIsland
Copy link
Contributor Author

AFAIK (1) != (1,)

Okay, but how does that relate to this issue?

@Leengit
Copy link

Leengit commented May 22, 2023

Uniformity among equivalent expressions like the following would also be appreciated.

(a, b) = (1, 2)
(a, b) = 1, 2
a, b = (1, 2)
a, b = 1, 2

black could turn them all into the last one. Similarly, black could change return statements like following to be the last one.

return (1, 2)
return 1, 2

Uniformity would be appreciated even when the tuple length is 1. black could change all of these to be one of them.

(a,) = (1,)
(a,) = 1,
a, = (1,)
a, = 1,

Note: I would say that these expressions are for tuples and are not equivalent to a = 1, a = (1), etc., even when the net effect of a = 1, etc. is the same in the end. But black could take these all the way to

a = 1

if that's what folks want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants