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

Various stub improvements and Pyright support #8

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

arvidfm
Copy link

@arvidfm arvidfm commented Sep 22, 2023

Hello, this PR contains a number of stub improvements along with support for running tests against Pyright. Additionally, the PR drops support for Python 3.6 and 3.7 as they are now EOL, and newer versions of Pyright don't support them (and older versions seem to have issues type checking the tests properly). Alternatively we could keep Python 3.6 support, but simply not run the tests against Pyright for versions prior to 3.8; let me know which you would prefer.

I also updated the workflows and contributing guidelines to use pip install -e . --config-settings editable_mode=strict for installing the stubs in editable mode. This option is a workaround for a change in how recent versions of setuptools do editable installs, see related Mypy issue. (Would there be interest in moving to Poetry instead to not have to deal with this? Poetry would make dealing with development dependencies and juggling multiple Python versions easier too.)

Additionally, I removed PySide2 as a hard dependency, as it's not technically needed for type checking, and the user may have their own PySide2 distribution they'd like to use rather than one fetched from PyPI.

The concrete changes to the stubs are as follows:

  • Type hint QtCore.Property as a descriptor, allowing reading and writing to properties to type check correctly
    • Though this is a much more practically useful version of the stub, it's not technically 100% accurate - Property is not actually a descriptor; the magic of assigning to and reading from properties happens in QObject's __getattribute__ and __setattr__. However, I can't think of a way that we could possibly type hint __getattribute__(self, name) to say that "if name corresponds to a Property[T], return a value of type T".
  • Make most window flag arguments accept both Qt.WindowType and Qt.WindowFlags
  • Fix qmlRegisterType arguments to be str instead of bytes (same issue as with property/setProperty)
  • Add proper type hinting for QEnum and QFlag for use as class decorators
    • The lack of type hints here wasn't actually pointed out by Mypy, at least not when not using strict mode, so this is one test where Pyright was useful for additional coverage
  • Add shiboken2 stubs using this XML as a reference (as well as the PySide6 stubs)
  • Fix a typo in the definition of QObjectT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant