Skip to content

Use a class variable to store linked data in Pydantic models in dandischema.models #320

@candleindark

Description

@candleindark

The linked data of each model defined in dandischema.models is currently stored with the private attribute _ldmeta, such as https://github.com/dandi/dandi-schema/blob/6d6c7b18addc40bbbbe8311d4a3f8a37e832cc3d/dandischema/models.py#L645C5-L645C34. We can instead store the linked data of each model in a class variable instead.

Specifically, instead of the following

_ldmeta = {"nskey": "schema"}

we can have

ldmeta: ClassVar[dict] = {"nskey": "schema"}. # Using an identifier without an underscore

Doing so has several benefits.

  1. Semantically, a class variable relates to the containing class/model while a private attribute relates to a instance of the class. Thus, a class variable is more in line with the fact that the link data relates to the model not to an instance of a model.
  2. We don't have to use an identifier that starts with an underscore. (We can use just ldmeta or even linked_data)
  3. The assigned value will not be implicitly coerced into another type.
    Currently the initial assigned value of a _ldmeta attribute is coerced into a pydantic.fields.ModelPrivateAttr, as depicted below.
     from dandischema.models import BaseType
     type(BaseType._ldmeta)
     <class 'pydantic.fields.ModelPrivateAttr'>
    As a result, to access the initial assigned value is rather cumbersome as https://github.com/dandi/dandi-schema/blob/6d6c7b18addc40bbbbe8311d4a3f8a37e832cc3d/dandischema/metadata.py#L75C12-L75C49.

Please let me know if I should proceed with this proposed change with a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    internalChanges only affect the internal API

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions