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

fix CoCreateInstance type annotation #386

Closed
junkmd opened this issue Nov 28, 2022 · 3 comments · Fixed by #395
Closed

fix CoCreateInstance type annotation #386

junkmd opened this issue Nov 28, 2022 · 3 comments · Fixed by #395
Labels
good first issue Good for newcomers

Comments

@junkmd
Copy link
Collaborator

junkmd commented Nov 28, 2022

CoCreateInstanceEx is annotated with a return value based on the type variable passed as an argument.

comtypes/comtypes/__init__.py

Lines 1095 to 1099 in 0f3cf2b

@overload
def CoCreateInstanceEx(
clsid, interface=None, clsctx=None, machine=None, pServerInfo=None):
# type: (GUID, Type[_T_IUnknown], Optional[int], Optional[str], Optional[COSERVERINFO]) -> _T_IUnknown
pass

However, CoCreateInstance does not.

@overload
def CoCreateInstance(clsid, interface, clsctx=None, punkouter=None):
# type: (GUID, Type[_T_IUnknown], Optional[int], Optional[pUnkOuter]) -> IUnknown
pass

Please fix as follows.

    @overload
    def CoCreateInstance(clsid, interface, clsctx=None, punkouter=None):
        # type: (GUID, Type[_T_IUnknown], Optional[int], Optional[pUnkOuter]) -> _T_IUnknown
        pass
@junkmd junkmd added the good first issue Good for newcomers label Nov 28, 2022
@junkmd
Copy link
Collaborator Author

junkmd commented Dec 6, 2022

The type hinting related enhancements related to #327 are moving toward inline annotations(not comments), and it is planned to develop on a branch that is based on Python3-only support.

I will assign this issue to myself to work on this to prevent a new branch from being created from master with the wrong annotation.

@junkmd junkmd linked a pull request Dec 6, 2022 that will close this issue
@kdschlosser
Copy link
Contributor

I believe that all COM interfaces have IUnknown as a base class. So if you are type hinting the return value type hint it as IUnknown.

@junkmd
Copy link
Collaborator Author

junkmd commented Dec 7, 2022

_T_IUnknown is the IUnknown-bound type variable.

_T_IUnknown = TypeVar("_T_IUnknown", bound="IUnknown")

And this "serve as the parameters for generic function definitions".

# they are examples of the type analyzer behavior. In runtime, they are not available.
a = CoCreateInstance(GUID(), IUnknown)  # `a` is recognized as `IUnknown` instance by type analyzer.
b = CoCreateInstance(GUID(), IPersist)  # `b` is recognized as `IPersist` instance by type analyzer.
c = CoCreateInstance(GUID(), IServiceProvider)  # `c` is recognized as `IServiceProvider` instance by type analyzer.

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

Successfully merging a pull request may close this issue.

2 participants