Replies: 1 comment 2 replies
-
IMO it would be nice to go the same was as pydantic and just store this as a model attribute (eg. Eg. here I make an actual pydantic model (rather than just a template object) so it can be introspected at runtime: and then have a method to populate it with whatever properties are needed: https://github.com/p2p-ld/nwb-linkml/blob/eac5ef4c80b2ba1ed751ddbe3348a34edaac8a68/nwb_linkml/src/nwb_linkml/generators/pydantic.py#L483 and i've extended the template to allow for passing extra classes because that in general is useful when trying to patch the build process: which ends up looking like this: https://github.com/p2p-ld/nwb-linkml/blob/eac5ef4c80b2ba1ed751ddbe3348a34edaac8a68/nwb_linkml/src/nwb_linkml/models/pydantic/core/v2_6_0_alpha/core_nwb_base.py#L49 and that example is just for the totally trivial thing of storing a single being a |
Beta Was this translation helpful? Give feedback.
-
Currently when compiling to pydantic, we translate a lot of the linkml metamodel
We prioritize things that would be useful for validation. However, there are some things we don't translate
id_prefixes
Some of these could be compiled into existing elements; e.g. fields like aliases could be appended onto the docstring. id_prefixes could be compiled into patterns.
However, it would be better to have a first-class representation of some of this in pydantic, such that things are available for introspection. The obvious way to do this is ClassVars or ModelConfigs.
Note that this wouldn't affect the runtime behavior of pydantic (and would not require any addition dependencies).
Another option is to extend the pydantic metamodel; for example @cthoyt's semantic-pydantic extends
Field
:https://cthoyt.com/2024/01/10/semantic-pydantic.html
This is an intriguing possibility. We could have a profile in pydanticgen that would make SemanticFields.
Note however that this would entail a runtime dependency. We have gone down this route previously with the dataclass generator, which adds lots of semantic goodies onto dataclasses, but does come with some downsides in managing dependencies.
There may also be other libraries that are out there that also aim to add semantic annotations onto pydantic.
Perhaps there is a way that allows for composability. This is common in the java universe, and is solved with annotations on the elements. Unfortunately we can't have decorators at the attribute level in python, AFAIK...
Beta Was this translation helpful? Give feedback.
All reactions