From af261c328c1aefac59b422de973c59662094190d Mon Sep 17 00:00:00 2001 From: Felddy Date: Thu, 20 Feb 2020 17:29:09 -0500 Subject: [PATCH 1/7] Autoupdate pre-commit hooks. Add mypy. --- .pre-commit-config.yaml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1522593..df79e95 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -47,7 +47,7 @@ repos: additional_dependencies: - flake8-docstrings - repo: https://github.com/asottile/pyupgrade - rev: v1.26.2 + rev: v2.0.0 hooks: - id: pyupgrade # Run bandit on "tests" tree with a configuration @@ -84,7 +84,7 @@ repos: rev: v4.2.0 hooks: - id: ansible-lint - # files: molecule/default/playbook.yml + # files: molecule/default/playbook.yml - repo: https://github.com/antonbabenko/pre-commit-terraform.git rev: v1.12.0 hooks: @@ -98,3 +98,7 @@ repos: rev: 1.19.1 hooks: - id: prettier + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.761 + hooks: + - id: mypy From b68db4ce5f1c19327c38a37ad3e6ccb6ce32eb97 Mon Sep 17 00:00:00 2001 From: Felddy Date: Thu, 20 Feb 2020 17:29:58 -0500 Subject: [PATCH 2/7] Sort .gitignore add mypy cache. --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 724760e..bedb6e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -*.egg-info __pycache__ -.python-version .coverage +.mypy_cache .pytest_cache +.python-version +*.egg-info From 3172a9924060b1bfb2b5c1d7edd0852154ba4a10 Mon Sep 17 00:00:00 2001 From: Felddy Date: Thu, 20 Feb 2020 17:35:43 -0500 Subject: [PATCH 3/7] Add PEP 484 type hints. See: https://www.python.org/dev/peps/pep-0484/ --- src/example/example.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/example/example.py b/src/example/example.py index b2856d9..556e8b5 100755 --- a/src/example/example.py +++ b/src/example/example.py @@ -25,6 +25,7 @@ import logging import os import sys +from typing import Any, Dict # Third-Party Libraries import docopt @@ -33,10 +34,10 @@ from ._version import __version__ -DEFAULT_ECHO_MESSAGE = "Hello World from the example default!" +DEFAULT_ECHO_MESSAGE: str = "Hello World from the example default!" -def example_div(dividend, divisor): +def example_div(dividend: float, divisor: float) -> float: """Print some logging messages.""" logging.debug("This is a debug message") logging.info("This is an info message") @@ -46,11 +47,11 @@ def example_div(dividend, divisor): return dividend / divisor -def main(): +def main() -> int: """Set up logging and call the example function.""" - args = docopt.docopt(__doc__, version=__version__) + args: Dict[str, str] = docopt.docopt(__doc__, version=__version__) # Validate and convert arguments as needed - schema = Schema( + schema: Schema = Schema( { "--log-level": And( str, @@ -70,16 +71,16 @@ def main(): ) try: - args = schema.validate(args) + validated_args: Dict[str, Any] = schema.validate(args) except SchemaError as err: # Exit because one or more of the arguments were invalid print(err, file=sys.stderr) return 1 # Assign validated arguments to variables - dividend = args[""] - divisor = args[""] - log_level = args["--log-level"] + dividend: int = validated_args[""] + divisor: int = validated_args[""] + log_level: str = validated_args["--log-level"] # Set up logging logging.basicConfig( @@ -89,11 +90,11 @@ def main(): logging.info(f"{dividend} / {divisor} == {example_div(dividend, divisor)}") # Access some data from an environment variable - message = os.getenv("ECHO_MESSAGE", DEFAULT_ECHO_MESSAGE) + message: str = os.getenv("ECHO_MESSAGE", DEFAULT_ECHO_MESSAGE) logging.info(f'ECHO_MESSAGE="{message}"') # Access some data from our package data (see the setup.py) - secret_message = ( + secret_message: str = ( pkg_resources.resource_string("example", "data/secret.txt") .decode("utf-8") .strip() From 067ee0850c154845b7de623988c5a1bd5ce67d3a Mon Sep 17 00:00:00 2001 From: Felddy Date: Thu, 20 Feb 2020 17:29:09 -0500 Subject: [PATCH 4/7] Autoupdate pre-commit hooks. Add mypy. --- .pre-commit-config.yaml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7856658..46cea9e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -47,7 +47,7 @@ repos: additional_dependencies: - flake8-docstrings - repo: https://github.com/asottile/pyupgrade - rev: v1.26.2 + rev: v2.0.0 hooks: - id: pyupgrade - repo: https://github.com/PyCQA/bandit @@ -74,7 +74,7 @@ repos: rev: v4.2.0 hooks: - id: ansible-lint - # files: molecule/default/playbook.yml + # files: molecule/default/playbook.yml - repo: https://github.com/antonbabenko/pre-commit-terraform.git rev: v1.12.0 hooks: @@ -88,3 +88,7 @@ repos: rev: 1.19.1 hooks: - id: prettier + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.761 + hooks: + - id: mypy From bf366086f48d9b7a7e49b9f44f33b44f6a23aeb3 Mon Sep 17 00:00:00 2001 From: Jeremy Frasier Date: Mon, 2 Mar 2020 08:11:32 -0500 Subject: [PATCH 5/7] Add .mypy_cache to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 073a081..95b74cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.mypy_cache __pycache__ .python-version From 454864bc029ce968cbeb8f9f5b88ad1c54522335 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Tue, 3 Mar 2020 16:49:53 -0500 Subject: [PATCH 6/7] Incorporate the Python version into keys for pip and pre-commit caches. This should resolve the issue seen when the Python version changes before there is an update to .pre-commit-config.yml which results in pre-commit pointing to a non-existent Python installation. --- .github/workflows/build.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index aff7e7a..a92cd83 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,20 +14,25 @@ jobs: - uses: actions/setup-python@v1 with: python-version: 3.8 + - name: Store installed Python version + run: | + echo "::set-env name=PY_VERSION::"\ + "$(python -c "import platform;print(platform.python_version())")" - name: Cache pip test requirements uses: actions/cache@v1 with: path: ~/.cache/pip - key: "${{ runner.os }}-pip-test-\ + key: "${{ runner.os }}-pip-test-py${{ env.PY_VERSION }}-\ ${{ hashFiles('**/requirements-test.txt') }}" restore-keys: | + ${{ runner.os }}-pip-test-py${{ env.PY_VERSION }}- ${{ runner.os }}-pip-test- ${{ runner.os }}-pip- - name: Cache pre-commit hooks uses: actions/cache@v1 with: path: ~/.cache/pre-commit - key: "${{ runner.os }}-pre-commit-\ + key: "${{ runner.os }}-pre-commit-py${{ env.PY_VERSION }}-\ ${{ hashFiles('**/.pre-commit-config.yaml') }}" - name: Install dependencies run: | From c6093a27ddd03d10138e8dadabba66243c7cb1a2 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Fri, 6 Mar 2020 09:58:31 -0500 Subject: [PATCH 7/7] Adjust handler logging setup variable names to appease the mypy hook. --- lambda_handler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lambda_handler.py b/lambda_handler.py index 9cbaafe..7428cfa 100644 --- a/lambda_handler.py +++ b/lambda_handler.py @@ -25,10 +25,10 @@ # and # https://stackoverflow.com/questions/37703609/using-python-logging-with-aws-lambda # for more details. -root = logging.getLogger() -if root.handlers: - for handler in root.handlers: - root.removeHandler(handler) +logging_root = logging.getLogger() +if logging_root.handlers: + for logging_handler in logging_root.handlers: + logging_root.removeHandler(logging_handler) def handler(event, context):