Skip to content

Fix typing of TransportQueryError.errors to List[GraphQLError]#587

Closed
magicmark wants to merge 1 commit intographql-python:masterfrom
magicmark:fix/transport-query-error-typing
Closed

Fix typing of TransportQueryError.errors to List[GraphQLError]#587
magicmark wants to merge 1 commit intographql-python:masterfrom
magicmark:fix/transport-query-error-typing

Conversation

@magicmark
Copy link
Contributor

Summary

  • Changed the type annotation of TransportQueryError.errors from Optional[List[Any]] to Optional[List[GraphQLError]] in both the class attribute and __init__ parameter
  • Added from graphql import GraphQLError import to gql/transport/exceptions.py

Motivation

TransportQueryError.errors is populated from ExecutionResult.errors (from graphql-core), which is typed as list[GraphQLError] | None. The current List[Any] annotation loses this type information, making it harder for downstream consumers to work with the errors without casting. This change makes the type annotation accurately reflect the actual data being stored.

Test plan

  • All existing tests pass (527 passed, 1 pre-existing failure unrelated to this change)
  • flake8, isort, black, and mypy checks pass on the modified file
  • No behavioral changes — this is a type annotation-only fix

🤖 Generated with Claude Code

…QLError]

- Change `errors` type annotation in `TransportQueryError` from
  `Optional[List[Any]]` to `Optional[List[GraphQLError]]`
- Wrap raw dict payload in `GraphQLError` in `websockets_protocol.py`
  so all error paths produce `GraphQLError` objects consistently
- Update test assertions to use `GraphQLError` attribute access
  (`.message`, `.extensions`) instead of dict indexing (`["message"]`)
- Add `extensions is not None` guards for mypy narrowing

This reduces the total mypy error count from 24 to 10 (fixing 14
pre-existing type errors in websocket exception tests).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@magicmark magicmark force-pushed the fix/transport-query-error-typing branch from 1a5e2e6 to 23d888a Compare March 19, 2026 01:35
@magicmark magicmark marked this pull request as draft March 19, 2026 01:35
@magicmark
Copy link
Contributor Author

Closing this PR — after deeper investigation, the typing change isn't correct as-is.

TransportQueryError.errors holds raw dicts at runtime, not GraphQLError objects. All transports (aiohttp, httpx, requests, websockets, phoenix) pass payload.get("errors") (parsed JSON dicts) directly into ExecutionResult, which then gets forwarded to TransportQueryError.errors. Even though ExecutionResult.errors is typed as list[GraphQLError], the actual runtime values are list[dict].

To properly type this as List[GraphQLError], every transport would need to convert the raw error dicts into GraphQLError objects (using GraphQLError(msg, extensions=...) etc). That's a larger change that should be discussed first.

For now, List[Any] accurately reflects the runtime behavior.

@magicmark magicmark closed this Mar 19, 2026
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.

1 participant