I'm trying to mask some functions as properties (through a wrapper which is not important here) and add them to the object dynamically, however, I need code completion and mypy to work.
I figured out how to add a property dynamically (either through a metaclass or simply in constructor), but the problem I have is mypy doesn't pick it up (and neither does the IDE).
One workaround is to define an attribute with the same name/type, but I really don't like this approach (too much code, static set of attributes, repetition).
Is there a better way?
class Meta(type):def __new__(cls, clsname, bases, dct):def prop(self) -> int:return 1inst = super(Meta, cls).__new__(cls, clsname, bases, dct)inst.dynprop=property(prop)return instclass Foo(metaclass=Meta):dynprop=int #this works, but I don't want itclass Bar(metaclass=Meta):passdef somefunc(s:str):print(s)foo=Foo()
bar=Bar()
somefunc(foo.dynprop) #this is ok
somefunc(bar.dynprop) #meta.py:24: error: "Bar" has no attribute "dynprop"