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

Speed up blib2to3 tokenization using generic python function #4541

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
### Performance

<!-- Changes that improve Black's performance. -->
- Speed up the `is_fstring_start` function in `blib2to3` tokenization using generic python `startswith` function (#4541)
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved

### Output

Expand Down
4 changes: 2 additions & 2 deletions src/blib2to3/pgen2/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _combinations(*l: str) -> set[str]:
| {f"{prefix}'" for prefix in _strprefixes | _fstring_prefixes}
| {f'{prefix}"' for prefix in _strprefixes | _fstring_prefixes}
)
fstring_prefix: Final = (
fstring_prefix: Final = tuple(
{f"{prefix}'" for prefix in _fstring_prefixes}
| {f'{prefix}"' for prefix in _fstring_prefixes}
| {f"{prefix}'''" for prefix in _fstring_prefixes}
Expand Down Expand Up @@ -459,7 +459,7 @@ def untokenize(iterable: Iterable[TokenInfo]) -> str:


def is_fstring_start(token: str) -> bool:
return builtins.any(token.startswith(prefix) for prefix in fstring_prefix)
return token.startswith(fstring_prefix)


def _split_fstring_start_and_middle(token: str) -> tuple[str, str]:
Expand Down