lib.data: accept data.Const in *.const().

This commit is contained in:
Wanda 2024-04-11 01:48:53 +02:00 committed by Catherine
parent cf534489a2
commit 0be2dda656
2 changed files with 30 additions and 0 deletions

View file

@ -208,6 +208,11 @@ class Layout(ShapeCastable, metaclass=ABCMeta):
an all-zero value and had every field assigned to the corresponding value in the order
in which they appear in :py:`init`.
"""
if isinstance(init, Const):
if Layout.cast(init.shape()) != self:
raise ValueError(f"Const layout {init.shape()!r} differs from shape layout "
f"{self!r}")
return init
if init is None:
iterator = iter(())
elif isinstance(init, Mapping):
@ -1139,6 +1144,11 @@ class _AggregateMeta(ShapeCastable, type):
return super().__call__(cls, target)
def const(cls, init):
if isinstance(init, Const):
if Layout.cast(init.shape()) != Layout.cast(cls.__layout):
raise ValueError(f"Const layout {init.shape()!r} differs from shape layout "
f"{cls.__layout!r}")
return init
if cls.__layout_cls is UnionLayout:
if init is not None and len(init) > 1:
raise ValueError("Initializer for at most one field can be provided for "