Skip to content

Commit

Permalink
Add notes to error messages & docs about get_metadata() vs. `get_ra…
Browse files Browse the repository at this point in the history
…w_metadata()`
  • Loading branch information
jwodder committed Jan 31, 2024
1 parent 96c3953 commit db6f4cf
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import click
from dandischema import models
from pydantic import BaseModel, Field, PrivateAttr
from pydantic import BaseModel, Field, PrivateAttr, ValidationError
import requests
import tenacity

Expand Down Expand Up @@ -971,8 +971,22 @@ def get_metadata(self) -> models.Dandiset:
"""
Fetch the metadata for this version of the Dandiset as a
`dandischema.models.Dandiset` instance
.. note::
Only published Dandiset versions can be expected to have valid
metadata. Consider using `get_raw_metadata()` instead in order to
fetch unstructured, possibly-invalid metadata.
"""
return models.Dandiset.parse_obj(self.get_raw_metadata())
try:
return models.Dandiset.parse_obj(self.get_raw_metadata())
except ValidationError as e:
raise ValueError(

Check warning on line 984 in dandi/dandiapi.py

View check run for this annotation

Codecov / codecov/patch

dandi/dandiapi.py#L983-L984

Added lines #L983 - L984 were not covered by tests
f"{type(e).__name__}: {e}\n\nNote: Only metadata for published"
" Dandiset versions can be expected to be valid. Use"
" get_raw_metadata() instead to get unstructured,"
" possibly-invalid metadata."
)

def get_raw_metadata(self) -> dict[str, Any]:
"""
Expand Down Expand Up @@ -1347,8 +1361,22 @@ def get_metadata(self) -> models.Asset:
"""
Fetch the metadata for the asset as a `dandischema.models.Asset`
instance
.. note::
Only assets in published Dandiset versions can be expected to have
valid metadata. Consider using `get_raw_metadata()` instead in
order to fetch unstructured, possibly-invalid metadata.
"""
return models.Asset.parse_obj(self.get_raw_metadata())
try:
return models.Asset.parse_obj(self.get_raw_metadata())
except ValidationError as e:
raise ValueError(

Check warning on line 1374 in dandi/dandiapi.py

View check run for this annotation

Codecov / codecov/patch

dandi/dandiapi.py#L1373-L1374

Added lines #L1373 - L1374 were not covered by tests
f"{type(e).__name__}: {e}\n\nNote: Only metadata for assets in"
" published Dandiset versions can be expected to be valid. Use"
" get_raw_metadata() instead to get unstructured,"
" possibly-invalid metadata."
)

def get_raw_metadata(self) -> dict[str, Any]:
"""Fetch the metadata for the asset as an unprocessed `dict`"""
Expand Down

0 comments on commit db6f4cf

Please sign in to comment.