Skip to content

Commit

Permalink
Add mypy check to CI (#618)
Browse files Browse the repository at this point in the history
我先 Merge 了,然后按照保存一个个改旧的 Typing 问题吧。
  • Loading branch information
messense committed Jan 15, 2021
1 parent 811081f commit d6fcd0b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ jobs:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
mypy:
name: mypy-check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install Dependencies
run: |
pip install poetry
poetry install
poetry run pip install mypy
- name: mypy
run: |
# run mypy, tee output to file
poetry run mypy --show-column-numbers --hide-error-context wechatpy | tee /tmp/mypy.out
exit_code="${PIPESTATUS[0]}"
# analyze output
poetry run python .github/workflows/github.py /tmp/mypy.out
exit $exit_code
46 changes: 46 additions & 0 deletions .github/workflows/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
quick script to write mypy errors to github comments
"""
import re
import sys


MYPY_REGEX = (
r"^(\w:)?(?P<file>[^:]+):(?P<line>\d+):((?P<col>\d+):)?"
r"\s*(?P<type>[^:]+):\s*(?P<msg>.+)"
)
MYPY = re.compile(MYPY_REGEX)


def error(
file: str, line: int = 0, col: int = 0, message: str = "error", warn: bool = False
) -> None:
"""write an error to stdout"""
kind = "warning" if warn else "error"
print(f"::{kind} file={file},line={line},col={col}::{message}")


def main(file_path: str) -> None:
"""read the given file, and print errors"""
data = ""
with open(file_path) as file:
data = file.read()

for line in data.split("\n"):
match = MYPY.match(line)
if match:
values = match.groupdict()
error(
values["file"],
line=int(values["line"]),
col=int(values["col"]),
message=values["msg"],
)


if __name__ == "__main__":
if len(sys.argv) != 2:
print("you must specify a json file path!")
sys.exit(1)
file_path = sys.argv[1]
main(file_path)
18 changes: 18 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# MyPy config file
# File reference here - http://mypy.readthedocs.io/en/latest/config_file.html#config-file

[mypy]
warn_redundant_casts = True
warn_unused_ignores = True

# Needed because of bug in MyPy
disallow_subclassing_any = False

[mypy-*]
disallow_untyped_calls = True
disallow_untyped_defs = True
check_untyped_defs = True
warn_return_any = True
no_implicit_optional = True
strict_optional = True
ignore_missing_imports = True

0 comments on commit d6fcd0b

Please sign in to comment.