I am using COM Objects within python to expose programmable interfaces to 3rd party software. This is achieved through using Dispatch from win32com.client. My project has also been making use of type hinting from python.3.7, however I am unsure as to how you would define the type of these COM objects for the purposes of type hinting. The question relates to all the COM objects I have, with a real example being a Microsoft Direct X Recordset: Dispatch("ADODB.Recordset").
from win32com.client import Dispatch
def create_my_com_object_instance(input_arg: Dict[str, Union[str, float, int]]) -> <type_of_com_object_here>:
my_instance = Dispatch("ADODB.Recordset")
# Set attributes/call methods of this instance here ...
return my_instance
In the above code snippet I would replace 'type_of_com_object_here' with the COM object type.
My first thought was just to call type() on the instance and use the returned type:
x = Dispatch("ADODB.Recordset")
x
Out[1]: <win32com.gen_py.Microsoft ActiveX Data Objects 6.1 Library._Recordset instance at 0x83848456>
type(x)
Out[2]: win32com.gen_py.B691E011-1797-432E-907A-4D8C69339129x0x6x1._Recordset._Recordset
x.__class__
Out[3]: win32com.gen_py.B691E011-1797-432E-907A-4D8C69339129x0x6x1._Recordset._Recordset
This does not return a suitable type for defining the COM Object. I believe that I could create an abstract base class using TypeVar('T') and Generic[], however I am unsure if there is a more pythonic/better alternative available.
Thanks