From fab2410d2bad4260aa998b1317612c56dedeed93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Mon, 5 Aug 2024 14:00:31 -0600 Subject: [PATCH 1/4] chore: Define `Auth` type alias in `types` module --- .../{{cookiecutter.library_name}}/rest-client.py | 6 ++---- singer_sdk/helpers/types.py | 4 ++++ singer_sdk/streams/rest.py | 11 ++--------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py index f4edca913..ca844a5e0 100644 --- a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py +++ b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py @@ -47,11 +47,9 @@ import importlib_resources if TYPE_CHECKING: - from singer_sdk.helpers.types import Context + from singer_sdk.helpers.types import Auth, Context -_Auth = Callable[[requests.PreparedRequest], requests.PreparedRequest] - # TODO: Delete this is if not using json files for schema definition SCHEMAS_DIR = importlib_resources.files(__package__) / "schemas" @@ -74,7 +72,7 @@ def url_base(self) -> str: {%- if cookiecutter.auth_method in ("OAuth2", "JWT") %} @cached_property - def authenticator(self) -> _Auth: + def authenticator(self) -> Auth: """Return a new authenticator object. Returns: diff --git a/singer_sdk/helpers/types.py b/singer_sdk/helpers/types.py index 715f5309a..39aaefef5 100644 --- a/singer_sdk/helpers/types.py +++ b/singer_sdk/helpers/types.py @@ -5,6 +5,8 @@ import sys import typing as t +import requests + if sys.version_info < (3, 9): from typing import Mapping # noqa: ICN003 else: @@ -15,6 +17,7 @@ else: from typing import TypeAlias # noqa: ICN003 + __all__ = [ "Context", "Record", @@ -22,3 +25,4 @@ Context: TypeAlias = Mapping[str, t.Any] Record: TypeAlias = t.Dict[str, t.Any] +Auth: TypeAlias = t.Callable[[requests.PreparedRequest], requests.PreparedRequest] diff --git a/singer_sdk/streams/rest.py b/singer_sdk/streams/rest.py index c65ee3f56..559389e8c 100644 --- a/singer_sdk/streams/rest.py +++ b/singer_sdk/streams/rest.py @@ -26,25 +26,18 @@ from singer_sdk.streams.core import Stream if t.TYPE_CHECKING: - import sys from datetime import datetime from backoff.types import Details from singer_sdk._singerlib import Schema - from singer_sdk.helpers.types import Context + from singer_sdk.helpers.types import Auth, Context from singer_sdk.tap_base import Tap - if sys.version_info >= (3, 10): - from typing import TypeAlias # noqa: ICN003 - else: - from typing_extensions import TypeAlias - DEFAULT_PAGE_SIZE = 1000 DEFAULT_REQUEST_TIMEOUT = 300 # 5 minutes _TToken = t.TypeVar("_TToken") -_Auth: TypeAlias = t.Callable[[requests.PreparedRequest], requests.PreparedRequest] class RESTStream(Stream, t.Generic[_TToken], metaclass=abc.ABCMeta): # noqa: PLR0904 @@ -610,7 +603,7 @@ def parse_response(self, response: requests.Response) -> t.Iterable[dict]: # Abstract methods: @property - def authenticator(self) -> _Auth: + def authenticator(self) -> Auth: """Return or set the authenticator for managing HTTP auth headers. If an authenticator is not specified, REST-based taps will simply pass From 6901525f1ee4664ce4c176c47ba0ba7fa5794a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Mon, 5 Aug 2024 14:06:17 -0600 Subject: [PATCH 2/4] Update cookiecutter --- .../{{cookiecutter.library_name}}/rest-client.py | 2 +- noxfile.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py index ca844a5e0..72d23dae4 100644 --- a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py +++ b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py @@ -8,7 +8,6 @@ {%- endif %} from typing import TYPE_CHECKING, Any, Callable, Iterable -import requests {% if cookiecutter.auth_method == "API Key" -%} from singer_sdk.authenticators import APIKeyAuthenticator from singer_sdk.helpers.jsonpath import extract_jsonpath @@ -47,6 +46,7 @@ import importlib_resources if TYPE_CHECKING: + import requests from singer_sdk.helpers.types import Auth, Context diff --git a/noxfile.py b/noxfile.py index d0d5fbd89..40875be38 100644 --- a/noxfile.py +++ b/noxfile.py @@ -25,6 +25,8 @@ RUFF_OVERRIDES = """\ extend = "./pyproject.toml" + +[lint] extend-ignore = ["TD002", "TD003", "FIX002"] """ From 2543c77365f558eece2b11ca84f86992497bde56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Mon, 5 Aug 2024 14:18:20 -0600 Subject: [PATCH 3/4] Remove unused import --- .../{{cookiecutter.library_name}}/rest-client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py index 72d23dae4..207a7bf1e 100644 --- a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py +++ b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py @@ -6,7 +6,7 @@ {%- if cookiecutter.auth_method in ("OAuth2", "JWT") %} from functools import cached_property {%- endif %} -from typing import TYPE_CHECKING, Any, Callable, Iterable +from typing import TYPE_CHECKING, Any, Iterable {% if cookiecutter.auth_method == "API Key" -%} from singer_sdk.authenticators import APIKeyAuthenticator From a45b0cbd08070ec1b36f7000a7dee38a091e9b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Mon, 5 Aug 2024 14:23:02 -0600 Subject: [PATCH 4/4] Limit imports --- .../{{cookiecutter.library_name}}/rest-client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py index 207a7bf1e..fb805ad55 100644 --- a/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py +++ b/cookiecutter/tap-template/{{cookiecutter.tap_id}}/{{cookiecutter.library_name}}/rest-client.py @@ -47,7 +47,11 @@ if TYPE_CHECKING: import requests + {%- if cookiecutter.auth_method in ("OAuth2", "JWT") %} from singer_sdk.helpers.types import Auth, Context + {%- else %} + from singer_sdk.helpers.types import Context + {%- endif %} # TODO: Delete this is if not using json files for schema definition