2017-04-11 4 views
0

mypyを使ってPyQt5アプリケーションのコードをタイプチェックしようとしました。しかし、私は定義されたウィジェットクラス内のコードをチェックしないことを知りました。私は何がチェックされ、何が得られないのかを見つけるための小さなサンプルアプリケーションを書いた。タイプを入力する方法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_numbers()に2つの整数を渡すためにしようと、本体部にadd_numbers2()で型エラーとエラーを検出しました引数として文字列を取るだけです。しかし何らかの理由でMyWidget.add_number()__init__()関数のエラーがスキップされました。 MyWidget()クラスの中のすべては、mypyによって無視されます。誰かがmypyがコードを完全にチェックできるようにするために何をすべきかを知っていますか?

答えて

1

デフォルトでは、注釈なしのメソッドはチェックされません。型名を追加するか、--check-untyped-defsで呼び出してください。

+0

'--check-untyped-defs'は間違いなく良い仕事です。これありがとう。これで '__init __()'関数の呼び出しが見つかりました。しかし、 'MyWidget.add_numbers()'のものはまだスキップされます。しかし、これは 'QSpinBox.value()'の型がわからないためです。私は '--ignore-missing-imports'と' --follow-imports = skip'オプションを省きたいと思います。しかし、新しい苦情があります: 'test.py:1:error:モジュール 'PyQt5.QtWidgets'のライブラリスタブファイルがありません。 PyQt5用のスタブファイルはありませんか? – MrLeeh

+1

最近のPyQt5リリースに同梱されています。 –

+0

私はソースファイルからビルドするときにそれぞれ生成されるソースファイルに含まれています。私は通常、ホイールを使用する 'pip install PyQt5'経由でPyQt5をインストールします。ソースからWindowsをビルドするのは難しいです。スタブファイルを取得する簡単な方法はありますか? – MrLeeh

関連する問題