-
Eelco van der Wel authorede9c082c8
"""
itembase.py contains the schema baseclasses, extended from Pydantic.
This file only contains core schema functionality
and does not to be changed to add new schema definitions.
"""
import uuid
from datetime import datetime, timezone
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Dict,
Generic,
List,
Tuple,
Type,
TypeVar,
Union,
no_type_check,
)
from pydantic import BaseModel, Extra, PrivateAttr, validator
from pydantic.fields import Field, FieldInfo, ModelField
from pydantic.generics import GenericModel
from pydantic.main import ModelMetaclass
from typing_extensions import dataclass_transform
from .utils import get_args, get_origin, type_or_union_to_tuple, type_to_str
# Typing variables
PodType = Union[bool, str, int, float, datetime]
TargetType = TypeVar("TargetType", bound="ItemBase")
ItemType = TypeVar("ItemType")
POD_TYPES: Dict[type, str] = {
bool: "Bool",
str: "Text",
int: "Integer",
float: "Real",
datetime: "DateTime",
}
def _field_is_property(field: ModelField) -> bool:
return field.outer_type_ in POD_TYPES and not field.name.startswith("_")
def _field_is_edge(field: ModelField) -> bool:
try:
if field.name.startswith("_") or get_origin(field.outer_type_) != list:
return False
args = get_args(field.outer_type_)
if args is None or len(args) != 1:
return False
return True
except Exception:
return False
@dataclass_transform(kw_only_default=True, field_descriptors=(Field, FieldInfo))
class _ItemMeta(ModelMetaclass):
"""
Metaclass for ItemBase that adds required class and instance properties.
Note: _itembase_private_attrs is a workaround for hiding private attributes
generated by pyright/pylance signature. This is only a cosmetic difference,
private attributes can be defined on a model with `PrivateAttr(default)` value.
See: https://github.com/pydantic/pydantic/discussions/4563