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

chore: remove more trailing commas #1758

Closed

Conversation

MarcoGorelli
Copy link
Member

yeah i wrote a script to do this:

import sys
import subprocess
import tokenize_rt

ret = 0
for file in sys.argv[1:]:
    with open(file) as fd:
        src = fd.read()
    tokens = tokenize_rt.src_to_tokens(src)
    n = len(tokens)
    if n < 2:
        continue
    tokens_to_remove: list[int] = []
    parens = 0
    n_commas = 0
    for i in range(len(tokens)):
        token = tokens[i]
        if token.name == 'OP' and token.src == '(':
            parens += 1
        elif token.name == 'OP' and token.src == ')':
            parens -= 1
        elif parens > 0 and token.name == 'OP' and token.src == ',':
            n_commas += 1

        if token.name == 'OP' and token.src == ')' and parens == 0:
            if n_commas > 1:
                j = i-1
                while j >0 and not tokens[j].src.strip():
                    j -= 1
                if tokens[j].name == 'OP' and tokens[j].src == ',':
                    tokens_to_remove.append(j)
            n_commas = 0
    if tokens_to_remove:
        for i in tokens_to_remove[::-1]:
            del tokens[i]
        with open(file, 'w') as fd:
            fd.write(tokenize_rt.tokens_to_src(tokens))
        subprocess.run(['ruff', 'format', file], capture_output=True)
        with open(file) as fd:
            new_src = fd.read()
        if new_src != src:
            ret = 1

sys.exit(ret)

😳

@MarcoGorelli MarcoGorelli marked this pull request as ready for review January 7, 2025 19:05
@MarcoGorelli
Copy link
Member Author

this shaves off exactly 0.2% of the package size...ok imma stop with this, it's a waste of time 😳 😄

@FBruzzesi
Copy link
Member

FBruzzesi commented Jan 7, 2025

this shaves off exactly 0.2% of the package size

(I am from mobile now but) I would aim for readability even if not relevant for the size

ok imma stop with this, it's a waste of time 😳 😄

If it helps morale, these scripts are magic and now you are a wizard to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants