From b11b52a9b9ab2926feca349f45a4cd6498bc3948 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Wed, 22 Oct 2025 11:13:39 -0500 Subject: [PATCH 1/2] Add pyupgrade as a pre-commit hook --- .pre-commit-config.yaml | 7 +++++++ CHANGELOG.rst | 1 + 2 files changed, 8 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 175d52d8..2bbbf47b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,11 @@ repos: + - repo: https://github.com/asottile/pyupgrade + rev: v3.20.0 + hooks: + - id: pyupgrade + name: Enforce Python 3.9+ idioms + args: ["--py39-plus"] + - repo: https://github.com/psf/black-pre-commit-mirror rev: 25.9.0 hooks: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d35ee036..0ab696ab 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,6 +26,7 @@ Added - Docs: Add example of using leeway with nbf by @djw8605 in `#1034 `__ - Docs: Refactored docs with ``autodoc``; added ``PyJWS`` and ``jwt.algorithms`` docs by @pachewise in `#1045 `__ - Docs: Documentation improvements for "sub" and "jti" claims by @cleder in `#1088 `__ +- Development: Add pyupgrade as a pre-commit hook by @kurtmckee in `#1109 `__ `v2.10.1 `__ ----------------------------------------------------------------------- From 7a87dca835b4ccd358dbf676b6d7d366e785b3da Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Wed, 22 Oct 2025 11:18:07 -0500 Subject: [PATCH 2/2] Run `pre-commit run -a` to upgrade Python syntax --- jwt/api_jwk.py | 3 ++- jwt/api_jwt.py | 4 ++-- jwt/help.py | 3 +-- jwt/jwks_client.py | 12 ++++++------ jwt/types.py | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/jwt/api_jwk.py b/jwt/api_jwk.py index b6b33790..3034d046 100644 --- a/jwt/api_jwk.py +++ b/jwt/api_jwk.py @@ -2,7 +2,8 @@ import json import time -from typing import Any, Iterator +from collections.abc import Iterator +from typing import Any from .algorithms import get_default_algorithms, has_crypto, requires_cryptography from .exceptions import ( diff --git a/jwt/api_jwt.py b/jwt/api_jwt.py index 80760b52..d3686029 100644 --- a/jwt/api_jwt.py +++ b/jwt/api_jwt.py @@ -4,9 +4,9 @@ import os import warnings from calendar import timegm -from collections.abc import Iterable, Sequence +from collections.abc import Container, Iterable, Sequence from datetime import datetime, timedelta, timezone -from typing import TYPE_CHECKING, Any, Container +from typing import TYPE_CHECKING, Any from . import api_jws from .exceptions import ( diff --git a/jwt/help.py b/jwt/help.py index 8e1c2286..2f42b5da 100644 --- a/jwt/help.py +++ b/jwt/help.py @@ -1,7 +1,6 @@ import json import platform import sys -from typing import Dict from . import __version__ as pyjwt_version @@ -13,7 +12,7 @@ cryptography_version = "" -def info() -> Dict[str, Dict[str, str]]: +def info() -> dict[str, dict[str, str]]: """ Generate information for a bug report. Based on the requests package help utility module. diff --git a/jwt/jwks_client.py b/jwt/jwks_client.py index 120eb256..5e33bfa5 100644 --- a/jwt/jwks_client.py +++ b/jwt/jwks_client.py @@ -4,7 +4,7 @@ import urllib.request from functools import lru_cache from ssl import SSLContext -from typing import Any, Dict, List, Optional +from typing import Any from urllib.error import URLError from .api_jwk import PyJWK, PyJWKSet @@ -21,14 +21,14 @@ def __init__( max_cached_keys: int = 16, cache_jwk_set: bool = True, lifespan: float = 300, - headers: Optional[Dict[str, Any]] = None, + headers: dict[str, Any] | None = None, timeout: float = 30, - ssl_context: Optional[SSLContext] = None, + ssl_context: SSLContext | None = None, ): if headers is None: headers = {} self.uri = uri - self.jwk_set_cache: Optional[JWKSetCache] = None + self.jwk_set_cache: JWKSetCache | None = None self.headers = headers self.timeout = timeout self.ssl_context = ssl_context @@ -82,7 +82,7 @@ def get_jwk_set(self, refresh: bool = False) -> PyJWKSet: return PyJWKSet.from_dict(data) - def get_signing_keys(self, refresh: bool = False) -> List[PyJWK]: + def get_signing_keys(self, refresh: bool = False) -> list[PyJWK]: jwk_set = self.get_jwk_set(refresh) signing_keys = [ jwk_set_key @@ -117,7 +117,7 @@ def get_signing_key_from_jwt(self, token: str | bytes) -> PyJWK: return self.get_signing_key(header.get("kid")) @staticmethod - def match_kid(signing_keys: List[PyJWK], kid: str) -> Optional[PyJWK]: + def match_kid(signing_keys: list[PyJWK], kid: str) -> PyJWK | None: signing_key = None for key in signing_keys: diff --git a/jwt/types.py b/jwt/types.py index cc1ea6a1..bb6c435c 100644 --- a/jwt/types.py +++ b/jwt/types.py @@ -1,6 +1,6 @@ -from typing import Any, Callable, Dict, TypedDict +from typing import Any, Callable, TypedDict -JWKDict = Dict[str, Any] +JWKDict = dict[str, Any] HashlibHash = Callable[..., Any]