lib.data: ignore Python typing annotations in aggregate base class.

This commit is contained in:
Catherine 2023-03-03 03:45:12 +00:00
parent 90b374c17a
commit c1b9c64e10
2 changed files with 20 additions and 3 deletions

View file

@ -407,12 +407,19 @@ class _AggregateMeta(ShapeCastable, type):
elif all(not hasattr(base, "_AggregateMeta__layout") for base in bases):
# This is a leaf class with its own layout. It is shape-castable and can
# be instantiated. It can also be subclassed, and used to share layout and behavior.
reset = dict()
for name in namespace["__annotations__"]:
layout = dict()
reset = dict()
for name in {**namespace["__annotations__"]}:
try:
Shape.cast(namespace["__annotations__"][name])
except TypeError:
# Not a shape-castable annotation; leave as-is.
continue
layout[name] = namespace["__annotations__"].pop(name)
if name in namespace:
reset[name] = namespace.pop(name)
cls = type.__new__(metacls, name, bases, namespace)
cls.__layout = cls.__layout_cls(namespace["__annotations__"])
cls.__layout = cls.__layout_cls(layout)
cls.__reset = reset
return cls
else:

View file

@ -639,6 +639,16 @@ class StructTestCase(FHDLTestCase):
class Sd(Sb):
b: 1
def test_typing_annotation_coexistence(self):
class S(Struct):
a: unsigned(1)
b: int
c: str = "x"
self.assertEqual(Layout.of(S()), StructLayout({"a": unsigned(1)}))
self.assertEqual(S.__annotations__, {"b": int, "c": str})
self.assertEqual(S.c, "x")
class UnionTestCase(FHDLTestCase):
def test_construct(self):