Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent itf to be an unbound variable in _cominterface_meta.__get_baseinterface_methodcount #472

Open
junkmd opened this issue Jan 16, 2023 · 0 comments
Labels
good first issue Good for newcomers

Comments

@junkmd
Copy link
Collaborator

junkmd commented Jan 16, 2023

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

def __get_baseinterface_methodcount(self):
"Return the number of com methods in the base interfaces"
try:
result = 0
for itf in self.mro()[1:-1]:
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

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 junkmd added good first issue Good for newcomers drop_py2 dev based on supporting only Python3, see #392 labels Jan 16, 2023
@junkmd junkmd removed the drop_py2 dev based on supporting only Python3, see #392 label Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant