-
Notifications
You must be signed in to change notification settings - Fork 0
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: remodel DoapTarget #130
Conversation
@BalduinLandolt This is my try-out of your idea how the DoapTarget could be remodeled. The result is pleasing, but I have a problem with the validation of nested pydantic models which makes the tests fail. The problem is that inside the pydantic model The problem appears when reading serialized JSON data: The model_validator doesn't know what to do with the JSON data, because I tried several solutions, but nothing worked. If you don't have an idea, I'll have to close this PR. |
Not sure if this would work and make things nice... but what about class DoapTarget(BaseModel):
project_iri: str
target: GroupDoapTarget | EntityDoapTarget
class GroupDoapTarget(BaseModel):
group: Group
class EntityDoapTarget(BaseModel):
resource_class: str | None = None
property: str | None = None Or if that nesting turns out to be undesireable: type DoapTarget = GroupDoapTarget | EntityDoapTarget
class GroupDoapTarget(BaseModel):
project_iri: str
group: Group
class EntityDoapTarget(BaseModel):
project_iri: str
resource_class: str | None = None
property: str | None = None But maybe it's just not possible to do this in a nice way, in python... in a more functional language, instead of a union type, an enum type could be used, which would make it much nicer. But Python's enums simply&sadly just don't work as types. Edit: Another option may be extracting the deserialization to a factory method. But that would make the use of pydantic a bit pointless. |
Thanks for your input. But the problem are not python's union types, I believe. Rather, when pydantic deserializes a JSON, and then encounters a field that is of type Perhaps it's possible to override the built-in |
I rest my case, if proper sum types were a thing in Python, then Pydantic would know how to handle them... but that's splitting hairs. |
Thanks for your links, thanks to them I could finally solve it. Now it works :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments, otherwise I think it looks pretty clean, right?
No description provided.