Open TimoGlastra opened 3 years ago
For anyone else running into this issue, I've managed to fix it using a custom pydantic converter with a wrapper model class. This leverages the union support of pydantic:
if typing.get_origin(self._model) is Union:
class Container(BaseModel):
v: self._model
data = {"v": data}
return Container.parse_obj(data).v
FTR, I shared the hopefully generic implementation I came up with combining various solutions found here and in the discussion #255.
Is your feature request related to a problem? Please describe.
It would be nice if Uplink could support Union types as response model. We're using Uplink (with Pydantic) to automatically generate a client. An example method (simplified) that it will generate is as follows:
This will throw an error:
TypeError: issubclass() arg 1 must be a class
becauseUnion
is not a class (see stack trace below for more info)Describe the solution you'd like
It would be nice if uplink could support Union types as response models. Pydantic has this feature, where you need to make sure to put the in the right order, as the first schema that matches will be taken. Something similar (maybe leaning on the implementation in Pydantic) would be a great addition I think. Pydantic feature docs: https://pydantic-docs.helpmanual.io/usage/types/#unions
Additional context
Error log when using Union type: