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

[DRAFT] Tweak annotation population to better maintain field definition ordering #3561

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AlecRosenbaum
Copy link
Contributor

Description

This change explores tweaking field annotation population to better maintain field ordering.

As-is (without this change), this is the behavior:

import strawberry

@strawberry.type
class A:
    type_1a: str = "type_1a"
    type_1b: str = strawberry.field()
    type_1c: str = strawberry.field(resolver=lambda: "type_1c")
    
    @strawberry.field
    def type_2(self) -> str:
        return "type_2"

    another_type_1: str
    another_type_1b: str = strawberry.field()
    another_type_1c: str = strawberry.field(resolver=lambda: "type_1c")

print(
    [f.name for f in A.__strawberry_definition__.fields]
)
# ['type_1a', 'type_1b', 'type_1c', 'another_type_1', 'another_type_1b', 'another_type_1c', 'type_2']

Ideally type_2 would be defined in the middle, matching the order of the field on the model.

Exploring potential solutions, it seems like without adding a metaclass or doing AST traversal it's pretty hard to get the actual definition order. In this PR I've tried to approximate it a little better without a major change.

The proposed change here does not work for maintaining ordering of bare annotations defined in the middle of other fields (e.g. another_type_1: str above). However, for other field definition styles it appears to work correctly. So if ordering is important in the schema there's then a simple solution to add = strawberry.field() to otherwise bare annotations.

Types of Changes

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

Issues Fixed or Closed by This PR

None, question was asked on discord.

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).

Copy link

codecov bot commented Jul 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.35%. Comparing base (dd8e827) to head (8f8ebb8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3561      +/-   ##
==========================================
- Coverage   96.58%   94.35%   -2.23%     
==========================================
  Files         524      520       -4     
  Lines       33632    32484    -1148     
  Branches     5577     3722    -1855     
==========================================
- Hits        32482    30649    -1833     
- Misses        915     1552     +637     
- Partials      235      283      +48     

Copy link

codspeed-hq bot commented Jul 8, 2024

CodSpeed Performance Report

Merging #3561 will not alter performance

Comparing closeio:field-ordering (8f8ebb8) with main (dd8e827)

Summary

✅ 13 untouched benchmarks

Copy link
Member

@erikwrede erikwrede left a comment

Choose a reason for hiding this comment

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

It might be worthwhile re-testing this on python 3.13 once we support it since they overhauled dataclass instantiation quite a bit.

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