Open-source updates - 13 September 2021
Published by Patrick Arminio on
I'm back after a break in Italy! In the last couple of weeks I've managed to add support for PyRight; this will make the experience of using Strawberry on vscode much better! I've also done a few improvements to our GitHub actions and some code reviews.
Iām back after a break in Italy! In the last couple of weeks Iāve managed to add support for PyRight; this will make the experience of using Strawberry on vscode much better! Iāve also done a few improvements to our GitHub actions and some code reviews.
PyRight
A while ago I started a PR to improve the support for PyRight. This was prompted
by
this PEP proposal.
The PEP proposes a standard way for ātaggingā function that do similar transform
to dataclasses (like we us, Pydantic, SQL Model and more). This allows to use
pure python code to define function/decorator that transform classes by adding
methods like __init__
, __eq__
and so on.
Previously we could only do this with MyPy by using a (complex) plugin. Hopefully the PEP will be accepted at some point, meaning that all python type checkers implementing the spec will properly type check Strawberry and other libraries adopting the PEP.
I did hold off this PR for a bit since I wanted to make sure we supported most of our APIs and Iām very happy to have managed to influnce the PEP proposal to make it a bit easier for libraries to take advantage of the proposal, even when they donāt fully dataclassesās semantics, like Strawberry š
For example, before that change the following example would result in a class
that accepted one parameter, field_x
, at least for the type checkers:
def get_x() -> int:
return 42
@strawberry.type
class Query:
field_x: int = strawberry.field(resolver=get_x)
while at runtime it wouldnāt actually accept any parameter since fields with
resolvers (in Strawberry) donāt need a value (the value comes from the resolver
function). To fix this we can follow dataclassesā semantics and pass
init=False
to the field
function:
@strawberry.type
class Query:
field_x: int = strawberry.field(resolver=get_x, init=False)
But this is quite annoying, since the init
parameter wonāt be used by
Strawberry (at least when passing resolvers).
Luckily we can solve this by using overloads and Literals
(if youāre curious
to see how it works see the
spec).
By typing the field
function something like this:
@overload
def field(
*, # Note the omission of `resolver`
init: Literal[True] = True,
) -> Any:
...
@overload
def field(
*,
resolver: Callable[[], T],
init: Literal[False] = False,
) -> T:
...
See here for the full implementation
we can tell the type checker that when resolver
is passed init
is always
False
, and so we donāt have to pass it explicitly šÆ
Big thanks to Eric for accepting the suggestion and implementing it so quickly!
Actions
Iāve also spent some time cleaning up our GitHub actions and moved some of our actions to dedicated repos, hopefully some people might re-use them.
Thereās an action to get information about a PR. This action works in the PR itself but also on master after the PR has been merged (it finds the PR from the commit). We use this for generating the twitter cards and for inviting contributors to our organisation.
Thereās a
couple of actions for our tweet workflow.
They allow to get the tweet text from a TWEET.md
file and to validate the
text. This action is quite coupled to our workflow, but Iād love to make it more
reusable, so if someone needs this, let me know!
Iāve created a new action to invite contributors to our organisation automatically. I did try to use a pre-build action but it wasnāt working so Iāve made our own :)
We were doing this manually in the past, but now we donāt have to anymore, it loses the personal touch, but it is so easy to forget to invite people!
Again, if you want to re-use these actions let me know! Always happy to improve them š
Pull Request reviews
Hereās the list of PRs I managed to review in the last couple of weeks:
A PR to add fragments to the selected_fields
property on the info parameter
by Jon
A PR to fix an issue with the export-schema command by Matt
A PR to fix using union types with Pydantic types by Matt
A PR to fix optional values conversion from Pydantic types by Matt
Big props to Matt for sending 3 PRs!
A PR to allow any version of opentelemetry before 2.0 by Michael
A PR to raise an exception when using unions or interfaces as inputs by Mohammad
Fin
I was mostly off during the last two weeks but I managed to do some useful work, also Iāve noticed that Strawberry is gaining quite a bit of traction lately, which is super nice!
Written in Venice š®š¹