Skip to content

Commit d588e2b

Browse files
committed
Remove typing.cast overhead and add character_classes to mypyc
- Remove cast() calls in parser hot paths (parse_name, parse_arguments, etc.) - Use type: ignore comments instead of runtime cast() overhead - Add character_classes.py to mypyc compilation - Marginal additional speedup on top of frozenset optimizations
1 parent 819386a commit d588e2b

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

build_mypyc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"graphql/language/lexer.py",
3131
"graphql/language/parser.py",
3232
"graphql/language/predicates.py",
33+
"graphql/language/character_classes.py",
3334
"graphql/utilities/coerce_input_value.py",
3435
"graphql/utilities/value_from_ast.py",
3536
"graphql/utilities/ast_from_value.py",

src/graphql/language/parser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ def __init__(
266266
def parse_name(self) -> NameNode:
267267
"""Convert a name lex token into a name parse node."""
268268
token = self.expect_token(TokenKind.NAME)
269-
# NAME tokens always have a value
270-
return NameNode(value=cast("str", token.value), loc=self.loc(token))
269+
# NAME tokens always have a string value (avoid cast() overhead)
270+
return NameNode(value=token.value, loc=self.loc(token)) # type: ignore[arg-type]
271271

272272
# Implement the parsing rules in the Document section.
273273

@@ -317,7 +317,7 @@ def parse_definition(self) -> DefinitionNode:
317317
)
318318

319319
if keyword_token.kind is TokenKind.NAME:
320-
token_name = cast("str", keyword_token.value)
320+
token_name: str = keyword_token.value # type: ignore[assignment]
321321
method_name = self._parse_type_system_definition_method_names.get(
322322
token_name
323323
)
@@ -474,7 +474,7 @@ def parse_arguments(self, is_const: bool) -> tuple[ArgumentNode, ...]:
474474
item = self.parse_const_argument if is_const else self.parse_argument
475475
return self.optional_many(
476476
TokenKind.PAREN_L,
477-
cast("Callable[[], ArgumentNode]", item),
477+
item,
478478
TokenKind.PAREN_R,
479479
)
480480

@@ -711,7 +711,7 @@ def parse_type_system_extension(self) -> TypeSystemExtensionNode:
711711
keyword_token = self._lexer.lookahead()
712712
if keyword_token.kind == TokenKind.NAME:
713713
method_name = self._parse_type_extension_method_names.get(
714-
cast("str", keyword_token.value)
714+
keyword_token.value # type: ignore[arg-type]
715715
)
716716
if method_name: # pragma: no cover
717717
return getattr(self, f"parse_{method_name}")()

0 commit comments

Comments
 (0)