Skip to content

Conversation

@magicmark
Copy link
Contributor

@magicmark magicmark commented Jan 7, 2026

Use TypedDict for Python query codegen.

Description

  • Change Python query codegen plugin to generate TypedDict classes instead of plain classes
  • Use NotRequired for optional fields with default values (TypedDict doesn't support = syntax)
  • Update documentation with new TypedDict output examples

🤖 Generated with Claude Code

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Summary by Sourcery

Switch Python query code generation to use TypedDict-based result and input types and improve schema codegen handling of custom directives and errors.

New Features:

  • Support custom directive definitions in schema code generation by skipping them without failing.

Enhancements:

  • Generate Python query result and input types as TypedDicts instead of plain classes for better type safety.
  • Represent optional fields with default values in generated Python code using NotRequired[...] annotations.
  • Improve schema codegen error messages to report the actual unknown definition type name.

Documentation:

  • Update query codegen documentation to show TypedDict-based Python output examples.
  • Add release notes describing the TypedDict migration and custom directive handling in schema codegen.

Tests:

  • Add a CLI schema codegen test to verify schemas with custom directives are processed successfully.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 7, 2026

Reviewer's Guide

Updates the Python query code generation to emit TypedDict-based result and input types (using NotRequired for optional fields with defaults), teaches schema codegen to gracefully skip custom directive definitions and improves its error messaging, and refreshes tests/docs plus adds a release note for these behaviors.

File-Level Changes

Change Details Files
Generate TypedDict-based Python query result and input types, using NotRequired for optional fields with default values, and update snapshots/docs accordingly.
  • Change object-type codegen to emit classes inheriting from TypedDict rather than plain classes.
  • Mark fields with default values as NotRequired[...] instead of using assignment-based defaults, and track required imports via typing_extensions.
  • Regenerate Python codegen snapshot fixtures for queries, mutations, variables, unions, interfaces, fragments, Relay examples, and enums to match TypedDict output.
  • Update query codegen documentation example to show TypedDict import and TypedDict-based classes.
  • Add a release note describing the TypedDict/NotRequired migration and its semantics.
strawberry/codegen/plugins/python.py
tests/codegen/snapshots/python/mutation_with_object.py
tests/codegen/snapshots/python/union.py
tests/codegen/snapshots/python/union_with_typename.py
tests/codegen/snapshots/python/variables.py
tests/codegen/snapshots/python/generic_types.py
tests/codegen/snapshots/python/interface_fragments_with_spread.py
tests/codegen/snapshots/python/union_with_typename_and_fragment.py
tests/codegen/snapshots/python/mutation-fragment.py
tests/codegen/snapshots/python/mutation.py
tests/codegen/snapshots/relay/python/relay_nested_nodes.py
tests/codegen/snapshots/relay/python/relay_variables.py
tests/codegen/snapshots/python/interface_fragments.py
tests/codegen/snapshots/python/multiple_types.py
tests/codegen/snapshots/python/alias.py
tests/codegen/snapshots/python/fragment.py
tests/codegen/snapshots/python/interface.py
tests/codegen/snapshots/python/interface_single_fragment.py
tests/codegen/snapshots/python/union_return.py
tests/codegen/snapshots/python/union_with_one_type.py
tests/codegen/snapshots/python/with_directives.py
tests/codegen/snapshots/relay/python/relay_alias.py
tests/codegen/snapshots/relay/python/relay_fragment.py
tests/codegen/snapshots/relay/python/relay_node_id.py
tests/codegen/snapshots/python/basic.py
tests/codegen/snapshots/python/multiple_types_optional.py
tests/codegen/snapshots/python/nullable_list_of_non_scalars.py
tests/codegen/snapshots/relay/python/relay_list.py
tests/codegen/snapshots/python/custom_scalar.py
tests/codegen/snapshots/python/enum.py
tests/codegen/snapshots/python/optional_and_lists.py
docs/codegen/query-codegen.md
RELEASE.md
Teach schema codegen to skip custom directive definitions and improve the NotImplementedError message for unknown definitions, plus add a targeted CLI test.
  • Add handling for DirectiveDefinitionNode in schema codegen that simply skips custom directive definitions as they require no Python code generation.
  • Change the NotImplementedError message to include the underlying GraphQL AST node class name instead of a generic object representation.
  • Introduce a CLI test that verifies schema-codegen succeeds and generates types when the SDL includes a custom directive definition and usage.
strawberry/schema_codegen/__init__.py
tests/cli/test_schema_codegen.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.21%. Comparing base (c8d6ae4) to head (21c18bf).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4089      +/-   ##
==========================================
- Coverage   94.22%   94.21%   -0.02%     
==========================================
  Files         541      541              
  Lines       35519    35521       +2     
  Branches     1877     1877              
==========================================
- Hits        33469    33466       -3     
- Misses       1739     1747       +8     
+ Partials      311      308       -3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Change the Python query codegen plugin to generate TypedDict classes instead
of plain classes. This provides better type safety for dictionary-like access
patterns commonly used when working with JSON responses from GraphQL APIs.

Optional fields with default values now use NotRequired instead of the
= value syntax (which TypedDict doesn't support).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@magicmark magicmark force-pushed the query_codegen_using_typed_dicts branch from 11caefc to 21c18bf Compare January 7, 2026 20:33
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 7, 2026

Merging this PR will not alter performance

Summary

✅ 31 untouched benchmarks


Comparing magicmark:query_codegen_using_typed_dicts (21c18bf) with main (c8d6ae4)

Open in CodSpeed

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