Skip to content

Commit

Permalink
Merge pull request #37 from cisagov/improvement/static_type_checking
Browse files Browse the repository at this point in the history
Add static type checking to the Python skeleton
  • Loading branch information
felddy committed Feb 21, 2020
2 parents a06c5ec + 3172a99 commit 4ae80fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.egg-info
__pycache__
.python-version
.coverage
.mypy_cache
.pytest_cache
.python-version
*.egg-info
8 changes: 6 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
23 changes: 12 additions & 11 deletions src/example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import logging
import os
import sys
from typing import Any, Dict

# Third-Party Libraries
import docopt
Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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["<dividend>"]
divisor = args["<divisor>"]
log_level = args["--log-level"]
dividend: int = validated_args["<dividend>"]
divisor: int = validated_args["<divisor>"]
log_level: str = validated_args["--log-level"]

# Set up logging
logging.basicConfig(
Expand All @@ -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()
Expand Down

0 comments on commit 4ae80fd

Please sign in to comment.