Skip to content

Commit

Permalink
Add PEP 484 type hints.
Browse files Browse the repository at this point in the history
  • Loading branch information
felddy committed Feb 20, 2020
1 parent b68db4c commit 3172a99
Showing 1 changed file with 12 additions and 11 deletions.
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 3172a99

Please sign in to comment.