Skip to content

Commit

Permalink
Remove DandiBaseModel.unvalidated
Browse files Browse the repository at this point in the history
Its use can be replaced with `pydantic.BaseModel.model_construct`
  • Loading branch information
candleindark committed Nov 21, 2023
1 parent b211bfd commit b697d76
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 28 deletions.
27 changes: 1 addition & 26 deletions dandischema/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import deepcopy
from datetime import date, datetime
from enum import Enum
import os
Expand Down Expand Up @@ -373,34 +372,10 @@ def ensure_schemakey(cls, val: str) -> str:
)
return val

@classmethod
def unvalidated(__pydantic_cls__: Type[M], **data: Any) -> M:
"""Allow model to be returned without validation"""
for name, field in __pydantic_cls__.model_fields.items():
try:
data[name]
except KeyError:
if not field.required:
if field.default_factory is not None:
value = field.default_factory()
elif field.default is None:
# deepcopy is quite slow on None
value = None
else:
value = deepcopy(field.default)
data[name] = value
else:
# TODO: Find out what is the intended way to handle this case.
# When the field is not required, no default can be trusted.
raise NotImplementedError(
f"Field {name} is required but not provided"
)
return __pydantic_cls__.model_construct(**data)

@classmethod
def to_dictrepr(__pydantic_cls__: Type["DandiBaseModel"]) -> str:
return (
__pydantic_cls__.unvalidated()
__pydantic_cls__.model_construct()
.__repr__()
.replace(__pydantic_cls__.__name__, "dict")
)
Expand Down
4 changes: 2 additions & 2 deletions dandischema/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@


def test_dandiset() -> None:
assert Dandiset.unvalidated()
assert Dandiset.model_construct()


def test_asset() -> None:
assert Asset.unvalidated()
assert Asset.model_construct()


def test_asset_digest() -> None:
Expand Down

0 comments on commit b697d76

Please sign in to comment.