Skip to content

Commit f8f5f0a

Browse files
authored
feat: add 'py.typed' declaration (#73)
Check typing under CI using new 'mypy' nox session.
1 parent c3b1e9c commit f8f5f0a

File tree

10 files changed

+30
-10
lines changed

10 files changed

+30
-10
lines changed

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mypy]
2+
python_version = 3.6
3+
exclude = tests/unit/resources/

noxfile.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"lint",
2828
"blacken",
2929
"lint_setup_py",
30+
"mypy",
3031
"unit",
3132
"check_lower_bounds"
3233
]
@@ -74,6 +75,18 @@ def lint_setup_py(session):
7475
session.run("python", "setup.py", "check", "--restructuredtext", "--strict")
7576

7677

78+
@nox.session(python="3.6")
79+
def mypy(session):
80+
"""Verify type hints are mypy compatible."""
81+
session.install("-e", ".")
82+
session.install(
83+
"mypy",
84+
"types-mock",
85+
"types-setuptools",
86+
)
87+
session.run("mypy", "test_utils/", "tests/")
88+
89+
7790
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"])
7891
def unit(session):
7992
constraints_path = str(

test_utils/lower_bound_checker/lower_bound_checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def _get_package_requirements(package_name: str) -> List[Requirement]:
3333
List[pkg_resources.Requirement]: A list of package requirements and extras.
3434
"""
3535
dist = pkg_resources.get_distribution(package_name)
36-
requirements = [Requirement(str(r)) for r in dist.requires(extras=dist.extras)]
36+
extras = tuple(dist.extras)
37+
requirements = [Requirement(str(r)) for r in dist.requires(extras=extras)]
3738

3839
return requirements
3940

test_utils/prefixer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import random
1717
import re
1818

19+
from typing import Union
1920

2021
_RESOURCE_DATE_FORMAT = "%Y%m%d%H%M%S"
2122
_RESOURCE_DATE_LENGTH = 4 + 2 + 2 + 2 + 2 + 2
@@ -61,7 +62,7 @@ def create_prefix(self) -> str:
6162
random_string = hex(random.randrange(0x1000000))[2:]
6263
return f"{self._prefix}{self._separator}{timestamp}{self._separator}{random_string}"
6364

64-
def _name_to_date(self, resource_name: str) -> datetime.datetime:
65+
def _name_to_date(self, resource_name: str) -> Union[datetime.datetime, None]:
6566
start_date = len(self._prefix) + len(self._separator)
6667
date_string = resource_name[start_date : start_date + _RESOURCE_DATE_LENGTH]
6768
try:

test_utils/py.typed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Marker file for PEP 561.
2+
# The test_utils package uses inline types.

test_utils/system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import sys
1818
import time
1919

20-
import google.auth.credentials
21-
from google.auth.environment_vars import CREDENTIALS as TEST_CREDENTIALS
20+
import google.auth.credentials # type: ignore
21+
from google.auth.environment_vars import CREDENTIALS as TEST_CREDENTIALS # type: ignore
2222

2323

2424
# From shell environ. May be None.

test_utils/vpcsc_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import os
1818

19-
import pytest
19+
import pytest # type: ignore
2020

2121

2222
INSIDE_VPCSC_ENVVAR = "GOOGLE_CLOUD_TESTS_IN_VPCSC"

tests/unit/test_lower_bound_checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import List
2020

2121
from click.testing import CliRunner
22-
import pytest
22+
import pytest # type: ignore
2323

2424
from test_utils.lower_bound_checker import lower_bound_checker
2525

@@ -45,7 +45,7 @@ def parse_error_msg(msg: str) -> List[str]:
4545
reqs = []
4646

4747
if match:
48-
reqs = match.groups(1)[0].split(",")
48+
reqs = match.groups(1)[0].split(",") # type: ignore
4949
reqs = [r.strip().replace("'", "").replace('"', "") for r in reqs]
5050

5151
return reqs

tests/unit/test_orchestrate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
try:
1919
from unittest import mock
2020
except ImportError: # pragma: NO PY3 COVER
21-
import mock
21+
import mock # type: ignore
2222

23-
import pytest
23+
import pytest # type: ignore
2424

2525
from test_utils import orchestrate
2626

tests/unit/test_prefixer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import datetime
1616
import re
1717

18-
import pytest
18+
import pytest # type: ignore
1919

2020
import test_utils.prefixer
2121

0 commit comments

Comments
 (0)