Skip to content

Commit 4ae80fd

Browse files
authored
Merge pull request #37 from cisagov/improvement/static_type_checking
Add static type checking to the Python skeleton
2 parents a06c5ec + 3172a99 commit 4ae80fd

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
*.egg-info
21
__pycache__
3-
.python-version
42
.coverage
3+
.mypy_cache
54
.pytest_cache
5+
.python-version
6+
*.egg-info

.pre-commit-config.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ repos:
4747
additional_dependencies:
4848
- flake8-docstrings
4949
- repo: https://github.com/asottile/pyupgrade
50-
rev: v1.26.2
50+
rev: v2.0.0
5151
hooks:
5252
- id: pyupgrade
5353
# Run bandit on "tests" tree with a configuration
@@ -84,7 +84,7 @@ repos:
8484
rev: v4.2.0
8585
hooks:
8686
- id: ansible-lint
87-
# files: molecule/default/playbook.yml
87+
# files: molecule/default/playbook.yml
8888
- repo: https://github.com/antonbabenko/pre-commit-terraform.git
8989
rev: v1.12.0
9090
hooks:
@@ -98,3 +98,7 @@ repos:
9898
rev: 1.19.1
9999
hooks:
100100
- id: prettier
101+
- repo: https://github.com/pre-commit/mirrors-mypy
102+
rev: v0.761
103+
hooks:
104+
- id: mypy

src/example/example.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import logging
2626
import os
2727
import sys
28+
from typing import Any, Dict
2829

2930
# Third-Party Libraries
3031
import docopt
@@ -33,10 +34,10 @@
3334

3435
from ._version import __version__
3536

36-
DEFAULT_ECHO_MESSAGE = "Hello World from the example default!"
37+
DEFAULT_ECHO_MESSAGE: str = "Hello World from the example default!"
3738

3839

39-
def example_div(dividend, divisor):
40+
def example_div(dividend: float, divisor: float) -> float:
4041
"""Print some logging messages."""
4142
logging.debug("This is a debug message")
4243
logging.info("This is an info message")
@@ -46,11 +47,11 @@ def example_div(dividend, divisor):
4647
return dividend / divisor
4748

4849

49-
def main():
50+
def main() -> int:
5051
"""Set up logging and call the example function."""
51-
args = docopt.docopt(__doc__, version=__version__)
52+
args: Dict[str, str] = docopt.docopt(__doc__, version=__version__)
5253
# Validate and convert arguments as needed
53-
schema = Schema(
54+
schema: Schema = Schema(
5455
{
5556
"--log-level": And(
5657
str,
@@ -70,16 +71,16 @@ def main():
7071
)
7172

7273
try:
73-
args = schema.validate(args)
74+
validated_args: Dict[str, Any] = schema.validate(args)
7475
except SchemaError as err:
7576
# Exit because one or more of the arguments were invalid
7677
print(err, file=sys.stderr)
7778
return 1
7879

7980
# Assign validated arguments to variables
80-
dividend = args["<dividend>"]
81-
divisor = args["<divisor>"]
82-
log_level = args["--log-level"]
81+
dividend: int = validated_args["<dividend>"]
82+
divisor: int = validated_args["<divisor>"]
83+
log_level: str = validated_args["--log-level"]
8384

8485
# Set up logging
8586
logging.basicConfig(
@@ -89,11 +90,11 @@ def main():
8990
logging.info(f"{dividend} / {divisor} == {example_div(dividend, divisor)}")
9091

9192
# Access some data from an environment variable
92-
message = os.getenv("ECHO_MESSAGE", DEFAULT_ECHO_MESSAGE)
93+
message: str = os.getenv("ECHO_MESSAGE", DEFAULT_ECHO_MESSAGE)
9394
logging.info(f'ECHO_MESSAGE="{message}"')
9495

9596
# Access some data from our package data (see the setup.py)
96-
secret_message = (
97+
secret_message: str = (
9798
pkg_resources.resource_string("example", "data/secret.txt")
9899
.decode("utf-8")
99100
.strip()

0 commit comments

Comments
 (0)