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 function is_with_or_async_with_stmt by 37% in src/black/nodes.py #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
8 changes: 6 additions & 2 deletions src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from collections.abc import Iterator
from typing import Final, Generic, Literal, Optional, TypeVar, Union

from blib2to3 import pygram
from blib2to3.pgen2 import token
from blib2to3.pytree import NL

if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
Expand Down Expand Up @@ -880,12 +884,12 @@ def is_import(leaf: Leaf) -> bool:

def is_with_or_async_with_stmt(leaf: Leaf) -> bool:
"""Return True if the given leaf starts a with or async with statement."""
return bool(
return (
leaf.type == token.NAME
and leaf.value == "with"
and leaf.parent
and leaf.parent.type == syms.with_stmt
) or bool(
) or (
leaf.type == token.ASYNC
and leaf.next_sibling
and leaf.next_sibling.type == syms.with_stmt
Expand Down