Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SchemaExtension.should_await #3724

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

patrick91
Copy link
Member

@patrick91 patrick91 commented Dec 12, 2024

  • Add SchemaExtension.should_await
  • Update test

Description

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

Add the should_await method to the SchemaExtension class to optimize asynchronous execution checks by using a custom _is_async attribute. Update tests to reflect this change.

New Features:

  • Introduce the should_await method in the SchemaExtension class to determine if the result from the next callable should be awaited.

Enhancements:

  • Optimize the determination of whether a resolver is asynchronous by using a custom _is_async attribute instead of inspect.isawaitable.

Tests:

  • Update tests to utilize the new should_await method in the ResolveExtension class.

Copy link
Contributor

sourcery-ai bot commented Dec 12, 2024

Reviewer's Guide by Sourcery

This PR introduces a new should_await method to the SchemaExtension class to optimize performance by avoiding runtime isawaitable checks. The implementation leverages a new _is_async attribute that is set during schema creation time to determine if a resolver is asynchronous.

Class diagram for SchemaExtension with should_await method

classDiagram
    class SchemaExtension {
        +on_execute()
        +resolve(_next: Callable)
        +should_await(_next: Callable) bool
    }
    note for SchemaExtension "The should_await method determines if the extension should await the result from next based on the _is_async attribute."
Loading

Class diagram for SchemaConverter with _is_async attribute

classDiagram
    class SchemaConverter {
        +from_scalar(scalar: Type) GraphQLScalarType
        +_async_resolver
        +_resolver
    }
    class _async_resolver {
        +_is_default: bool
        +_is_async: bool
    }
    class _resolver {
        +_is_default: bool
        +_is_async: bool
    }
    SchemaConverter --> _async_resolver
    SchemaConverter --> _resolver
    note for _async_resolver "The _is_async attribute is set to True if the field is async."
    note for _resolver "The _is_async attribute is set to False if the field is not async."
Loading

File-Level Changes

Change Details Files
Added a new method to determine if a resolver is asynchronous
  • Implemented should_await method that checks _is_async attribute
  • Added docstring explaining the performance benefits over inspect.isawaitable
strawberry/extensions/base_extension.py
Modified schema converter to set async status during creation
  • Added _is_async flag setting for async and sync resolvers
  • Set _is_async=True for async resolvers
  • Set _is_async=False for sync resolvers
strawberry/schema/schema_converter.py
Updated benchmark test to use new should_await method
  • Replaced isawaitable check with should_await method call
tests/benchmarks/test_execute_with_extensions.py

Possibly linked issues


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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @patrick91 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

tests/benchmarks/test_execute_with_extensions.py Outdated Show resolved Hide resolved
@botberry
Copy link
Member

botberry commented Dec 12, 2024

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to @patrick91 for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

Copy link

codspeed-hq bot commented Dec 12, 2024

CodSpeed Performance Report

Merging #3724 will not alter performance

Comparing feature/faster-extension-check (8fb12e7) with main (45f4e50)

Summary

✅ 21 untouched benchmarks

Copy link

codecov bot commented Dec 12, 2024

Codecov Report

Attention: Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 97.01%. Comparing base (45f4e50) to head (8fb12e7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3724      +/-   ##
==========================================
- Coverage   97.01%   97.01%   -0.01%     
==========================================
  Files         502      502              
  Lines       33521    33525       +4     
  Branches     5598     5598              
==========================================
+ Hits        32522    32525       +3     
- Misses        793      794       +1     
  Partials      206      206              

@patrick91 patrick91 force-pushed the feature/faster-extension-check branch from 3a927dc to 8fb12e7 Compare December 12, 2024 20:47
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