Type stubs for certifi
types-certifi is a PEP 561 type stub package providing external type annotations for the `certifi` library, which itself provides Mozilla's carefully curated collection of Root Certificates for validating SSL certificates in Python applications. It is used by static type-checking tools like Mypy and Pyright to analyze code that uses `certifi`. This specific package version (2021.10.8.3) is an older snapshot from the `typeshed` project.
Warnings
- breaking The `types-certifi` package is obsolete for `certifi` versions 2022.5.18.1 and newer. Starting with `certifi==2022.5.18.1`, the `certifi` library itself includes inline type annotations. If you are using `certifi` 2022.5.18.1 or a newer version (e.g., `certifi==2026.2.25`), you MUST uninstall `types-certifi` to avoid type-checking conflicts and potential errors.
- gotcha Type stub packages like `types-certifi` are solely for static type checking (e.g., with Mypy, Pyright, or IDEs) and should not be imported or used at runtime. Attempting to import symbols directly from `types_certifi` in your runtime code will result in an `ImportError`.
- gotcha Mismatched versions between `types-certifi` and the `certifi` runtime package can lead to incorrect or incomplete type-checking results. While `types-certifi` typically tracks `certifi`'s API, older stub versions may not reflect changes in newer runtime versions. This is particularly relevant given the deprecation mentioned above.
Install
-
pip install types-certifi
Imports
- where
import certifi # For type checking: path = certifi.where()
Quickstart
import certifi
import requests
from typing import Text
# certifi.where() provides the path to the CA bundle.
# requests automatically uses certifi for SSL verification.
ca_bundle_path: Text = certifi.where()
def make_secure_request(url: str) -> requests.Response:
"""Makes a secure HTTP GET request using certifi's CA bundle."""
# Requests typically finds certifi automatically,
# but explicitly passing certifi.where() ensures its use and enables type checking.
response: requests.Response = requests.get(url, verify=ca_bundle_path)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
return response
# Example usage:
try:
secure_response = make_secure_request("https://www.google.com")
print(f"Successfully connected to {secure_response.url} (Status: {secure_response.status_code})")
# Type checkers (e.g., Mypy) would use types-certifi to validate
# the types of `certifi.where()` and related calls here.
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")