Unfortunately, this isn’t well supported by Python’s type system. What you’d need is an intersection type to communicate that the return value of datastructclass
is compatible both with A
and with some protocol that describes the added unpack
/pack
functionality. Intersection types currently can’t be expressed in type annotations (though some type checkers support them internally).
As far as convincing mypy to make this work, you may have luck with implementing a mypy plugin for your package.
See also this related thread for more on adding attributes with a decorator and intersection types (edit: d’oh, I just noticed you were the OP of that thread as well ):