From 725a1cd30b04bd255ccb5c1386da682101f83fbe Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Mon, 17 Aug 2020 01:12:40 +0900 Subject: [PATCH 1/5] Add mypy --- python_test_example/Dockerfile | 2 +- python_test_example/Makefile | 4 ++++ python_test_example/flask_app/app.py | 5 +++-- python_test_example/flask_app/model.py | 2 +- python_test_example/flask_app/service.py | 2 +- python_test_example/mypy.ini | 18 ++++++++++++++++++ 6 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 python_test_example/mypy.ini diff --git a/python_test_example/Dockerfile b/python_test_example/Dockerfile index 9d0711b..e97349f 100644 --- a/python_test_example/Dockerfile +++ b/python_test_example/Dockerfile @@ -1,4 +1,4 @@ FROM python:3.7-slim RUN pip install --no-cache-dir --upgrade pip setuptools wheel \ - && pip install --no-cache-dir requests flask + && pip install --no-cache-dir requests flask mypy diff --git a/python_test_example/Makefile b/python_test_example/Makefile index 9f0661e..ae48f0e 100644 --- a/python_test_example/Makefile +++ b/python_test_example/Makefile @@ -10,6 +10,10 @@ dbuild: drun: docker run --rm -it -v $(curdir):/opt -w /opt $(image):$(tag) bash +.PHONY: drmi +drmi: + docker rmi -f $(image):$(tag) + .PHONY: all_test all_test: python3 -m unittest discover --verbose --pattern "*_test.py" diff --git a/python_test_example/flask_app/app.py b/python_test_example/flask_app/app.py index de21bc0..f3de11e 100644 --- a/python_test_example/flask_app/app.py +++ b/python_test_example/flask_app/app.py @@ -1,6 +1,7 @@ import json from flask import Flask, jsonify, make_response +from flask.wrappers import Response from .service import Service @@ -10,12 +11,12 @@ service = Service() @app.route('/', methods=['GET']) -def index(): +def index() -> Response: message = {'message': 'OK'} return make_response(jsonify(message), 200) @app.route('/predict', methods=['POST']) -def predict(): +def predict() -> Response: print('Call predict in app') target = 'A' if not service.check_model(): diff --git a/python_test_example/flask_app/model.py b/python_test_example/flask_app/model.py index ef296ea..f136e85 100644 --- a/python_test_example/flask_app/model.py +++ b/python_test_example/flask_app/model.py @@ -1,3 +1,3 @@ -def load_model(): +def load_model() -> str: print('Call load_model in model') return 'model' diff --git a/python_test_example/flask_app/service.py b/python_test_example/flask_app/service.py index 5f4791f..98dd09e 100644 --- a/python_test_example/flask_app/service.py +++ b/python_test_example/flask_app/service.py @@ -5,7 +5,7 @@ print('In service') class Service: - def __init__(self): + def __init__(self) -> None: print('Init Service') self.model = load_model() print(f'self.model: {self.model}') diff --git a/python_test_example/mypy.ini b/python_test_example/mypy.ini new file mode 100644 index 0000000..9db99d4 --- /dev/null +++ b/python_test_example/mypy.ini @@ -0,0 +1,18 @@ +[mypy] +python_version = 3.7 +disallow_untyped_defs = True +ignore_missing_imports = True +warn_redundant_casts = True +no_implicit_optional = True + +[mypy-flask_app.app_test.*] +disallow_untyped_defs = False + +[mypy-i76_testcase_subclass.utils_test.*] +disallow_untyped_defs = False + +[mypy-i77_setup_and_teardown.*] +disallow_untyped_defs = False + +[mypy-mock.my_class_test.*] +disallow_untyped_defs = False From 5d746bf79d94cbaa987cf279d45c722969cafd99 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Mon, 17 Aug 2020 01:15:06 +0900 Subject: [PATCH 2/5] Add mypy to CI --- .github/workflows/test_for_python_test_example.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_for_python_test_example.yml b/.github/workflows/test_for_python_test_example.yml index 7aa48ab..9de6539 100644 --- a/.github/workflows/test_for_python_test_example.yml +++ b/.github/workflows/test_for_python_test_example.yml @@ -15,8 +15,11 @@ jobs: - name: Install Library run: | python3 -m pip install --upgrade pip - pip install requests flask + pip install requests flask mypy - name: Run unittest run: | cd python_test_example python3 -m unittest discover --verbose --pattern "*_test.py" + - name: Run mypy + run: | + mypy . From 3b97cf09bf13c479677668b3426282d1c146f0c0 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Mon, 17 Aug 2020 01:27:11 +0900 Subject: [PATCH 3/5] Fixed [Run mypy] --- .github/workflows/test_for_python_test_example.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test_for_python_test_example.yml b/.github/workflows/test_for_python_test_example.yml index 9de6539..f881bd5 100644 --- a/.github/workflows/test_for_python_test_example.yml +++ b/.github/workflows/test_for_python_test_example.yml @@ -16,10 +16,10 @@ jobs: run: | python3 -m pip install --upgrade pip pip install requests flask mypy + - name: Run mypy + run: | + mypy python_test_example --config-file python_test_example/mypy.ini - name: Run unittest run: | cd python_test_example python3 -m unittest discover --verbose --pattern "*_test.py" - - name: Run mypy - run: | - mypy . From 66e7ad8cb102969f1adb2c06a3f6ed74fe58e1d8 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Sat, 22 Aug 2020 17:28:07 +0900 Subject: [PATCH 4/5] Add intentionally error --- python_test_example/i76_testcase_subclass/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_test_example/i76_testcase_subclass/utils.py b/python_test_example/i76_testcase_subclass/utils.py index 0f8d014..b9c74e4 100644 --- a/python_test_example/i76_testcase_subclass/utils.py +++ b/python_test_example/i76_testcase_subclass/utils.py @@ -1,4 +1,4 @@ -def concat(str1: str, str2: str) -> str: +def concat(str1: str, str2: str): if not isinstance(str1, str) and isinstance(str2, str): raise TypeError From 050a71d05bf31cdcab7db9191cdcefa0e70205e9 Mon Sep 17 00:00:00 2001 From: suaaa7 Date: Sat, 22 Aug 2020 17:31:05 +0900 Subject: [PATCH 5/5] Revert "Add intentionally error" This reverts commit 66e7ad8cb102969f1adb2c06a3f6ed74fe58e1d8. --- python_test_example/i76_testcase_subclass/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_test_example/i76_testcase_subclass/utils.py b/python_test_example/i76_testcase_subclass/utils.py index b9c74e4..0f8d014 100644 --- a/python_test_example/i76_testcase_subclass/utils.py +++ b/python_test_example/i76_testcase_subclass/utils.py @@ -1,4 +1,4 @@ -def concat(str1: str, str2: str): +def concat(str1: str, str2: str) -> str: if not isinstance(str1, str) and isinstance(str2, str): raise TypeError