assert_name checks each character in a Python generator

Open Beginner friendly
#304 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start with src/graphql/type/assert_name.py and read assert_name() alongside the existing name validation tests. Check the proposed isascii() and isidentifier() fast path against the specified GraphQL name pattern, while preserving current errors for invalid names. Run the relevant tests and compare schema build timings; done means valid names take the fast path and invalid names retain their existing behavior.

Written by the indexing model from the issue text.

Description

assert_name() in src/graphql/type/assert_name.py runs for every type, field, argument and enum value name when a schema is built.
It checks the name tail with all(is_name_continue(char) for char in name[1:]).
This runs one Python function call for each character.

For a schema of the GitHub size, this check uses about one quarter of the build time.

Situation Now With the proposed fix Change
build_ast_schema on the GitHub SDL (assume_valid=True) 14.40 ms 10.79 ms 25.0% less time
build_client_schema on the GitHub introspection result 13.97 ms 10.43 ms 25.3% less time
assert_name on 10,000 valid names 6.94 ms 0.69 ms 90.0% less time
assert_name on 10,000 invalid names 13.32 ms 8.62 ms 35.3% less time
parse on the large query (does not call assert_name) 38.43 ms 37.90 ms No change (within noise)

These are the best of 15 runs, median of 7 alternating processes per version.

Proposed fix: for an ASCII string, str.isidentifier() is true exactly for [_a-zA-Z][_a-zA-Z0-9]*.
Use name.isascii() and name.isidentifier() to return valid names early, and keep the current error checks and messages for invalid names.

Environment: CPython 3.12.13, Linux x86-64, AMD EPYC 9R14, graphql-core main at 0aaea71.

Dominant language
Python
Stars
534
Forks
159
Avg merge
1d 10h
Merged PRs (30d)
11

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from graphql-python/graphql-core

All issues in graphql-python/graphql-core

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.