Skip to content

Commit

Permalink
Added module path to __qualname__ for type arguments of Dataset and M…
Browse files Browse the repository at this point in the history
…odel
  • Loading branch information
sveinugu committed Oct 21, 2023
1 parent d2d9975 commit 0078f88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/omnipy/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydantic.generics import GenericModel
from pydantic.utils import lenient_issubclass

from omnipy.data.model import Model
from omnipy.data.model import Model, generate_qualname

ModelT = TypeVar('ModelT', bound=Model)
Undefined = object()
Expand Down Expand Up @@ -88,7 +88,11 @@ def __class_getitem__(cls, model: ModelT) -> ModelT:
raise TypeError('Invalid model: {}! '.format(model)
+ 'omnipy Dataset models must be a specialization of the omnipy '
'Model class.')
return super().__class_getitem__(model)
created_dataset = super().__class_getitem__(model)

created_dataset.__qualname__ = generate_qualname(cls.__name__, model)

return created_dataset

def __init__(self,
value: Union[Dict[str, Any], Iterator[Tuple[str, Any]]] = Undefined,
Expand Down
13 changes: 13 additions & 0 deletions src/omnipy/data/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
from types import NoneType
from typing import Any, Dict, Generic, get_args, get_origin, Type, TypeVar, Union

from isort import place_module
from isort.sections import STDLIB
# from orjson import orjson
from pydantic import Protocol, root_validator
from pydantic.fields import ModelField, Undefined, UndefinedType
from pydantic.generics import GenericModel
from pydantic.typing import display_as_type

RootT = TypeVar('RootT')
ROOT_KEY = '__root__'
Expand All @@ -15,6 +18,14 @@
# return orjson.dumps(v, default=default).decode()


def generate_qualname(cls_name: str, model: Any) -> str:
m_module = model.__module__ if hasattr(model, '__module__') else ''
m_module_prefix = f'{m_module}.' \
if m_module and place_module(m_module) != STDLIB else ''
fully_qual_model_name = f"{m_module_prefix}{display_as_type(model)}"
return f'{cls_name}[{fully_qual_model_name}]'


class Model(GenericModel, Generic[RootT]):
"""
A data model containing a value parsed according to the model.
Expand Down Expand Up @@ -141,6 +152,8 @@ def __class_getitem__(cls, model: Union[Type[RootT], TypeVar]) -> Union[Type[Roo
if cls == Model:
cls._depopulate_root_field()

created_model.__qualname__ = generate_qualname(cls.__name__, model)

return created_model

def __new__(cls, value: Union[RootT, UndefinedType] = Undefined, **kwargs):
Expand Down

0 comments on commit 0078f88

Please sign in to comment.