From 3b81b00dd1685fbce0749fe280325cad85a8fb16 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 1 Mar 2026 20:31:09 +0200 Subject: [PATCH 1/2] fix: do not store reference to algorithms dict on PyJWK --- CHANGELOG.rst | 1 + jwt/api_jwk.py | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d2cfb771..ff57d723 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,7 @@ Fixed - Annotate PyJWKSet.keys for pyright by @tamird in `#1134 `__ - Close ``HTTPError`` response to prevent ``ResourceWarning`` on Python 3.14 by @veeceey in `#1133 `__ +- Do not keep `algorithms` dict in JWK instances by @akx in `#1143 `__ Added ~~~~~ diff --git a/jwt/api_jwk.py b/jwt/api_jwk.py index 960de75c..4ae7cc3f 100644 --- a/jwt/api_jwk.py +++ b/jwt/api_jwk.py @@ -28,7 +28,6 @@ def __init__(self, jwk_data: JWKDict, algorithm: str | None = None) -> None: :raises MissingCryptographyError: If the algorithm requires ``cryptography`` to be installed and it is not available. :raises PyJWKError: If unable to find an algorithm for the key. """ - self._algorithms = get_default_algorithms() self._jwk_data = jwk_data kty = self._jwk_data.get("kty", None) @@ -73,10 +72,12 @@ def __init__(self, jwk_data: JWKDict, algorithm: str | None = None) -> None: self.algorithm_name = algorithm - if algorithm in self._algorithms: - self.Algorithm = self._algorithms[algorithm] - else: - raise PyJWKError(f"Unable to find an algorithm for key: {self._jwk_data}") + try: + self.Algorithm = get_default_algorithms()[algorithm] + except KeyError: + raise PyJWKError( + f"Unable to find an algorithm for key: {self._jwk_data}", + ) from None self.key = self.Algorithm.from_jwk(self._jwk_data) From 88a575d23351a9f175be2b87262760e89f7b8168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asif=20Saif=20Uddin=20=7B=22Auvi=22=3A=22=E0=A6=85?= =?UTF-8?q?=E0=A6=AD=E0=A6=BF=22=7D?= Date: Mon, 2 Mar 2026 10:22:30 +0600 Subject: [PATCH 2/2] Update CHANGELOG.rst Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ff57d723..eca4c59f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,7 +12,7 @@ Fixed - Annotate PyJWKSet.keys for pyright by @tamird in `#1134 `__ - Close ``HTTPError`` response to prevent ``ResourceWarning`` on Python 3.14 by @veeceey in `#1133 `__ -- Do not keep `algorithms` dict in JWK instances by @akx in `#1143 `__ +- Do not keep ``algorithms`` dict in PyJWK instances by @akx in `#1143 `__ Added ~~~~~