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 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 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()