Skip to content

Commit 3172a99

Browse files
committed
Add PEP 484 type hints.
See: https://www.python.org/dev/peps/pep-0484/
1 parent b68db4c commit 3172a99

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/example/example.py

Lines changed: 12 additions & 11 deletions
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)