Skip to content

Commit 426bbb5

Browse files
committed
Refactor Qt typing
1 parent 5373311 commit 426bbb5

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

Tests/test_qt_image_qapplication.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
4-
from typing import Union
5-
63
import pytest
74

85
from PIL import Image, ImageQt
@@ -11,18 +8,8 @@
118

129
TYPE_CHECKING = False
1310
if TYPE_CHECKING:
14-
import PyQt6
15-
import PySide6
16-
17-
QApplication = Union[PyQt6.QtWidgets.QApplication, PySide6.QtWidgets.QApplication]
18-
QHBoxLayout = Union[PyQt6.QtWidgets.QHBoxLayout, PySide6.QtWidgets.QHBoxLayout]
19-
QImage = Union[PyQt6.QtGui.QImage, PySide6.QtGui.QImage]
20-
QLabel = Union[PyQt6.QtWidgets.QLabel, PySide6.QtWidgets.QLabel]
21-
QPainter = Union[PyQt6.QtGui.QPainter, PySide6.QtGui.QPainter]
22-
QPixmap = Union[PyQt6.QtGui.QPixmap, PySide6.QtGui.QPixmap]
23-
QPoint = Union[PyQt6.QtCore.QPoint, PySide6.QtCore.QPoint]
24-
QRegion = Union[PyQt6.QtGui.QRegion, PySide6.QtGui.QRegion]
25-
QWidget = Union[PyQt6.QtWidgets.QWidget, PySide6.QtWidgets.QWidget]
11+
from pathlib import Path
12+
2613

2714
if ImageQt.qt_is_installed:
2815
from PIL.ImageQt import QPixmap
@@ -32,11 +19,16 @@
3219
from PyQt6.QtGui import QImage, QPainter, QRegion
3320
from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
3421
elif ImageQt.qt_version == "side6":
35-
from PySide6.QtCore import QPoint
36-
from PySide6.QtGui import QImage, QPainter, QRegion
37-
from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
38-
39-
class Example(QWidget): # type: ignore[misc]
22+
from PySide6.QtCore import QPoint # type: ignore[assignment]
23+
from PySide6.QtGui import QImage, QPainter, QRegion # type: ignore[assignment]
24+
from PySide6.QtWidgets import ( # type: ignore[assignment]
25+
QApplication,
26+
QHBoxLayout,
27+
QLabel,
28+
QWidget,
29+
)
30+
31+
class Example(QWidget):
4032
def __init__(self) -> None:
4133
super().__init__()
4234

@@ -47,9 +39,9 @@ def __init__(self) -> None:
4739
pixmap1 = getattr(ImageQt.QPixmap, "fromImage")(qimage)
4840

4941
# hbox
50-
QHBoxLayout(self) # type: ignore[operator]
42+
QHBoxLayout(self)
5143

52-
lbl = QLabel(self) # type: ignore[operator]
44+
lbl = QLabel(self)
5345
# Segfault in the problem
5446
lbl.setPixmap(pixmap1.copy())
5547

@@ -63,7 +55,7 @@ def roundtrip(expected: Image.Image) -> None:
6355
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
6456
def test_sanity(tmp_path: Path) -> None:
6557
# Segfault test
66-
app: QApplication | None = QApplication([]) # type: ignore[operator]
58+
app: QApplication | None = QApplication([])
6759
ex = Example()
6860
assert app # Silence warning
6961
assert ex # Silence warning
@@ -84,11 +76,11 @@ def test_sanity(tmp_path: Path) -> None:
8476
imageqt = ImageQt.ImageQt(im)
8577
data = getattr(QPixmap, "fromImage")(imageqt)
8678
qt_format = getattr(QImage, "Format") if ImageQt.qt_version == "6" else QImage
87-
qimage = QImage(128, 128, getattr(qt_format, "Format_ARGB32")) # type: ignore[operator]
88-
painter = QPainter(qimage) # type: ignore[operator]
89-
image_label = QLabel() # type: ignore[operator]
79+
qimage = QImage(128, 128, getattr(qt_format, "Format_ARGB32"))
80+
painter = QPainter(qimage)
81+
image_label = QLabel()
9082
image_label.setPixmap(data)
91-
image_label.render(painter, QPoint(0, 0), QRegion(0, 0, 128, 128)) # type: ignore[operator]
83+
image_label.render(painter, QPoint(0, 0), QRegion(0, 0, 128, 128))
9284
painter.end()
9385
rendered_tempfile = str(tmp_path / f"temp_rendered_{mode}.png")
9486
qimage.save(rendered_tempfile)

