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:

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.

class NWBPydanticGenerator(schema: Union[str, TextIO, types.SchemaDefinition, ForwardRef('Generator'), pathlib.Path], schemaview: linkml_runtime.utils.schemaview.SchemaView = None, format: Optional[str] = None, metadata: bool = True, useuris: Optional[bool] = None, log_level: Optional[int] = 30, mergeimports: Optional[bool] = True, 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, include: Union[str, pathlib.Path, types.SchemaDefinition, NoneType] = None, template_file: str = None, package: str = 'example', array_representations: List[linkml.generators.pydanticgen.array.ArrayRepresentation] = <factory>, black: bool = True, template_dir: Union[str, pathlib.Path, NoneType] = None, extra_fields: Literal['allow', 'forbid', 'ignore'] = 'forbid', gen_mixin_inheritance: bool = True, injected_classes: Optional[List[Union[str, Type]]] = None, injected_fields: List[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")'), imports: list[linkml.generators.pydanticgen.template.Import] = <factory>, metadata_mode: Union[linkml.generators.pydanticgen.pydanticgen.MetadataMode, str, NoneType] = <MetadataMode.AUTO: 'auto'>, split: bool = True, split_pattern: str = '.{{ schema.name }}', split_context: Optional[dict] = None, split_mode: linkml.generators.pydanticgen.pydanticgen.SplitMode = <SplitMode.AUTO: 'auto'>, gen_classvars: bool = True, gen_slots: bool = True, genmeta: bool = False, emit_metadata: bool = True, _predefined_slot_values: Optional[Dict[str, Dict[str, str]]] = None, _class_bases: Optional[Dict[str, List[str]]] = None, schema_map: Optional[Dict[str, types.SchemaDefinition]] = None, inlined: bool = True, **_kwargs)
injected_fields: List[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")')

A list/tuple of field strings to inject into the base class.

Examples:

injected_fields = (
    'object_id: Optional[str] = Field(None, description="Unique UUID for each object")',
)
split: bool = True

Generate schema that import other schema as separate python modules that import from one another, rather than rolling all into a single module (default, False).

schema_map: Dict[str, SchemaDefinition] | None = None

See LinkMLProvider.build() for usage - a list of specific versions to import from

array_representations: List[ArrayRepresentation]
black: bool = True

If black is present in the environment, format the serialized code with it

inlined: bool = True
emit_metadata: bool = True
gen_classvars: bool = True
gen_slots: bool = True
skip_meta: ClassVar[Tuple[str]] = ('domain_of', 'alias')
after_generate_slot(slot: SlotResult, sv: SchemaView) SlotResult
  • strip unwanted metadata

  • generate range with any_of

before_render_template(template: PydanticModule, sv: SchemaView) PydanticModule
compile_module(module_path: Path = None, module_name: str = 'test', **kwargs) ModuleType

Compiles generated python code to a module :return:

class AfterGenerateSlot

Container class for slot-modification methods

static skip_meta(slot: SlotResult, skip_meta: tuple[str]) SlotResult
static make_array_anyofs(slot: SlotResult) SlotResult

Make a Union of array ranges if multiple array types specified in any_of

static make_named_class_range(slot: SlotResult) SlotResult

When a slot has a named annotation, wrap it in Named

compile_python(text_or_fn: str, package_path: Path = None, module_name: str = 'test') ModuleType

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