Closed
Description
Since itf
can be an unbound variable, the following code will cause several type checkers to warn.
comtypes/comtypes/_post_coinit/unknwn.py
Lines 325 to 336 in 97cbe1c
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.
- try:
- result = 0
- for itf in self.mro()[1:-1]:
+ result = 0
+ for itf in self.mro()[1:-1]:
+ if "_methods_" in itf.__dict__:
result += len(itf.__dict__["_methods_"])
- return result
- except KeyError as err:
- (name,) = err.args
- if name == "_methods_":
- raise TypeError("baseinterface '%s' has no _methods_" % itf.__name__)
- raise
+ else:
+ raise TypeError(f"baseinterface '{itf.__name__}' has no _methods_")
+ return result
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.