Skip to content

Type annotation test #605

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ repos:
files: ^(HOWTORELEASE.rst|README.rst)$
language: python
additional_dependencies: [pygments, restructuredtext_lint]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.15.0'
hooks:
- id: mypy
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
# 'preamble': '',
}
} # type: dict[str, str]

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
Expand Down
29 changes: 21 additions & 8 deletions src/pytestqt/qtbot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import weakref
import warnings
from typing import TYPE_CHECKING, Callable, Optional

from pytestqt.exceptions import TimeoutError, ScreenshotError
from pytestqt.qt_compat import qt_api
Expand All @@ -13,6 +14,11 @@
CallbackCalledTwiceError,
)

if TYPE_CHECKING:
from qtpy.QtWidgets import QWidget
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does qtpy provide useful types?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can provide useful types for Qt classes for now... as commented in #417 (comment), I would focus on typing non-Qt things for now and getting the infrastructure in place.


BeforeCloseFunc = Callable[["QWidget"], None]


def _parse_ini_boolean(value):
if value in (True, False):
Expand Down Expand Up @@ -170,7 +176,9 @@ def _should_raise(self, raising_arg):
else:
return True

def addWidget(self, widget, *, before_close_func=None):
def addWidget(
self, widget: "QWidget", *, before_close_func: Optional[BeforeCloseFunc] = None
):
"""
Adds a widget to be tracked by this bot. This is not required, but will ensure that the
widget gets closed by the end of the test, so it is highly recommended.
Expand Down Expand Up @@ -725,13 +733,18 @@ def mouseRelease(*args, **kwargs):


# provide easy access to exceptions to qtbot fixtures
QtBot.SignalEmittedError = SignalEmittedError
QtBot.TimeoutError = TimeoutError
QtBot.ScreenshotError = ScreenshotError
QtBot.CallbackCalledTwiceError = CallbackCalledTwiceError


def _add_widget(item, widget, *, before_close_func=None):
QtBot.SignalEmittedError = SignalEmittedError # type: ignore[attr-defined]
QtBot.TimeoutError = TimeoutError # type: ignore[attr-defined]
QtBot.ScreenshotError = ScreenshotError # type: ignore[attr-defined]
QtBot.CallbackCalledTwiceError = CallbackCalledTwiceError # type: ignore[attr-defined]


def _add_widget(
item,
widget: "QWidget",
*,
before_close_func: Optional[BeforeCloseFunc] = None,
):
"""
Register a widget into the given pytest item for later closing.
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_modeltest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
pytestmark = pytest.mark.usefixtures("qtbot")


class BasicModel(qt_api.QtCore.QAbstractItemModel):
class BasicModel(qt_api.QtCore.QAbstractItemModel): # type: ignore[name-defined]
def data(self, index, role=qt_api.QtCore.Qt.ItemDataRole.DisplayRole):
return None

Expand Down