Skip to content

Commit 5f09ea8

Browse files
committed
Add type annotations to package
Adding type annotations will allow packages consuming certifi to have access to its typing information. The py.typed data file is include for PEP 561 compliance. If the API is ever expanded, the mypy type checker will ensure it is also typed. For example, right now, typeshed is incomplete after certifi.contents() was added: https://github.com/python/typeshed/blob/ac2ef6e8c963a45841ad91e068e06748caa1fce6/stubs/certifi/certifi.pyi
1 parent 1c8485c commit 5f09ea8

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

Diff for: .github/workflows/ci.yml

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ on:
66
pull_request: {}
77

88
jobs:
9+
mypy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Set up Python
15+
uses: actions/setup-python@v2
16+
- name: Install dependencies
17+
run: pip install mypy
18+
- name: Run mypy
19+
run: mypy --strict certifi
20+
921
test:
1022
runs-on: ubuntu-latest
1123
strategy:

Diff for: certifi/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .core import contents, where
22

3+
__all__ = ["contents", "where"]
34
__version__ = "2021.10.08"

Diff for: certifi/core.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
This module returns the installation location of cacert.pem or its contents.
88
"""
99
import os
10+
import types
11+
from typing import Union
1012

1113
try:
1214
from importlib.resources import path as get_path, read_text
1315

1416
_CACERT_CTX = None
1517
_CACERT_PATH = None
1618

17-
def where():
19+
def where() -> str:
1820
# This is slightly terrible, but we want to delay extracting the file
1921
# in cases where we're inside of a zipimport situation until someone
2022
# actually calls where(), but we don't want to re-extract the file
@@ -40,21 +42,29 @@ def where():
4042

4143

4244
except ImportError:
45+
Package = Union[types.ModuleType, str]
46+
Resource = Union[str, "os.PathLike"]
47+
4348
# This fallback will work for Python versions prior to 3.7 that lack the
4449
# importlib.resources module but relies on the existing `where` function
4550
# so won't address issues with environments like PyOxidizer that don't set
4651
# __file__ on modules.
47-
def read_text(_module, _path, encoding="ascii"):
52+
def read_text(
53+
package: Package,
54+
resource: Resource,
55+
encoding: str = 'utf-8',
56+
errors: str = 'strict'
57+
) -> str:
4858
with open(where(), "r", encoding=encoding) as data:
4959
return data.read()
5060

5161
# If we don't have importlib.resources, then we will just do the old logic
5262
# of assuming we're on the filesystem and munge the path directly.
53-
def where():
63+
def where() -> str:
5464
f = os.path.dirname(__file__)
5565

5666
return os.path.join(f, "cacert.pem")
5767

5868

59-
def contents():
69+
def contents() -> str:
6070
return read_text("certifi", "cacert.pem", encoding="ascii")

Diff for: certifi/py.typed

Whitespace-only changes.

Diff for: certifi/tests/test_certify.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77

88

99
class TestCertifi(unittest.TestCase):
10-
def test_cabundle_exists(self):
10+
def test_cabundle_exists(self) -> None:
1111
assert os.path.exists(certifi.where())
1212

13-
def test_read_contents(self):
13+
def test_read_contents(self) -> None:
1414
content = certifi.contents()
1515
assert "-----BEGIN CERTIFICATE-----" in content
16+
17+
def test_py_typed_exists(self) -> None:
18+
assert os.path.exists(
19+
os.path.join(os.path.dirname(certifi.__file__), 'py.typed')
20+
)

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
'certifi',
4444
],
4545
package_dir={'certifi': 'certifi'},
46-
package_data={'certifi': ['*.pem']},
46+
package_data={'certifi': ['*.pem', 'py.typed']},
4747
# data_files=[('certifi', ['certifi/cacert.pem'])],
4848
include_package_data=True,
4949
zip_safe=False,

0 commit comments

Comments
 (0)