Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all content for experiment #1

Merged
merged 6 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
venv/
__pycache__
personinfo_busted.py
personinfo_workaround.py
67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,65 @@
# linkml-experiments
LinkML experiments
# linkml-multivalued-default

An experiment in generating an empty list default for multivalued slots in a
LinkML model.

## Background

It is difficult or impossible in LinkML to specify that you want an empty list
as the default value for a slot that is both `required` and `multivalued`. This
repository contains some materials to help demonstrate this, as well as a
workaround.

For more context, see the following issues:
- https://github.com/dandi/dandi-archive/issues/2150
- https://github.com/dandi/dandi-schema/issues/286

## Setup

1. If you are using Nix flakes, run `nix develop` to install dependencies.
Otherwise, ensure you have a modern Python installed (e.g., Python 3.10).

2. Create a virtual environment with `python -m venv venv` and activate it with
`. ./venv/bin/activate`.

3. Install the Python dependencies with `pip install -r requirements.txt`.

## Demonstration of non-optional list types with default value.

1. Generate a Pydantic model by running `gen-pydantic personinfo_busted.yaml >personinfo_busted.py`.
Note that setting both `multivalued=true` and `required=true` correctly
infers the type of the `aliases3` field as `List[str]` (rather than
`Optional[List[str]]` as for `aliases2`, which is not required), but that it
is not possible to set a default value of `[]` directly (the Pydantic
generator does not have a way to encode list values in the `ifabsent`
attribute; attempts to do so generate the string `"[]"`).

2. Generate another Pydantic model by running `gen-pydantic
personinfo_workaround.yaml >personinfo_workaround.py`. Note that this model
sets a globally unique and incorrectly typed default on `aliases3`.

3. Run `gen-pydantic personinfo_workaround.yaml | sed s/\"aliases3dummy\"/[]/ >personinfo_workaround.py`.
Note that now the definition of `aliases3` correctly has an empty list
default value.

4. Fire up Python and run this script:

```python
from personinfo_workaround import Person
p = Person(name2='Ada Byron', aliases2=['Lady Byron'])
p
```

Note that `p` contains the expected values for all fields, including a
default `[]` for `aliases3`.

The need to post-process like this is unfortunate, but seems to be necessary
due to unresolved issues of defaults for multivalued slots in LinkML
([[1]](https://github.com/orgs/linkml/discussions/1975),
[[2]](https://github.com/orgs/linkml/discussions/2314)).

## Conclusion

It *is* possible to generate correct Pydantic models from LinkML schemata
featuring **required**, **multivalued** attributes **with a list-valued
default**, but it currently requires a transformation of the generated code.
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
description = "Nix dependencies for running LinkML experiments";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
};

outputs = { self, nixpkgs }: {
devShell.x86_64-linux =
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in pkgs.mkShell {
buildInputs = with pkgs; [
python310Packages.python
];
};
};
}
45 changes: 45 additions & 0 deletions personinfo_busted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
id: https://w3id.org/linkml/examples/personinfo
name: personinfo
prefixes:
linkml: https://w3id.org/linkml/
personinfo: https://w3id.org/linkml/examples/personinfo
imports:
- linkml:types
default_range: string
default_prefix: personinfo

classes:
Person:
attributes:
# Not required, implicit default value is None.
name1:

# Required, but no default specified.
name2:
required: true

# Required, with a default.
name3:
required: true
ifabsent: string(Alan Turing)

# Not required, implicit default value is None.
aliases1:
multivalued: true

# Required, but no default specified.
aliases2:
multivalued: true
required: true

# Required, with a default (ends up as `"[]"`, which is wrong).
aliases3:
multivalued: true
required: true
ifabsent: []

# Required, with a dummy default value to be replaced later.
aliases4:
multivalued: true
required: true
ifabsent: string(aliases4dummy)
39 changes: 39 additions & 0 deletions personinfo_workaround.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
id: https://w3id.org/linkml/examples/personinfo
name: personinfo
prefixes:
linkml: https://w3id.org/linkml/
personinfo: https://w3id.org/linkml/examples/personinfo
imports:
- linkml:types
default_range: string
default_prefix: personinfo

classes:
Person:
attributes:
# Not required, implicit default value is None.
name1:

# Required, but no default specified.
name2:
required: true

# Required, with a default.
name3:
required: true
ifabsent: string(Alan Turing)

# Not required, implicit default value is None.
aliases1:
multivalued: true

# Required, but no default specified.
aliases2:
multivalued: true
required: true

# Required, with a dummy default value to be replaced later.
aliases3:
multivalued: true
required: true
ifabsent: string(aliases3dummy)
76 changes: 76 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
annotated-types==0.7.0
antlr4-python3-runtime==4.9.3
arrow==1.3.0
attrs==25.1.0
certifi==2025.1.31
CFGraph==0.2.1
chardet==5.2.0
charset-normalizer==3.4.1
click==8.1.8
curies==0.10.6
Deprecated==1.2.18
et_xmlfile==2.0.0
exceptiongroup==1.2.2
fqdn==1.5.1
graphviz==0.20.3
greenlet==3.1.1
hbreader==0.9.1
idna==3.10
iniconfig==2.0.0
isodate==0.7.2
isoduration==20.11.0
Jinja2==3.1.5
json-flattener==0.1.9
jsonasobj==1.3.1
jsonasobj2==1.0.4
jsonpatch==1.33
jsonpath-ng==1.7.0
jsonpointer==3.0.0
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
linkml==1.8.7
linkml-dataops==0.1.0
linkml-runtime==1.8.3
MarkupSafe==3.0.2
openpyxl==3.1.5
packaging==24.2
parse==1.20.2
pluggy==1.5.0
ply==3.11
prefixcommons==0.1.12
prefixmaps==0.2.6
pydantic==2.10.6
pydantic_core==2.27.2
PyJSG==0.11.10
pyparsing==3.2.1
PyShEx==0.8.1
PyShExC==0.9.1
pytest==8.3.4
pytest-logging==2015.11.4
python-dateutil==2.9.0.post0
PyTrie==0.4.0
PyYAML==6.0.2
rdflib==7.1.3
rdflib-jsonld==0.6.1
rdflib-shim==1.0.3
referencing==0.36.2
requests==2.32.3
rfc3339-validator==0.1.4
rfc3987==1.3.8
rpds-py==0.23.1
ruamel.yaml==0.18.10
ruamel.yaml.clib==0.2.12
ShExJSG==0.8.2
six==1.17.0
sortedcontainers==2.4.0
sparqlslurper==0.5.1
SPARQLWrapper==2.0.0
SQLAlchemy==2.0.38
tomli==2.2.1
types-python-dateutil==2.9.0.20241206
typing_extensions==4.12.2
uri-template==1.3.0
urllib3==2.3.0
watchdog==6.0.0
webcolors==24.11.1
wrapt==1.17.2