Closed asamitov closed 1 month ago
I'm also struggling with the same regression.
@asamitov @forderud
Thank you for the bug report.
Python raises a SyntaxError
if proper syntax is not used, even within a TYPE_CHECKING
block that is not executed at runtime. This contrasts with the fact that NameError
or AttributeError
would not occur within a TYPE_CHECKING
block.
Type annotations are important for modern Python programming. However, we should avoid making this package non-functional.
I have rewritten typeannotator
so that if a Python reserved word is used as a member name, it is replaced with pass
and a comment. This should prevent SyntaxError
from occurring at runtime.
https://github.com/junkmd/comtypes/tree/avoid_keywords_named_methods_syntax_error (diff: https://github.com/enthought/comtypes/compare/b2da379...35f3336)
I do not have a COM type library to reproduce your situation, so please install the above changes in your environment and test it.
Also, if you have a method to reproduce the situation, please let me know.
Is it possible set option to disable
typing.TYPE_CHECKING
in the latest comtypes version?
That option is not provided in comtypes
.
There are no plans to implement the option at the moment.
Thank @junkmd for so fast response. I can confirm that the provided fix solves this issue.
https://github.com/junkmd/comtypes/tree/avoid_keywords_named_methods_syntax_error (diff: b2da379...35f3336)
I'm preparing a small reproducer for you. Will attach soon.
You're welcome. Thank you for your fast feedback as well.
I'm preparing a small reproducer for you. Will attach soon.
I greatly appreciate this offer.
I have come up with some ideas for unit tests to prevent regression.
I would like to review your reproducer and consider whether it can also be used for these tests.
Here is 7zip archive with precompiled binaries to reproduce: simple_bin.zip Python code to reproduce:
import comtypes.client
comtypes.client.gen_dir = None
comtypes.gen.__path__ = ['']
comtypes.client.GetModule("simpleax.tlb")
and source code to local build: simple_source.zip
Reproducer is based on https://doc.qt.io/qt-6/activeqt-activeqt-simple-example.html without code modification, only modified CMake to registrar the target as a post-build event and generate tlb
.
Note: I have to change file extension from 7z to zip to be able upload to github.
Here is 7zip archive with precompiled binaries to reproduce: simple_bin.zip Python code to reproduce:
import comtypes.client comtypes.client.gen_dir = None comtypes.gen.__path__ = [''] comtypes.client.GetModule("simpleax.tlb")
Thank you.
By using your reproducer, I confirmed that the error indeed occurs in my environment as well.
Applying my patch replaces the line where the error occurred with pass
and a comment, as shown below.
pass # avoid using a keyword for def raise(self) -> hints.Incomplete: ...
I am currently writing unit tests using typedesc
to prevent the regression.
I will submit a PR. @asamitov (and @forderud), any feedback to that PR would be appreciated (even just a reaction is okay).
Thank @junkmd for so rapid response.
Latest comtypes 1.4.4 and 1.4.5 fails on call comtypes.client.GetModule("some.tlb") if tlb is based on QWidget which contains the method https://doc.qt.io/qt-6/qwidget.html#raise
There is no issue on comtypes 1.1.7 where doesn't use
typing.TYPE_CHECKING
.Is it possible set option to disable
typing.TYPE_CHECKING
in the latest comtypes version?