From b95169d86ff2837f3df814c7217be77b735ea917 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sat, 3 Oct 2020 20:22:43 -0400 Subject: [PATCH 1/8] fix `pyqtSlot` `result` parameter type and `Callable` generic --- CHANGELOG.md | 1 + PyQt5-stubs/QtCore.pyi | 4 ++-- tests/pyqtslot.py | 9 +++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adb4c64b..893363f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * [#51](https://github.com/stlehmann/PyQt5-stubs/pull/51) adds `pyqtBoundSignal.signal` hinted as `str` ### Changed +* [#](https://github.com/stlehmann/PyQt5-stubs/pull/) fix `pyqtSlot` `result` parameter type and `Callable` generic * [#92](https://github.com/stlehmann/PyQt5-stubs/pull/92) remove self from `qDefaultSurfaceFormat()` and `qIdForNode()` * [#83](https://github.com/stlehmann/PyQt5-stubs/pull/83) fixes `sip.array` to be generic * [#79](https://github.com/stlehmann/PyQt5-stubs/pull/79) fixes extra class layer in several modules diff --git a/PyQt5-stubs/QtCore.pyi b/PyQt5-stubs/QtCore.pyi index 3df7c2fa..75761425 100644 --- a/PyQt5-stubs/QtCore.pyi +++ b/PyQt5-stubs/QtCore.pyi @@ -9232,7 +9232,7 @@ QT_VERSION = ... # type: int QT_VERSION_STR = ... # type: str -FuncT = typing.TypeVar('FuncT', bound=typing.Callable) # For a correct pyqtSlot annotation +FuncT = typing.TypeVar('FuncT', bound=typing.Callable[..., typing.Any]) # For a correct pyqtSlot annotation def qSetRealNumberPrecision(precision: int) -> QTextStreamManipulator: ... @@ -9264,7 +9264,7 @@ def oct_(s: QTextStream) -> QTextStream: ... def bin_(s: QTextStream) -> QTextStream: ... def Q_RETURN_ARG(type: typing.Any) -> QGenericReturnArgument: ... def Q_ARG(type: typing.Any, data: typing.Any) -> QGenericArgument: ... -def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Optional[str] = ...) -> typing.Callable[[FuncT], FuncT]: ... +def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Type[object] = ...) -> typing.Callable[[FuncT], FuncT]: ... def QT_TRANSLATE_NOOP(a0: str, a1: str) -> str: ... def QT_TR_NOOP_UTF8(a0: str) -> str: ... def QT_TR_NOOP(a0: str) -> str: ... diff --git a/tests/pyqtslot.py b/tests/pyqtslot.py index ed53b7a3..13509da4 100644 --- a/tests/pyqtslot.py +++ b/tests/pyqtslot.py @@ -1,7 +1,12 @@ from PyQt5.QtCore import pyqtSlot @pyqtSlot(str) -def func(s: str) -> int: +def func_none(s: str) -> None: + return + +@pyqtSlot(str, result=int) +def func_int(s: str) -> int: return 42 -func("test") +func_none("test") +x = func_int("test") # type: int From 7c86c3c1f96e51dcc23ffc01a6cb952e83bb63f9 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sat, 3 Oct 2020 20:24:45 -0400 Subject: [PATCH 2/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 893363f6..49cccdcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * [#51](https://github.com/stlehmann/PyQt5-stubs/pull/51) adds `pyqtBoundSignal.signal` hinted as `str` ### Changed -* [#](https://github.com/stlehmann/PyQt5-stubs/pull/) fix `pyqtSlot` `result` parameter type and `Callable` generic +* [#102](https://github.com/stlehmann/PyQt5-stubs/pull/102) fix `pyqtSlot` `result` parameter type and `Callable` generic * [#92](https://github.com/stlehmann/PyQt5-stubs/pull/92) remove self from `qDefaultSurfaceFormat()` and `qIdForNode()` * [#83](https://github.com/stlehmann/PyQt5-stubs/pull/83) fixes `sip.array` to be generic * [#79](https://github.com/stlehmann/PyQt5-stubs/pull/79) fixes extra class layer in several modules From 383e4f9efda83afac5df0663282cab95dacfd8e3 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sun, 4 Oct 2020 09:19:36 -0400 Subject: [PATCH 3/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49cccdcb..a2e3d029 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * [#51](https://github.com/stlehmann/PyQt5-stubs/pull/51) adds `pyqtBoundSignal.signal` hinted as `str` ### Changed -* [#102](https://github.com/stlehmann/PyQt5-stubs/pull/102) fix `pyqtSlot` `result` parameter type and `Callable` generic +* [#102](https://github.com/stlehmann/PyQt5-stubs/pull/102) fix `pyqtSlot` `result` parameter type to `typing.Type[object]` * [#92](https://github.com/stlehmann/PyQt5-stubs/pull/92) remove self from `qDefaultSurfaceFormat()` and `qIdForNode()` * [#83](https://github.com/stlehmann/PyQt5-stubs/pull/83) fixes `sip.array` to be generic * [#79](https://github.com/stlehmann/PyQt5-stubs/pull/79) fixes extra class layer in several modules From 4516100c53025761a723a2f9881529d53d9f441c Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sun, 4 Oct 2020 13:55:23 -0400 Subject: [PATCH 4/8] Update QtCore.pyi --- PyQt5-stubs/QtCore.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PyQt5-stubs/QtCore.pyi b/PyQt5-stubs/QtCore.pyi index e45df8a5..3eb9bff6 100644 --- a/PyQt5-stubs/QtCore.pyi +++ b/PyQt5-stubs/QtCore.pyi @@ -9264,7 +9264,7 @@ def oct_(s: QTextStream) -> QTextStream: ... def bin_(s: QTextStream) -> QTextStream: ... def Q_RETURN_ARG(type: typing.Any) -> QGenericReturnArgument: ... def Q_ARG(type: typing.Any, data: typing.Any) -> QGenericArgument: ... -def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Type[object] = ...) -> typing.Callable[[FuncT], FuncT]: ... +def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Any = ...) -> typing.Callable[[FuncT], FuncT]: ... def QT_TRANSLATE_NOOP(a0: str, a1: str) -> str: ... def QT_TR_NOOP_UTF8(a0: str) -> str: ... def QT_TR_NOOP(a0: str) -> str: ... From 93576bbf1a9dcb7b23ddd4704b49d0644be1b00e Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Sun, 4 Oct 2020 15:18:18 -0400 Subject: [PATCH 5/8] Add revision parameter to pyqtSlot --- PyQt5-stubs/QtCore.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PyQt5-stubs/QtCore.pyi b/PyQt5-stubs/QtCore.pyi index 3eb9bff6..a5508661 100644 --- a/PyQt5-stubs/QtCore.pyi +++ b/PyQt5-stubs/QtCore.pyi @@ -9264,7 +9264,7 @@ def oct_(s: QTextStream) -> QTextStream: ... def bin_(s: QTextStream) -> QTextStream: ... def Q_RETURN_ARG(type: typing.Any) -> QGenericReturnArgument: ... def Q_ARG(type: typing.Any, data: typing.Any) -> QGenericArgument: ... -def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Any = ...) -> typing.Callable[[FuncT], FuncT]: ... +def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Any = ..., revision: int = ...) -> typing.Callable[[FuncT], FuncT]: ... def QT_TRANSLATE_NOOP(a0: str, a1: str) -> str: ... def QT_TR_NOOP_UTF8(a0: str) -> str: ... def QT_TR_NOOP(a0: str) -> str: ... From 0d9fd8ce363e3c7170d4ec1e052c54eaf32e83af Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Tue, 6 Oct 2020 22:40:34 -0400 Subject: [PATCH 6/8] alias over typevar --- PyQt5-stubs/QtCore.pyi | 10 ++++++++-- tests/pyqtslot.py | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/PyQt5-stubs/QtCore.pyi b/PyQt5-stubs/QtCore.pyi index 162457f0..503504b0 100644 --- a/PyQt5-stubs/QtCore.pyi +++ b/PyQt5-stubs/QtCore.pyi @@ -9235,7 +9235,8 @@ QT_VERSION = ... # type: int QT_VERSION_STR = ... # type: str -FuncT = typing.TypeVar('FuncT', bound=typing.Callable[..., typing.Any]) # For a correct pyqtSlot annotation +T = typing.TypeVar("T") +FuncT = typing.Callable[..., T] def qSetRealNumberPrecision(precision: int) -> QTextStreamManipulator: ... @@ -9267,7 +9268,12 @@ def oct_(s: QTextStream) -> QTextStream: ... def bin_(s: QTextStream) -> QTextStream: ... def Q_RETURN_ARG(type: typing.Any) -> QGenericReturnArgument: ... def Q_ARG(type: typing.Any, data: typing.Any) -> QGenericArgument: ... -def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Any = ..., revision: int = ...) -> typing.Callable[[FuncT], FuncT]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., revision: int = ...) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: str = ..., revision: int = ...) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Type[T] = ..., revision: int = ...) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... def QT_TRANSLATE_NOOP(a0: str, a1: str) -> str: ... def QT_TR_NOOP_UTF8(a0: str) -> str: ... def QT_TR_NOOP(a0: str) -> str: ... diff --git a/tests/pyqtslot.py b/tests/pyqtslot.py index 13509da4..973bcf5b 100644 --- a/tests/pyqtslot.py +++ b/tests/pyqtslot.py @@ -8,5 +8,9 @@ def func_none(s: str) -> None: def func_int(s: str) -> int: return 42 +@pyqtSlot(str, result='int') +def func_str(s: str) -> str: + return '42' + func_none("test") x = func_int("test") # type: int From 097218f57e6c1a7a6ba62a473b3c936942348bee Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 7 Oct 2020 09:20:42 -0400 Subject: [PATCH 7/8] expand pyqtSlot overloads instead of inaccurate Optional --- PyQt5-stubs/QtCore.pyi | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/PyQt5-stubs/QtCore.pyi b/PyQt5-stubs/QtCore.pyi index 503504b0..574deeb4 100644 --- a/PyQt5-stubs/QtCore.pyi +++ b/PyQt5-stubs/QtCore.pyi @@ -9269,11 +9269,29 @@ def bin_(s: QTextStream) -> QTextStream: ... def Q_RETURN_ARG(type: typing.Any) -> QGenericReturnArgument: ... def Q_ARG(type: typing.Any, data: typing.Any) -> QGenericArgument: ... @typing.overload -def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., revision: int = ...) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +def pyqtSlot(*types: typing.Any) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... @typing.overload -def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: str = ..., revision: int = ...) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +def pyqtSlot(*types: typing.Any, name: str) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... @typing.overload -def pyqtSlot(*types: typing.Any, name: typing.Optional[str] = ..., result: typing.Type[T] = ..., revision: int = ...) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +def pyqtSlot(*types: typing.Any, result: typing.Type[T]) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, result: str) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, revision: int) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: str, result: typing.Type[T]) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: str, result: str) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: str, revision: int) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, result: typing.Type[T], revision: int) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, result: str, revision: int) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: str, result: typing.Type[T], revision: int) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... +@typing.overload +def pyqtSlot(*types: typing.Any, name: str, result: str, revision: int) -> typing.Callable[[FuncT[T]], FuncT[T]]: ... def QT_TRANSLATE_NOOP(a0: str, a1: str) -> str: ... def QT_TR_NOOP_UTF8(a0: str) -> str: ... def QT_TR_NOOP(a0: str) -> str: ... From bf51e07703eef88542fe6e944740ba1250594c89 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 7 Oct 2020 14:34:53 -0400 Subject: [PATCH 8/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c44d790..f052a1ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * [#51](https://github.com/stlehmann/PyQt5-stubs/pull/51) adds `pyqtBoundSignal.signal` hinted as `str` ### Changed -* [#102](https://github.com/stlehmann/PyQt5-stubs/pull/102) fix `pyqtSlot` `result` parameter type to `typing.Type[object]` +* [#102](https://github.com/stlehmann/PyQt5-stubs/pull/102) fix `pyqtSlot` parameter typing and overloads * [#104](https://github.com/stlehmann/PyQt5-stubs/pull/104) `sip.voidptr` handles integer values and sequences and takes `self` * [#103](https://github.com/stlehmann/PyQt5-stubs/pull/103) `pyqtBoundSignal.disconnect()`'s `slot` parameter is optional * [#100](https://github.com/stlehmann/PyQt5-stubs/pull/100) fill generic parameter as `sip.array[int]` for QtDataVisualization textures