dandi / dandi/dandi-schema

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

Open
#320 1 comment 0 reactions 0 assignees View on GitHub
internal
Dominant language
Python
Stars
7
Forks
15
PR merge metrics
No merged PRs in 30d

Description

The linked data of each model defined in `dandischema.models` is currently stored with the [private attribute](https://docs.pydantic.dev/latest/concepts/models/#private-model-attributes) `_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](https://docs.pydantic.dev/latest/concepts/models/#class-variables) instead.

Specifically, instead of the following

```py
_ldmeta = {"nskey": "schema"}
```

we can have

```py
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.
```py
from dandischema.models import BaseType
type(BaseType._ldmeta)

```
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.