If you want type checkers to be able to recognize that the return type is both compatible with the passed class as well as with HasNewAttrib
, what you’d need is an intersection type. This is unfortunately a well known missing feature in Python’s type system. I think the best you can do for now is a union.
You can find some more info on the status of intersection types in links contained in this comment:
For more inspiration, you can take a look at how @dataclass
is type hinted in typeshed. Note that even though @dataclass
adds an attribute (__dataclass_fields__
), its type hints can’t express that: