Pydantic#
Subclass of linkml.generators.PydanticGenerator
The pydantic generator is a subclass of
- linkml.utils.generator.Generator
- linkml.generators.oocodegen.OOCodeGenerator
The default __main__ method
- Instantiates the class
- Calls serialize()
The serialize method:
Accepts an optional jinja-style template, otherwise it uses the default template
Uses
linkml_runtime.utils.schemaview.SchemaView
to interact with the schema- Generates linkML Classes
generate_enums runs first
Note
This module is heinous. We have mostly copied and pasted the existing linkml.generators.PydanticGenerator
and overridden what we need to make this work for NWB, but the source is…
a little messy. We will be tidying this up and trying to pull changes upstream,
but for now this is just our hacky little secret.
- pydantic model LinkML_Meta#
Extra LinkML Metadata stored as a class attribute
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
__init__ uses __pydantic_self__ instead of the more common self for the first arg to allow self as a field name.
- Fields:
- default_template(pydantic_ver: str = '2', extra_classes: List[Type[BaseModel]] | None = None) str #
Constructs a default template for pydantic classes based on the version of pydantic
- class NWBPydanticGenerator(schema: Union[str, TextIO, linkml_runtime.linkml_model.meta.SchemaDefinition, ForwardRef('Generator')], schemaview: Optional[linkml_runtime.utils.schemaview.SchemaView] = None, format: Optional[str] = None, metadata: bool = <factory>, useuris: Optional[bool] = None, log_level: int = 30, mergeimports: Optional[bool] = <factory>, source_file_date: Optional[str] = None, source_file_size: Optional[int] = None, logger: Optional[logging.Logger] = None, verbose: Optional[bool] = None, output: Optional[str] = None, namespaces: Optional[linkml_runtime.utils.namespaces.Namespaces] = None, directory_output: bool = False, base_dir: str = None, metamodel_name_map: Dict[str, str] = None, importmap: Union[str, Mapping[str, str], NoneType] = None, emit_prefixes: Set[str] = <factory>, metamodel: linkml.utils.schemaloader.SchemaLoader = None, stacktrace: bool = False, template_file: str = None, package: str = 'example', pydantic_version: str = <factory>, allow_extra: bool = <factory>, gen_mixin_inheritance: bool = <factory>, gen_classvars: bool = <factory>, gen_slots: bool = <factory>, genmeta: bool = <factory>, emit_metadata: bool = <factory>, SKIP_ENUM: Tuple[str] = ('FlatDType',), SKIP_SLOTS: Tuple[str] = ('',), SKIP_CLASSES: Tuple[str] = ('',), INJECTED_FIELDS: Tuple[str] = ('hdf5_path: Optional[str] = Field(None, description="The absolute path that this object is stored in an NWB file")', 'object_id: Optional[str] = Field(None, description="Unique UUID for each object")'), split: bool = True, schema_map: Optional[Dict[str, linkml_runtime.linkml_model.meta.SchemaDefinition]] = None, versions: dict = None, **_kwargs)#
-
- INJECTED_FIELDS: Tuple[str] = ('hdf5_path: Optional[str] = Field(None, description="The absolute path that this object is stored in an NWB file")', 'object_id: Optional[str] = Field(None, description="Unique UUID for each object")')#
- schema_map: Dict[str, SchemaDefinition] | None = None#
- versions: dict = None#
See
LinkMLProvider.build()
for usage - a list of specific versions to import from
- sort_classes(clist: List[ClassDefinition], imports: Dict[str, List[str]]) List[ClassDefinition] #
sort classes such that if C is a child of P then C appears after P in the list
Overridden method include mixin classes
Modified from original to allow for imported classes
- get_class_slot_range(slot_range: str, inlined: bool, inlined_as_list: bool) str #
Monkeypatch to convert Array typed slots and classes into npytyped hints
- get_class_isa_plus_mixins(classes: List[ClassDefinition] | None = None) Dict[str, List[str]] #
Generate the inheritance list for each class from is_a plus mixins
Patched to only get local classes
- Returns:
- get_predefined_slot_value(slot: SlotDefinition, class_def: ClassDefinition) str | None #
Modified from base pydantic generator to use already grabbed induced_slot from already-grabbed and modified classes rather than doing a fresh iteration to save time and respect changes already made elsewhere in the serialization routine
- Returns:
Dictionary of dictionaries with predefined slot values for each class
- compile_python(text_or_fn: str, package_path: Path = None, module_name: str = 'test') module #
Compile the text or file and return the resulting module @param text_or_fn: Python text or file name that references python file @param package_path: Root package path. If omitted and we’ve got a python file, the package is the containing directory @return: Compiled module