You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#280 went back to using get_type_hints, but appears to have lost the description annotations on Generic classes. Here's a snippet that works on 0.16.4:
fromdataclassesimportdataclassfromtypesimportnew_classfromtypingimportAny, Dict, Generic, TypeVarfromapischemaimportdeserializer, schema, serializerfromapischema.conversionsimportConversionfromapischema.graphqlimportgraphql_schemafromapischema.tagged_unionsimportTagged, TaggedUnion, get_taggedfromapischema.type_namesimporttype_namefromgraphqlimportprint_schemafromtyping_extensionsimportAnnotatedCls=TypeVar("Cls", bound=type)
T=TypeVar("T")
generic_name=type_name(lambdacls, *args: cls.__name__)
defas_tagged_union(cls: Cls) ->Cls:
defserialization() ->Conversion:
annotations= {sub.__name__: Tagged[sub] forsubincls.__subclasses__()}
namespace= {"__annotations__": annotations}
tagged_union=new_class(
cls.__name__,
(TaggedUnion, Generic[T]),
exec_body=lambdans: ns.update(namespace),
)
returnConversion(
lambdaobj: tagged_union(**{obj.__class__.__name__: obj}),
source=cls[T],
target=tagged_union[T],
# Conversion must not be inherited because it would lead to# infinite recursion otherwiseinherited=False,
)
defdeserialization() ->Conversion:
generic_name(cls)
forsubincls.__subclasses__():
generic_name(sub)
namespace: Dict[str, Any] = {
"__annotations__": {
sub.__name__: Tagged[sub[T]] forsubincls.__subclasses__()
}
}
# Create the deserialization tagged union classtagged_union=new_class(
cls.__name__,
(TaggedUnion, Generic[T]),
exec_body=lambdans: ns.update(namespace),
)
returnConversion(
lambdaobj: get_tagged(obj)[1], source=tagged_union[T], target=cls[T]
)
deserializer(lazy=deserialization, target=cls)
serializer(lazy=serialization, source=cls)
returncls@as_tagged_unionclassBase(Generic[T]):
bar: T@dataclassclassFoo(Base[T]):
bar: Annotated[T, schema(description="A Foo bar")]
@dataclassclassBat(Base[T]):
bar: Annotated[T, schema(description="A Foo bar")]
defget_bar(base: Base[str]) ->str:
returnbase.bardeftest_fails():
expected='''\type Query { getBar(base: BaseInput!): String!}input BaseInput { Foo: FooInput Bat: BatInput}input FooInput { """A Foo bar""" bar: String!}input BatInput { """A Foo bar""" bar: String!}'''assertprint_schema(graphql_schema(query=[get_bar])) ==expected
Shame on me … I've forgotten one include_extras=True in #280.
I will fix it tonight and release a new patch (version 0.16.4 should be deleted because it's bugged).
Sorry for the inconvenience, I hope you can still work with v0.16.4 for now.
Thanks for the quick response, 0.16.4 will be fine for me, I only noticed because I have a weekly job on one of my projects that tests against the latest version of all its dependencies...
#280 went back to using
get_type_hints
, but appears to have lost the description annotations on Generic classes. Here's a snippet that works on 0.16.4:On 0.16.5 it fails with:
The text was updated successfully, but these errors were encountered: