Skip to content

Commit

Permalink
refactor: Use future warnings.deprecated decorator (#2647)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon authored Sep 6, 2024
1 parent 93a0796 commit e46a1cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
17 changes: 10 additions & 7 deletions singer_sdk/authenticators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
import base64
import datetime
import math
import sys
import typing as t
import warnings
from types import MappingProxyType
from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit

import requests

from singer_sdk.helpers._util import utc_now

if sys.version_info < (3, 13):
from typing_extensions import deprecated
else:
from warnings import deprecated

if t.TYPE_CHECKING:
import logging

Expand Down Expand Up @@ -277,6 +282,10 @@ def create_for_stream(
return cls(stream=stream, token=token)


@deprecated(
"BasicAuthenticator is deprecated. Use requests.auth.HTTPBasicAuth instead.",
category=DeprecationWarning,
)
class BasicAuthenticator(APIAuthenticatorBase):
"""Implements basic authentication for REST Streams.
Expand All @@ -302,12 +311,6 @@ def __init__(
password: API password.
"""
super().__init__(stream=stream)
warnings.warn(
"BasicAuthenticator is deprecated. Use "
"requests.auth.HTTPBasicAuth instead.",
DeprecationWarning,
stacklevel=2,
)

credentials = f"{username}:{password}".encode()
auth_token = base64.b64encode(credentials).decode("ascii")
Expand Down
34 changes: 20 additions & 14 deletions singer_sdk/connectors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import logging
import sys
import typing as t
import warnings
from collections import UserString
Expand All @@ -18,6 +19,11 @@
from singer_sdk.helpers._util import dump_json, load_json
from singer_sdk.helpers.capabilities import TargetLoadMethods

if sys.version_info < (3, 13):
from typing_extensions import deprecated
else:
from warnings import deprecated

if t.TYPE_CHECKING:
from sqlalchemy.engine import Engine
from sqlalchemy.engine.reflection import Inspector
Expand Down Expand Up @@ -161,6 +167,14 @@ def _connect(self) -> t.Iterator[sa.engine.Connection]:
with self._engine.connect().execution_options(stream_results=True) as conn:
yield conn

@deprecated(
"`SQLConnector.create_sqlalchemy_connection` is deprecated. "
"If you need to execute something that isn't available "
"on the connector currently, make a child class and "
"add your required method on that connector.",
category=DeprecationWarning,
stacklevel=1,
)
def create_sqlalchemy_connection(self) -> sa.engine.Connection:
"""(DEPRECATED) Return a new SQLAlchemy connection using the provided config.
Expand All @@ -180,16 +194,14 @@ def create_sqlalchemy_connection(self) -> sa.engine.Connection:
Returns:
A newly created SQLAlchemy engine object.
"""
warnings.warn(
"`SQLConnector.create_sqlalchemy_connection` is deprecated. "
"If you need to execute something that isn't available "
"on the connector currently, make a child class and "
"add your required method on that connector.",
DeprecationWarning,
stacklevel=2,
)
return self._engine.connect().execution_options(stream_results=True)

@deprecated(
"`SQLConnector.create_sqlalchemy_engine` is deprecated. Override "
"`_engine` or `sqlalchemy_url` instead.",
category=DeprecationWarning,
stacklevel=1,
)
def create_sqlalchemy_engine(self) -> Engine:
"""(DEPRECATED) Return a new SQLAlchemy engine using the provided config.
Expand All @@ -199,12 +211,6 @@ def create_sqlalchemy_engine(self) -> Engine:
Returns:
A newly created SQLAlchemy engine object.
"""
warnings.warn(
"`SQLConnector.create_sqlalchemy_engine` is deprecated. Override"
"`_engine` or sqlalchemy_url` instead.",
DeprecationWarning,
stacklevel=2,
)
return self._engine

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_connector_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_engine_creates_and_returns_cached_engine(self, connector):
engine2 = connector._cached_engine
assert engine1 is engine2

def test_deprecated_functions_warn(self, connector):
def test_deprecated_functions_warn(self, connector: SQLConnector):
with pytest.deprecated_call():
connector.create_sqlalchemy_engine()
with pytest.deprecated_call():
Expand Down

0 comments on commit e46a1cd

Please sign in to comment.