From 3e6e1571ab33fb42647e5305c9626829b15dec6f Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 16 Nov 2021 12:18:54 -0500 Subject: [PATCH 1/3] feat: add 'py.typed' declaration Check typing under CI using new 'mypy' nox session. --- mypy.ini | 3 +++ noxfile.py | 12 ++++++++++++ src/google_crc32c/__init__.py | 2 +- src/google_crc32c/cext.py | 6 +++--- src/google_crc32c/py.typed | 2 ++ tests/test___init__.py | 2 +- 6 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 mypy.ini create mode 100644 src/google_crc32c/py.typed diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 00000000..19d08026 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,3 @@ +[mypy] +python_version = 3.6 +exclude = tests/unit/resources/ diff --git a/noxfile.py b/noxfile.py index 2648c42c..1d247094 100644 --- a/noxfile.py +++ b/noxfile.py @@ -47,3 +47,15 @@ def check(session): # Run py.test against the unit tests. session.run("py.test", "tests") session.run("python", f"{HERE}/scripts/check_crc32c_extension.py", *session.posargs) + + +@nox.session(python="3.6") +def mypy(session): + """Verify type hints are mypy compatible.""" + session.install("-e", ".") + session.install( + "mypy", + "types-mock", + "types-setuptools", + ) + session.run("mypy", "src/google_crc32c/", "tests/") diff --git a/src/google_crc32c/__init__.py b/src/google_crc32c/__init__.py index 272b001e..cae23411 100644 --- a/src/google_crc32c/__init__.py +++ b/src/google_crc32c/__init__.py @@ -25,7 +25,7 @@ from google_crc32c import cext as impl implementation = "c" except ImportError as exc: - from google_crc32c import python as impl + from google_crc32c import python as impl # type: ignore warnings.warn(_SLOW_CRC32C_WARNING, RuntimeWarning) implementation = "python" diff --git a/src/google_crc32c/cext.py b/src/google_crc32c/cext.py index d7c452d6..4a764e38 100644 --- a/src/google_crc32c/cext.py +++ b/src/google_crc32c/cext.py @@ -16,9 +16,9 @@ # NOTE: ``__config__`` **must** be the first import because it (may) # modify the search path used to locate shared libraries. -import google_crc32c.__config__ -from google_crc32c._crc32c import extend -from google_crc32c._crc32c import value +import google_crc32c.__config__ # type: ignore +from google_crc32c._crc32c import extend # type: ignore +from google_crc32c._crc32c import value # type: ignore from google_crc32c._checksum import CommonChecksum diff --git a/src/google_crc32c/py.typed b/src/google_crc32c/py.typed new file mode 100644 index 00000000..076325e2 --- /dev/null +++ b/src/google_crc32c/py.typed @@ -0,0 +1,2 @@ +# Marker file for PEP 561. +# The google_crc32c package uses inline types. diff --git a/tests/test___init__.py b/tests/test___init__.py index e7c44066..327713da 100644 --- a/tests/test___init__.py +++ b/tests/test___init__.py @@ -15,7 +15,7 @@ import itertools from unittest import mock -import pytest +import pytest # type: ignore import google_crc32c From 92bfffa1d3b24685088605bb790ce855816c004e Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 16 Nov 2021 12:24:03 -0500 Subject: [PATCH 2/3] ci: exercise mypy --- .github/workflows/presubmit.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/presubmit.yml b/.github/workflows/presubmit.yml index 1158c765..7d0cf9fc 100644 --- a/.github/workflows/presubmit.yml +++ b/.github/workflows/presubmit.yml @@ -7,6 +7,20 @@ on: jobs: + mypy: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + - name: Install nox + run: | + python -m ensurepip + python -m pip install nox + - name: Run mypy + run: | + nox -s mypy + build-wheels-linux: name: ${{ matrix.os }} strategy: From 00010fbc3abfe8cb0cf6f66ed26c6f02a2c172ff Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 16 Nov 2021 12:27:15 -0500 Subject: [PATCH 3/3] ci: install needed Python version --- .github/workflows/presubmit.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/presubmit.yml b/.github/workflows/presubmit.yml index 7d0cf9fc..a9a0eb2f 100644 --- a/.github/workflows/presubmit.yml +++ b/.github/workflows/presubmit.yml @@ -13,6 +13,8 @@ jobs: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 + with: + python-version: "3.6" - name: Install nox run: | python -m ensurepip