Skip to content

Commit

Permalink
refactor!: Make PyJWT and cryptography dependencies optional (#2525)
Browse files Browse the repository at this point in the history
* chore!: Make `PyJWT` and `cryptography` dependencies optional

* Catch exception when jwt deps are missing

* Add `pragma: no cover
  • Loading branch information
edgarrmondragon authored Jul 11, 2024
1 parent 5500e45 commit 9c755bf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ python = ">=3.8"
backoff = { version = ">=2.0.0", python = "<4" }
backports-datetime-fromisoformat = { version = ">=2.0.1", python = "<3.11" }
click = "~=8.0"
cryptography = ">=3.4.6"
fs = ">=2.4.16"
importlib-metadata = {version = "<9.0.0", python = "<3.12"}
importlib-resources = {version = ">=5.12.0,!=6.2.0,!=6.3.0,!=6.3.1", python = "<3.9"}
Expand All @@ -52,7 +51,6 @@ jsonpath-ng = ">=1.5.3"
jsonschema = ">=4.16.0"
packaging = ">=23.1"
pendulum = ">=2.1.0,<4"
PyJWT = "~=2.4"
python-dateutil = ">=2.8.2"
python-dotenv = ">=0.20"
PyYAML = ">=6.0"
Expand Down Expand Up @@ -93,10 +91,15 @@ pytest-durations = {version = ">=1.2.0", optional = true}
# installed as optional 'faker' extra
faker = {version = ">=22.5,<27.0", optional = true}

# Crypto extras
cryptography = { version = ">=3.4.6", optional = true }
PyJWT = { version = "~=2.4", optional = true }

[tool.poetry.extras]
# TODO: Add 'cryptography' and 'PyJWT' to the 'jwt' when we want to remove them
# from the main dependencies
jwt = []
jwt = [
"cryptography",
"PyJWT",
]
docs = [
"furo",
"myst-parser",
Expand Down Expand Up @@ -273,9 +276,9 @@ warn_unused_ignores = true
ignore_missing_imports = true
module = [
"backports.datetime_fromisoformat.*",
"joblib.*", # TODO: Remove when https://github.com/joblib/joblib/issues/1516 is shipped
"jsonpath_ng.*",
"pyarrow.*", # TODO: Remove when https://github.com/apache/arrow/issues/32609 if implemented and released
"joblib.*", # TODO: Remove when https://github.com/joblib/joblib/issues/1516 is shipped
"jsonpath_ng.*", # TODO: Remove when https://github.com/h2non/jsonpath-ng/issues/152 is implemented and released
"pyarrow.*", # TODO: Remove when https://github.com/apache/arrow/issues/32609 if implemented and released
]

[tool.poetry-dynamic-versioning]
Expand Down
13 changes: 9 additions & 4 deletions singer_sdk/authenticators.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,18 @@ def oauth_request_payload(self) -> dict:
Payload object for OAuth.
Raises:
RuntimeError: If the JWT dependencies are not installed.
ValueError: If the private key is not set.
"""
import jwt # noqa: PLC0415
from cryptography.hazmat.backends import default_backend # noqa: PLC0415
from cryptography.hazmat.primitives import serialization # noqa: PLC0415
try:
import jwt # noqa: PLC0415
from cryptography.hazmat.backends import default_backend # noqa: PLC0415
from cryptography.hazmat.primitives import serialization # noqa: PLC0415
except ModuleNotFoundError as ex: # pragma: no cover
msg = "Install singer-sdk[jwt] to use OAuthJWTAuthenticator."
raise RuntimeError(msg) from ex

if not self.private_key:
if not self.private_key: # pragma: no cover
msg = "Missing 'private_key' property for OAuth payload."
raise ValueError(msg)

Expand Down

0 comments on commit 9c755bf

Please sign in to comment.