Я попытался проверить код приложения PyQt5 с помощью mypy. Но я обнаружил, что он не будет проверять код внутри моего определенного класса виджетов. Я написал небольшой пример приложения, чтобы узнать, что проверяется, а что нет.
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, \
QSpinBox, QLabel
def add_numbers(a: str, b: str) -> str:
return a + b
def add_numbers2(a: str, b: int) -> int:
return a + b # found: unsupported operand int + str
class MyWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
add_numbers(1, 2) # not found: should result in incompatible type error
self.a_label = QLabel('a:')
self.a_spinbox = QSpinBox()
self.b_label = QLabel('b:')
self.b_spinbox = QSpinBox()
self.c_label = QLabel('c:')
self.c_spinbox = QSpinBox()
self.button = QPushButton('a + b')
layout = QGridLayout()
layout.addWidget(self.a_label, 0, 0)
layout.addWidget(self.a_spinbox, 0, 1)
layout.addWidget(self.b_label, 1, 0)
layout.addWidget(self.b_spinbox, 1, 1)
layout.addWidget(self.button, 2, 1)
layout.addWidget(self.c_label, 3, 0)
layout.addWidget(self.c_spinbox, 3, 1)
self.setLayout(layout)
self.button.clicked.connect(self.add_numbers)
def add_numbers(self):
a = self.a_spinbox.value()
b = self.b_spinbox.value()
c = add_numbers(a, b) # not found: should result in incompatible type error
self.c_spinbox.setValue(c)
if __name__ == '__main__':
add_numbers(1, 2) # found: incompatible type found by mypy
app = QApplication([])
w = MyWidget()
w.show()
app.exec_()
Если я запускаю mypy, я получаю следующий результат:
$ mypy --ignore-missing-imports --follow-imports=skip test.py
test.py:10: error: Unsupported operand types for + ("str" and "int")
test.py:48: error: Argument 1 to "add_numbers" has incompatible type "int";
expected "str"
test.py:48: error: Argument 2 to "add_numbers" has incompatible type "int";
expected "str"
Mypy обнаружил ошибку типа в add_numbers2()
и ошибку в основной части, где я пытаюсь передать два целых числа в функцию add_numbers()
, которая принимает только строки в качестве аргументов. Но ошибки в MyWidget.add_number()
и в __init__()
функции почему-то пропущены. Mypy игнорирует все, что находится внутри класса MyWidget()
. Кто-нибудь знает, что сделать, чтобы mypy полностью проверил код?
typed.py
, который mypy мог бы их выбрать. Я попытался создать этот файл, и у меня все сработало. - person LtWorf   schedule 29.12.2020