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

Refactor the backend typing system #906

Merged
merged 106 commits into from
Jul 20, 2021
Merged

Conversation

BryceBeagle
Copy link
Member

@BryceBeagle BryceBeagle commented May 4, 2021

This PR is a refactor of the typing system within the library. Previously, typing was handled individually by fields, arguments, and types with a hodgepodge of functions to tie it together. This branch creates a unified typing system that the object, fields, and arguments each hook into.

This PR mainly replaces the attributes that were stored on StrawberryArgument and StrawberryField with a hierarchy of StrawberryTypes.

There is more work to be done in the future, especially related to unifying the system by removing .type_definition and ._enum_definition monkeypatching.

There is also some future discussion to be had on this PR's implementation of StrawberryType.__eq__, but current thoughts are to look back into it after this PR is merged.

See #1063 for testing additions. This was done to prevent this PR from getting too long. It is also a good place to look to see how the changes here work.

To Do

- [ ] Tests involving StrawberryType.__eq__
- [ ] Think longer about how to implement StrawberryType.__hash__. Currently breaking the hash contract
- [ ] New unit tests for the StrawberryType types
(@patrick91 and I decided this should be addressed in another discussion/PR after this one is merged)

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

@patrick91 patrick91 self-requested a review July 5, 2021 18:29
@patrick91 patrick91 marked this pull request as ready for review July 5, 2021 18:29
@patrick91 patrick91 changed the title (WIP) Refactor the backend typing system Refactor the backend typing system Jul 5, 2021
@botberry
Copy link
Member

botberry commented Jul 5, 2021

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Refactor of the library's typing internals. Previously, typing was handled
individually by fields, arguments, and objects with a hodgepodge of functions to tie it
together. This change creates a unified typing system that the object, fields, and
arguments each hook into.

Mainly replaces the attributes that were stored on StrawberryArgument and
StrawberryField with a hierarchy of StrawberryTypes.

Introduces StrawberryAnnotation, as well as StrawberryType and some subclasses,
including StrawberryList, StrawberryOptional, and StrawberryTypeVar.

This is a breaking change if you were calling the constructor for StrawberryField,
StrawberryArgument, etc. and using arguments such as is_optional or child.

@strawberry.field no longer takes an argument called type_. It instead takes a
StrawberryAnnotation called type_annotation.

@patrick91 patrick91 force-pushed the feature/strawberry_type_system branch from d70f3b5 to 825bc9b Compare July 5, 2021 18:37
@BryceBeagle BryceBeagle mentioned this pull request Jul 15, 2021
13 tasks
Copy link
Member

@patrick91 patrick91 left a comment

Choose a reason for hiding this comment

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

I'll test this on our work repo as soon as it is released. I tested it already, but I think we did some changes after that :)

strawberry/annotation.py Outdated Show resolved Hide resolved
@BryceBeagle BryceBeagle requested review from jkimbo and a team July 18, 2021 21:35
Copy link
Member

@jkimbo jkimbo left a comment

Choose a reason for hiding this comment

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

Nice work! Added some comments

strawberry/annotation.py Show resolved Hide resolved
raise MultipleStrawberryArgumentsError(
field_name=origin.__name__, argument_name=python_name
# TODO: This isn't the field name, it is the argument name
field_name=self.python_name,
Copy link
Member

Choose a reason for hiding this comment

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

Do we no longer have access to the field at this point? It makes the error message nicer if we can say which field has the issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, we no longer have access to the field name here. I would agree it makes the error better, but I don't have a good idea of how to do it.

I believe that @patrick91 and I agreed to just include this regression (I should open an issue for it, thanks for the reminder), but I'm open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

yes :) I think we can change a bit how we handle these errors, I'd love to have a better message and visualisation (for example pointing to the actual line), but that's something for the future :)

Copy link
Member

Choose a reason for hiding this comment

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

Ok in that case can you update the exception message to not include the bit about the field name: https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/exceptions.py#L121-L128

Copy link
Member Author

Choose a reason for hiding this comment

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

The test for it is now an xfail. I think we eventually want to go back to having the field name in the exception message. Should we still change it now?

Copy link
Member

Choose a reason for hiding this comment

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

I think we should change it because otherwise we have a misleading error message.

Copy link
Member

Choose a reason for hiding this comment

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

good point :D

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok changed the error message for now

strawberry/exceptions.py Show resolved Hide resolved
Copy link
Member

@jkimbo jkimbo left a comment

Choose a reason for hiding this comment

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

👍

@patrick91 patrick91 merged commit 669ca12 into main Jul 20, 2021
@patrick91 patrick91 deleted the feature/strawberry_type_system branch July 20, 2021 09:53
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.

4 participants