comtypes icon indicating copy to clipboard operation
comtypes copied to clipboard

prevent `itf` to be an unbound variable in `_cominterface_meta.__get_baseinterface_methodcount`

Open junkmd opened this issue 2 years ago • 0 comments

Since itf can be an unbound variable, the following code will cause several type checkers to warn.

https://github.com/enthought/comtypes/blob/1615af5b56670cc811e70403dd6762a49f6fe7d1/comtypes/init.py#L513-L524

But this try...except... clause traps a KeyError.

Therefore, it is possible to change to a better code by verifying the existence of the key.

        ...
            if "_methods_" in itf.__dict__:
                ...
            else:
                raise TypeError(f"baseinterface '{itf.__name__}' has no _methods_")
        ...

Maybe vars(itf) is better than itf.__dict__, or maybe there is another way to write it better, but I'll leave it to the contributors for fixing this.

junkmd avatar Jan 16 '23 13:01 junkmd