-
Notifications
You must be signed in to change notification settings - Fork 799
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
Ensure disp_class
cannot be None
in DispatchWithEvents
and WithEvents
#2322
base: main
Are you sure you want to change the base?
Conversation
com/win32com/client/__init__.py
Outdated
else: | ||
disp_class = disp.__class__ | ||
if disp_class is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! This could all still stay in that inner if
block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically yes, but neither mypy nor pyright seem to see that disp_class
cannot be None
if I raise after the try-except in the if
branch.
Some likely relevant issues:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deduplicating code between DispatchWithEvents and WithEvents resolved this anyway
…tchWithEvents-None-disp_class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
damn! But fair enough I guess...
disp_class
cannot be None
in DispatchWithEvents
disp_class
cannot be None
in DispatchWithEvents
and WithEvents
I noticed the code was near-identical in another function and that this improvement could apply to it as well. So I combined the code in a different method which also resolves the type-narrowing issue. Please let me know if this change suits you. |
from __future__ import annotations | ||
|
||
mapCLSIDToClass: dict[str, type] = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary for this change. It just helped be make sense of, and validate the type narrowing.
If
gencache.GetClassForProgID
returnsNone
, the error message is now a clearerTypeError
instead of the previous genericAttributeError: 'NoneType' object has no attribute 'CLSID'