Using amaranth.lib.data Signatures with typing comes with some boilerplate and repetition :(
I came up with a class that can be used both for typing and to get amaranth Signature for use in Component constructor.
It works by crating a class with IOSignal fields declared as self. in constructor, so it can be directly for typechecking. IOSignal type derives Value with additonal shape and direction information.
IOInterface is never used globally, but is intended only for interface typing. Those signals will be filled with actual objects at super().__init__(...Component constructor (that gets Signature from generated IOInterface.signature). Nested IOInterfaces are supported.
The boilerplate reduction is nice, but this is a little bit hacky. Maybe, instead of a custom amaranth.Value subclass, typing.Annotated could be used to store the same information?
Using
amaranth.lib.data
Signatures with typing comes with some boilerplate and repetition :(I came up with a class that can be used both for typing and to get amaranth
Signature
for use inComponent
constructor.It works by crating a class with
IOSignal
fields declared asself.
in constructor, so it can be directly for typechecking.IOSignal
type derivesValue
with additonal shape and direction information.IOInterface
is never used globally, but is intended only for interface typing. Those signals will be filled with actual objects atsuper().__init__(...
Component
constructor (that gets Signature from generatedIOInterface.signature
). NestedIOInterfaces
are supported.It is a bit hacky, but what do you think?