Tests/test_qt_image_toqimage.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
4-
53
import pytest
64

75
from PIL import ImageQt
86

97
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
108

9+
TYPE_CHECKING = False
10+
if TYPE_CHECKING:
11+
from pathlib import Path
12+
1113
pytestmark = pytest.mark.skipif(
1214
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
1315
)
@@ -21,7 +23,7 @@ def test_sanity(mode: str, tmp_path: Path) -> None:
2123
src = hopper(mode)
2224
data = ImageQt.toqimage(src)
2325

24-
assert isinstance(data, QImage) # type: ignore[arg-type, misc]
26+
assert isinstance(data, QImage)
2527
assert not data.isNull()
2628

2729
# reload directly from the qimage

src/PIL/ImageQt.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,19 @@
1818
from __future__ import annotations
1919

2020
import sys
21-
from collections.abc import Callable
2221
from io import BytesIO
23-
from typing import Any, Union
2422

2523
from . import Image
2624
from ._util import is_path
2725

2826
TYPE_CHECKING = False
2927
if TYPE_CHECKING:
30-
import PyQt6
31-
import PySide6
28+
from collections.abc import Callable
29+
from typing import Any
3230

3331
from . import ImageFile
3432

3533
QBuffer: type
36-
QByteArray = Union[PyQt6.QtCore.QByteArray, PySide6.QtCore.QByteArray]
37-
QIODevice = Union[PyQt6.QtCore.QIODevice, PySide6.QtCore.QIODevice]
38-
QImage = Union[PyQt6.QtGui.QImage, PySide6.QtGui.QImage]
39-
QPixmap = Union[PyQt6.QtGui.QPixmap, PySide6.QtGui.QPixmap]
4034

4135
qt_version: str | None
4236
qt_versions = [
@@ -50,11 +44,15 @@
5044
try:
5145
qRgba: Callable[[int, int, int, int], int]
5246
if qt_module == "PyQt6":
53-
from PyQt6.QtCore import QBuffer, QIODevice
47+
from PyQt6.QtCore import QBuffer, QByteArray, QIODevice
5448
from PyQt6.QtGui import QImage, QPixmap, qRgba
5549
elif qt_module == "PySide6":
56-
from PySide6.QtCore import QBuffer, QIODevice
57-
from PySide6.QtGui import QImage, QPixmap, qRgba
50+
from PySide6.QtCore import ( # type: ignore[assignment]
51+
QBuffer,
52+
QByteArray,
53+
QIODevice,
54+
)
55+
from PySide6.QtGui import QImage, QPixmap, qRgba # type: ignore[assignment]
5856
except (ImportError, RuntimeError):
5957
continue
6058
qt_is_installed = True
@@ -184,7 +182,7 @@ def _toqclass_helper(im: Image.Image | str | QByteArray) -> dict[str, Any]:
184182

185183
if qt_is_installed:
186184

187-
class ImageQt(QImage): # type: ignore[misc]
185+
class ImageQt(QImage):
188186
def __init__(self, im: Image.Image | str | QByteArray) -> None:
189187
"""
190188
An PIL image wrapper for Qt. This is a subclass of PyQt's QImage

0 commit comments

Comments
 (0)