core.skeleton.instance

Attributes

Skeleton_Cls

Classes

SkeletonInstance

The actual wrapper around a Skeleton-Class.

Module Contents

core.skeleton.instance.Skeleton_Cls
class core.skeleton.instance.SkeletonInstance(skel_cls, entity=None, *, bones=(), bone_map=None, clone=False, clonedBoneMap=None)

Bases: Generic[core.skeleton.meta.Skeleton_Cls]

The actual wrapper around a Skeleton-Class.

An object of this class is what’s actually returned when you call a Skeleton-Class. With ViUR3, you don’t get an instance of a Skeleton-Class any more - it’s always this class. This is much faster as this is a small class.

The class is generic over Skeleton_Cls, which lets the type checker track which concrete Skeleton subclass a given instance belongs to. Without the type parameter the class still works exactly as before — the parameter is purely a static-analysis hint.

Basic usage

Calling a Skeleton class returns a typed SkeletonInstance:

skel = ProductSkel()          # -> SkeletonInstance[ProductSkel]
skel.skeletonCls              # ProductSkel
skel["price"]                 # type-safe bone access

Typed module method

Override viewSkel / editSkel etc. in your module with an explicit return type so that callers and IDE auto-complete know which bones are available:

class ProductModule(List):
    def editSkel(self) -> SkeletonInstance[ProductSkel]:
        skel = super().editSkel()
        skel.price.readOnly = True
        return skel

Generic helper

Use Skeleton_Cls when writing utilities that must stay agnostic about the concrete skeleton but still preserve the type through the call:

def set_owner(skel: SkeletonInstance[Skeleton_Cls], owner: str) -> SkeletonInstance[Skeleton_Cls]:
    skel = skel.clone()
    skel["owner"] = owner
    return skel  # type checker keeps SkeletonInstance[ProductSkel] etc.

Classmethod signatures (t.Self)

Inside Skeleton classmethods, t.Self is preferred over Skeleton_Cls because the type checker automatically narrows to the class the method is called on:

class BaseSkeleton:
    @classmethod
    def fromClient(cls, skel: SkeletonInstance[t.Self], data: dict) -> bool: ...

# Inferred as SkeletonInstance[ProductSkel] when called on ProductSkel
ProductSkel.fromClient(skel, request.POST)

Creates a new SkeletonInstance based on skel_cls.

Parameters:
  • skel_cls (Type[core.skeleton.meta.Skeleton_Cls]) – Is the base skeleton class to inherit from and reference to.

  • bones (Iterable[str]) – If given, defines an iterable of bones that are take into the SkeletonInstance. The order of the bones defines the order in the SkeletonInstance.

  • bone_map (Optional[Dict[str, core.bones.base.BaseBone]]) – A pre-defined bone map to use, or extend.

  • clone (bool) – If set True, performs a cloning of the used bone map, to be entirely stand-alone.

  • entity (Optional[viur.core.db.Entity | dict])

  • clonedBoneMap (Optional[Dict[str, core.bones.base.BaseBone]])

__slots__
_cascade_deletion = False
accessedValues
dbEntity = None
errors = []
is_cloned = False
renderAccessedValues
renderPreparation = None
skeletonCls: Type[core.skeleton.meta.Skeleton_Cls]
items(yieldBoneValues=False)
Parameters:

yieldBoneValues (bool)

Return type:

Iterable[tuple[str, core.bones.base.BaseBone]]

keys()
Return type:

Iterable[str]

values()
Return type:

Iterable[Any]

__iter__()
Return type:

Iterable[str]

__contains__(item)
__bool__()
get(item, default=None)
update(*args, **kwargs)
Return type:

None

__setitem__(key, value)
__getitem__(key)
__getattr__(item)

Get a special attribute from the SkeletonInstance

__getattr__ is called when an attribute access fails with an AttributeError. So we know that this is not a real attribute of the SkeletonInstance. But there are still a few special cases in which attributes are loaded from the skeleton class.

Parameters:

item (str)

__delattr__(item)
__setattr__(key, value)
__repr__()
Return type:

str

__str__()
Return type:

str

__len__()
Return type:

int

__ior__(other)
Parameters:

other (dict | SkeletonInstance | viur.core.db.Entity)

Return type:

SkeletonInstance

clone(*, apply_clone_strategy=False)

Clones a SkeletonInstance into a modificable, stand-alone instance. This will also allow to modify the underlying data model.

Parameters:

apply_clone_strategy (bool)

Return type:

Self

ensure_is_cloned()

Ensured this SkeletonInstance is a stand-alone clone, which can be modified. Does nothing in case it was already cloned before.

setEntity(entity)
Parameters:

entity (viur.core.db.Entity)

structure()
Return type:

dict

dump()

Return a JSON-serializable version of the bone values in this skeleton.

The function is not called “to_json()” because the JSON-serializable format can be used for different purposes and renderings, not just JSON.

__deepcopy__(memodict)