|
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | 4 | import json |
5 | | -from copy import deepcopy |
6 | 5 | from datetime import date |
7 | | -from typing import Any, Callable, Collection, Optional, Sequence, Type, TypeVar, Union |
| 6 | +from typing import Any, Collection, Optional, Sequence, Union |
8 | 7 |
|
9 | | -from pydantic import create_model, field_validator, model_validator |
10 | | -from pydantic.fields import FieldInfo |
| 8 | +from pydantic import field_validator, model_validator |
11 | 9 | from typing_extensions import Self |
12 | 10 |
|
13 | 11 | from mavedb.lib.validation import urn_re |
|
42 | 40 | TargetGeneCreate, |
43 | 41 | ) |
44 | 42 | from mavedb.view_models.user import SavedUser, User |
| 43 | +from mavedb.view_models.utils import all_fields_optional_model |
45 | 44 |
|
46 | 45 | UnboundedRange = tuple[Union[float, None], Union[float, None]] |
47 | 46 |
|
48 | | -Model = TypeVar("Model", bound=BaseModel) |
49 | | - |
50 | | - |
51 | | -def all_fields_optional_model() -> Callable[[Type[Model]], Type[Model]]: |
52 | | - """A decorator that create a partial model. |
53 | | -
|
54 | | - Args: |
55 | | - model (Type[BaseModel]): BaseModel model. |
56 | | -
|
57 | | - Returns: |
58 | | - Type[BaseModel]: ModelBase partial model. |
59 | | - """ |
60 | | - |
61 | | - def wrapper(model: Type[Model]) -> Type[Model]: |
62 | | - def make_field_optional(field: FieldInfo, default: Any = None) -> tuple[Any, FieldInfo]: |
63 | | - new = deepcopy(field) |
64 | | - new.default = default |
65 | | - new.annotation = Optional[field.annotation] # type: ignore[assignment] |
66 | | - return new.annotation, new |
67 | | - |
68 | | - return create_model( |
69 | | - model.__name__, |
70 | | - __base__=model, |
71 | | - __module__=model.__module__, |
72 | | - **{field_name: make_field_optional(field_info) for field_name, field_info in model.model_fields.items()}, |
73 | | - ) # type: ignore[call-overload] |
74 | | - |
75 | | - return wrapper |
76 | | - |
77 | 47 |
|
78 | 48 | class ExternalLink(BaseModel): |
79 | 49 | url: Optional[str] = None |
|
0 commit comments