You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# TODO: Follow pydantic topic https://github.com/pydantic/pydantic/issues/6868 on MappingProxyType.
fromtypesimportMappingProxyTypefromtypingimportGeneric, TypeAliasfromtyping_extensionsimportForwardReffromomnipy.data.helpersimportDoubleTypeVarStore, TypeVarStorefromomnipy.data.modelimportModelfromomnipy.modules.frozen.typedefsimportFrozenDict, KeyT, ValTfromomnipy.modules.general.modelsimportNotIterableExceptStrOrBytesModel# TODO: Follow pydantic topic https://github.com/pydantic/pydantic/issues/6868 on MappingProxyType.# Used way too much energy to implement (and test) recursive frozen models, only to discover# that pydantic actually does not support MappingProxyType, which was hugely surprising.# Add tests on the immutability part once there is support for this.# Note: see comments in omnipy.modules.json.models for more information on the basic organisation of# nested models.# Note 2: currently crashes mypy (v1.10), probably due to recursive type aliases combined with# TypeVars from typing_extensions (with default). Not urgent to fix, as this in any case# awaits pydantic 2 and issue 6868 to be resolved. Keep out of __all__ until mypy issue# is fixed in order to avoid crashes with pytest-mypy-plugins, e.g. "test_json_types.yml".## Private models#class_FrozenScalarM(Model[ValT], Generic[ValT]):
_parse_data=NotIterableExceptStrOrBytesModel._parse_dataclass_FrozenScalarWithTwoParamsM(Model[ValT], Generic[KeyT, ValT]):
...
# class _FrozenTupleBaseM(Model[tuple[_FrozenBaseT, ...]], Generic[_FrozenBaseT]):# ...# class _FrozenDictBaseM(Model[FrozenDict[KeyT, _FrozenBaseT]], Generic[KeyT, _FrozenBaseT]):# ...class_FrozenTupleM(Model[tuple[DoubleTypeVarStore[KeyT, ValT]
|ForwardRef('_FrozenAnyUnion[KeyT, ValT]'),
...]],
Generic[KeyT, ValT]):
...
## class _FrozenDictM(_FrozenDictBaseM[KeyT, '_FrozenAnyUnion'], Generic[KeyT, ValT]):# ...class_FrozenDictM(Model[FrozenDict[KeyT, TypeVarStore[ValT]
|ForwardRef('_FrozenAnyUnion')]],
Generic[KeyT, ValT]):
defto_data(self) ->dict[KeyT, ValT]:
to_data=super().to_data()
returnMappingProxyType({key: val.to_data() forkey, valinto_data.items()})
class_FrozenNoDictsM(Model[tuple[TypeVarStore[ValT]
|ForwardRef('_FrozenNoDictsUnion[ValT]'), ...]],
Generic[ValT]):
...
# class _FrozenNoTuplesM(_FrozenDictBaseM['KeyT', '_FrozenNoTuplesUnion'], Generic[KeyT, ValT]):# ...class_FrozenNoTuplesM(Model[FrozenDict[KeyT,
DoubleTypeVarStore[KeyT, ValT]
|ForwardRef('_FrozenNoTuplesUnion')]],
Generic[KeyT, ValT]):
defto_data(self) ->dict[KeyT, ValT]:
to_data=super().to_data()
returnMappingProxyType({key: val.to_data() forkey, valinto_data.items()})
# TypeAliases_FrozenAnyUnion: TypeAlias= \
DoubleTypeVarStore[KeyT, ValT] |_FrozenScalarM[ValT] \
|_FrozenTupleM[KeyT, ValT] |_FrozenDictM[KeyT, ValT]
_FrozenTupleM.update_forward_refs()
_FrozenDictM.update_forward_refs()
_FrozenNoDictsUnion: TypeAlias=TypeVarStore[ValT] |_FrozenScalarM[ValT] |_FrozenNoDictsM[ValT]
_FrozenNoTuplesUnion: TypeAlias=DoubleTypeVarStore[
KeyT, ValT] |_FrozenScalarM[ValT] |_FrozenNoTuplesM[KeyT, ValT]
class_FrozenAnyUnionM(Model[_FrozenAnyUnion[KeyT, ValT]], Generic[KeyT, ValT]):
...
# Basic models needs to update their forward_refs with type aliases declared above_FrozenTupleM.update_forward_refs()
_FrozenDictM.update_forward_refs()
_FrozenAnyUnionM.update_forward_refs()
_FrozenNoDictsM.update_forward_refs()
_FrozenNoTuplesM.update_forward_refs()
## Exportable models#classNestedFrozenDictsOrTuplesModel(Model[_FrozenAnyUnionM[KeyT, ValT]], Generic[KeyT, ValT]):
""" Recursive model for nested immutable containers (FrozenDict and tuples). Not functional. Awaiting support for MappingProxyType in pydantic for the immutability to actually work. Also use as generic types awaits Python 3.13. Way ahead of the crowd here! """classNestedFrozenOnlyTuplesModel(Model[_FrozenNoDictsM[ValT]], Generic[ValT]):
""" Recursive model for nested tuples. Awaiting support for MappingProxyType in pydantic for the immutability to actually work. Also use as generic types awaits Python 3.13. Way ahead of the crowd here! """classNestedFrozenOnlyDictsModel(Model[_FrozenNoTuplesM[KeyT, ValT]], Generic[KeyT, ValT]):
""" Recursive model for nested FrozenDicts. Awaiting support for MappingProxyType in pydantic for the immutability to actually work. Also use as generic types awaits Python 3.13. Way ahead of the crowd here! """
The text was updated successfully, but these errors were encountered:
Used way too much energy to implement (and test) recursive frozen models, only to discover
that pydantic actually does not support MappingProxyType, which was hugely surprising.
Add tests on the immutability part once there is support for this.
nested models.
TypeVars from typing_extensions (with default). Not urgent to fix, as this in any case
awaits pydantic 2 and issue 6868 to be resolved. Keep out of __all__ until mypy issue
is fixed in order to avoid crashes with pytest-mypy-plugins, e.g. "test_json_types.yml".
...
...
...
...
omnipy/src/omnipy/modules/frozen/models.py
Line 11 in 86e1ee9
The text was updated successfully, but these errors were encountered: