Skip to content

Remove redundant condition from has_magic_trailing_comma#4023

Merged
JelleZijlstra merged 2 commits into
psf:mainfrom
bluetech:rm-redundant-branch
Nov 8, 2023
Merged

Remove redundant condition from has_magic_trailing_comma#4023
JelleZijlstra merged 2 commits into
psf:mainfrom
bluetech:rm-redundant-branch

Conversation

@bluetech
Copy link
Copy Markdown
Contributor

@bluetech bluetech commented Nov 4, 2023

Simplify the has_magic_trailing_comma logic (it will make #3918 a bit easier to implement).

The second if cannot be true at its execution point, because it is already covered by the first if. The condition
comma.parent.type == syms.subscriptlist always holds if closing.parent.type == syms.trailer holds, because subscriptlist only appears inside trailer in the grammar:

trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: (subscript|star_expr) (',' (subscript|star_expr))* [',']

NOTE: I split the PR to two commits, the first one is just a refactor with no logical changes, but makes the argument above easier to follow. The commits should be squashed when merging.

The second `if` cannot be true at its execution point, because it is
already covered by the first `if`. The condition
`comma.parent.type == syms.subscriptlist` always holds if
`closing.parent.type == syms.trailer` holds, because `subscriptlist`
only appears inside `trailer` in the grammar:

```
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: (subscript|star_expr) (',' (subscript|star_expr))* [',']
```
@bluetech
Copy link
Copy Markdown
Contributor Author

bluetech commented Nov 4, 2023

cc @hauntsaninja - you added some of the condition in 4ebf14d, maybe you can poke a hole in my logic :)

Also c4bd2e3 made a change here (removing the return comma.parent.type == syms.listmaker part), I haven't looked into why though.

@JelleZijlstra
Copy link
Copy Markdown
Collaborator

Also c4bd2e3 made a change here (removing the return comma.parent.type == syms.listmaker part), I haven't looked into why though.

Looks like the listmaker condition was only used if preview style for that year was off. Turning on the preview style by default meant that that branch was dead.

@JelleZijlstra JelleZijlstra added the ci: skip news Pull requests that don't need a changelog entry. label Nov 4, 2023
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 4, 2023

diff-shades reports zero changes comparing this PR (ac21ded) to main (c54c213).


What is this? | Workflow run | diff-shades documentation

Copy link
Copy Markdown
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you're correct, and diff-shades also found nothing changed by this refactor.

@bluetech
Copy link
Copy Markdown
Contributor Author

bluetech commented Nov 4, 2023

The branch is only relevant when --skip-magic-trailing-comma is used. I'm not sure whether diff-shades uses each project own config, and if so if any of the projects use the flag (I can check if necessary).

There's always a risk with changes like this of unexpected breakage, so it'd be nice to get some assurance like diff-shades. If diff-shades doesn't cover it, I'll look for some projects which use skip-magic-trailing-comma and check them.

@JelleZijlstra
Copy link
Copy Markdown
Collaborator

I'll run it on our internal codebase to check.

@JelleZijlstra JelleZijlstra merged commit 72e7a2e into psf:main Nov 8, 2023
@bluetech bluetech deleted the rm-redundant-branch branch November 9, 2023 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci: skip news Pull requests that don't need a changelog entry.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants