Skip to content

Add type hints to graphene/types/field.py - #1607

Open
faresrafat3 wants to merge 5 commits into
graphql-python:masterfrom
faresrafat3:graphene-1454-type-field-py
Open

faresrafat3 wants to merge 5 commits into
graphql-python:masterfrom
faresrafat3:graphene-1454-type-field-py

Conversation

@faresrafat3

Copy link
Copy Markdown

Part of #1454.

Annotates graphene/types/field.py and removes the duplicated type information
from the Field docstring, so the code becomes the single source of truth for
typing. Adds sphinx-autodoc-typehints to the docs so the hints are rendered
back into the API reference automatically.

Following the issue's guidance, this covers a whole file rather than a subset,
to avoid a later re-check of the same file.

Note on name and source

These are annotated as Optional[Union[str, Argument, UnmountedType]] rather
than Optional[str]. The constructor accepts an Argument/UnmountedType
for either and moves it into extra_args:

if isinstance(name, (Argument, UnmountedType)):
    extra_args["name"] = name
    name = None

An initial Optional[str] annotation surfaced a real call site doing exactly
that (graphene/validation/tests/test_depth_limit_validator.py passes
name=String()), so the annotation was widened to match the actual contract.

Verification

mypy graphene   -> Success: no issues found in 120 source files
ruff check      -> All checks passed
pytest -q       -> 465 passed

The docs extension was verified in a standalone Sphinx build against the
modified module — the rendered page contains Callable, Optional and Any,
confirming sphinx_autodoc_typehints is picking up the new annotations.

The one Sphinx warning in that build (undefined label: 'resolverparamgraphqlarguments') is pre-existing — it reproduces identically
on the unmodified file.

Part of graphql-python#1454. Annotates the module and removes the now-duplicated type
information from the Field docstring, so the code is the single source of
truth for typing. sphinx-autodoc-typehints is added to the docs so the
hints are rendered back into the API reference.

The name and source parameters accept an Argument or UnmountedType as
well as a string (the constructor moves such values into extra_args), so
their annotations include those types.

mypy graphene: Success: no issues found in 120 source files
ruff: All checks passed
pytest: 465 passed
Copilot AI balanced review requested due to automatic review settings October 1, 2026 12:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

faresrafat3 added 4 commits October 1, 2026 15:14
Part of graphql-python#1454. Annotates the Structure, List and NonNull classes.

mypy graphene: Success: no issues found in 120 source files
ruff: All checks passed
pytest: 465 passed
Part of graphql-python#1454. Annotates get_field_as, yank_fields_from_attrs, get_type and
get_underlying_type.

mypy graphene: Success: no issues found in 120 source files
ruff: All checks passed
pytest: 465 passed
The existing example only validates a single already-parsed query via
graphql.validate(), which left it unclear how to actually apply the rule
to incoming requests (see graphql-python#1552).

Add an 'Enforcing validation on every query' section showing the pattern:
parse, run validate() with the rules tuple, return the errors if any, and
otherwise execute. The example output was produced by running the code
against graphene, not written from reading it.

Part of graphql-python#1552.
The List section only showed a list of strings and moved on (see graphql-python#1171).
Adds answers to the three questions raised:

- returning a list of other objects, and how little the parent needs to
  know about the child
- self-referencing lists (and circular references) via lambda
- combining NonNull items with required=True for [String!]!

Every claim was checked by running the code:

  (1) list of objects   -> {'data': {'people': [{'name': 'Ada'}, ...]}}
  (2) self-referencing  -> {'data': {'me': {'friends': [{'name': ...}]}}}
  (3) required list     -> [String!]!

and the two negative claims were confirmed to actually fail without lambda:

  graphene.List(Later)  -> NameError: name 'Later' is not defined
  graphene.List(U)      -> NameError: name 'U' is not defined

Part of graphql-python#1171.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants