PyCharm集成類型檢查mypy

安裝

pip install mypy




配置

配置到PyCharm的External Tools

File → Settings → Tools → External Tools → +

Program:C:\Users\Administrators\AppData\Roaming\Python\Python36\Scripts\mypy.exe

Arguments:$FilePath$

Working directory:$FileDir$

在這裏插入圖片描述

PS:Arguments根據需求配置,查看官方文檔




測試

from typing import List


def greeting(names: List[str]) -> str:
    return 'Hello, {}'.format(', '.join(names))


print(greeting([1, 2, 3]))

Tools → External Tools → Mypy

在這裏插入圖片描述

結果

a.py:8: error: List item 0 has incompatible type "int"; expected "str"
a.py:8: error: List item 1 has incompatible type "int"; expected "str"
a.py:8: error: List item 2 has incompatible type "int"; expected "str"
Found 3 errors in 1 file (checked 1 source file)




安裝插件mypy-pycharm

File → Settings → Plugins → 搜【mypy】 → Install → Restart

在這裏插入圖片描述

重啓PyCharm後 → File → Settings → 設置Path to Pylint executable

C:\Users\Administrators\AppData\Roaming\Python\Python36\Scripts\mypy.exe

在這裏插入圖片描述

運行

在這裏插入圖片描述




參考文獻

  1. Mypy documentation
  2. mypy-pycharm
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章