I’m building type hints for some code that we have internally, but a section of it is dynamic at run-time. I’m trying to make it so that the users can see the expected class functions in advance to help with development, but without forcing them to do a lot of extra work.
For example, given this generic example:
target_dict = {"Example1": {"fcn1":fcn1, "fcn2":fcn2},
"Example2": {"fcn3":fcn3, "fcn4":fcn4}}
class Container:
def __init__(self, target: str):
for fcn_name, fcn in target_dict[target].items():
setattr(self, fcn_name, fcn)
def container_fcn(self):
pass
with the caller of:
x = Container("Example1")
y = Container("Example2")
With this, x will be a Container class with fcn1 and fcn2 available, while y will be a Container class with fcn3 and fcn4.
What I want to accomplish to allow typing tools to see this dynamic functionality, so that my user can type:
x = Container("Example1")
x.
and see the options of .container_fcn()
, .fcn1()
and .fcn2()
available as auto-completes/hints.
It’s important to note that these are basically interfaces for actual classes, so even though I’ve defined a dictionary here, there is an actual backing class for these, but we are intentionally not trying to pull them in as subclasses. This means that even though I’ve defined them as dict entries, I do have access to the original class for things like type hints.
This means that I can currently do this to accomplish what I want:
x: Union[Container, Example1]
y: Union[Container, Example2]
What I’m trying to figure out is if it’s possible to skip this extra step and allow the typing libraries to use the string value in the initializer as a class declaration.
Really this question boils down to the capabilities defined in stuff like PEP483, PEP484, etc. We’ve got some flexibility here, so we can add notations in different places if it can facilitate this feature. Worst case, I just fall back on the Union
notation, but the less stuff my end users have to remember, the better.