Skip to content

Commit 5fd135e

Browse files
committed
changes all configspecs to conform with new init methods, drops special init for credentials
1 parent d7f8eee commit 5fd135e

File tree

94 files changed

+499
-716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+499
-716
lines changed

dlt/common/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
from dlt.common import logger
12
from dlt.common.arithmetics import Decimal
23
from dlt.common.wei import Wei
34
from dlt.common.pendulum import pendulum
45
from dlt.common.json import json
56
from dlt.common.runtime.signals import sleep
6-
from dlt.common.runtime import logger
77

88
__all__ = ["Decimal", "Wei", "pendulum", "json", "sleep", "logger"]

dlt/common/configuration/resolve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def initialize_credentials(hint: Any, initial_value: Any) -> CredentialsConfigur
7676
first_credentials: CredentialsConfiguration = None
7777
for idx, spec in enumerate(specs_in_union):
7878
try:
79-
credentials = spec(initial_value)
79+
credentials = spec.from_init_value(initial_value)
8080
if credentials.is_resolved():
8181
return credentials
8282
# keep first credentials in the union to return in case all of the match but not resolve
@@ -88,7 +88,7 @@ def initialize_credentials(hint: Any, initial_value: Any) -> CredentialsConfigur
8888
return first_credentials
8989
else:
9090
assert issubclass(hint, CredentialsConfiguration)
91-
return hint(initial_value) # type: ignore
91+
return hint.from_init_value(initial_value) # type: ignore
9292

9393

9494
def inject_section(

dlt/common/configuration/specs/api_credentials.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
@configspec
88
class OAuth2Credentials(CredentialsConfiguration):
9-
client_id: str
10-
client_secret: TSecretValue
11-
refresh_token: Optional[TSecretValue]
9+
client_id: str = None
10+
client_secret: TSecretValue = None
11+
refresh_token: Optional[TSecretValue] = None
1212
scopes: Optional[List[str]] = None
1313

1414
token: Optional[TSecretValue] = None

dlt/common/configuration/specs/aws_credentials.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,9 @@ def parse_native_representation(self, native_value: Any) -> None:
121121
self.__is_resolved__ = True
122122
except Exception:
123123
raise InvalidBoto3Session(self.__class__, native_value)
124+
125+
@classmethod
126+
def from_session(cls, botocore_session: Any) -> "AwsCredentials":
127+
self = cls()
128+
self.parse_native_representation(botocore_session)
129+
return self

dlt/common/configuration/specs/config_providers_context.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import dataclasses
23
import io
34
from typing import ClassVar, List
45

@@ -28,7 +29,7 @@ class ConfigProvidersConfiguration(BaseConfiguration):
2829
only_toml_fragments: bool = True
2930

3031
# always look in providers
31-
__section__ = known_sections.PROVIDERS
32+
__section__: ClassVar[str] = known_sections.PROVIDERS
3233

3334

3435
@configspec
@@ -37,8 +38,12 @@ class ConfigProvidersContext(ContainerInjectableContext):
3738

3839
global_affinity: ClassVar[bool] = True
3940

40-
providers: List[ConfigProvider]
41-
context_provider: ConfigProvider
41+
providers: List[ConfigProvider] = dataclasses.field(
42+
default=None, init=False, repr=False, compare=False
43+
)
44+
context_provider: ConfigProvider = dataclasses.field(
45+
default=None, init=False, repr=False, compare=False
46+
)
4247

4348
def __init__(self) -> None:
4449
super().__init__()

dlt/common/configuration/specs/config_section_context.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class ConfigSectionContext(ContainerInjectableContext):
99
TMergeFunc = Callable[["ConfigSectionContext", "ConfigSectionContext"], None]
1010

11-
pipeline_name: Optional[str]
11+
pipeline_name: Optional[str] = None
1212
sections: Tuple[str, ...] = ()
1313
merge_style: TMergeFunc = None
1414
source_state_key: str = None
@@ -70,13 +70,3 @@ def __str__(self) -> str:
7070
super().__str__()
7171
+ f": {self.pipeline_name} {self.sections}@{self.merge_style} state['{self.source_state_key}']"
7272
)
73-
74-
if TYPE_CHECKING:
75-
# provide __init__ signature when type checking
76-
def __init__(
77-
self,
78-
pipeline_name: str = None,
79-
sections: Tuple[str, ...] = (),
80-
merge_style: TMergeFunc = None,
81-
source_state_key: str = None,
82-
) -> None: ...

dlt/common/configuration/specs/connection_string_credentials.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
from typing import Any, ClassVar, Dict, List, Optional
1+
import dataclasses
2+
from typing import Any, ClassVar, Dict, List, Optional, Union
3+
24
from dlt.common.libs.sql_alchemy import URL, make_url
35
from dlt.common.configuration.specs.exceptions import InvalidConnectionString
4-
56
from dlt.common.typing import TSecretValue
67
from dlt.common.configuration.specs.base_configuration import CredentialsConfiguration, configspec
78

89

910
@configspec
1011
class ConnectionStringCredentials(CredentialsConfiguration):
11-
drivername: str = None
12+
drivername: str = dataclasses.field(default=None, init=False, repr=False, compare=False)
1213
database: str = None
1314
password: Optional[TSecretValue] = None
1415
username: str = None
@@ -18,6 +19,11 @@ class ConnectionStringCredentials(CredentialsConfiguration):
1819

1920
__config_gen_annotations__: ClassVar[List[str]] = ["port", "password", "host"]
2021

22+
def __init__(self, connection_string: Union[str, Dict[str, Any]] = None) -> None:
23+
"""Initializes the credentials from SQLAlchemy like connection string or from dict holding connection string elements"""
24+
super().__init__()
25+
self._apply_init_value(connection_string)
26+
2127
def parse_native_representation(self, native_value: Any) -> None:
2228
if not isinstance(native_value, str):
2329
raise InvalidConnectionString(self.__class__, native_value, self.drivername)

dlt/common/configuration/specs/gcp_credentials.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import dataclasses
12
import sys
2-
from typing import Any, Final, List, Tuple, Union, Dict
3+
from typing import Any, ClassVar, Final, List, Tuple, Union, Dict
34

45
from dlt.common import json, pendulum
56
from dlt.common.configuration.specs.api_credentials import OAuth2Credentials
@@ -22,8 +23,12 @@
2223

2324
@configspec
2425
class GcpCredentials(CredentialsConfiguration):
25-
token_uri: Final[str] = "https://oauth2.googleapis.com/token"
26-
auth_uri: Final[str] = "https://accounts.google.com/o/oauth2/auth"
26+
token_uri: Final[str] = dataclasses.field(
27+
default="https://oauth2.googleapis.com/token", init=False, repr=False, compare=False
28+
)
29+
auth_uri: Final[str] = dataclasses.field(
30+
default="https://accounts.google.com/o/oauth2/auth", init=False, repr=False, compare=False
31+
)
2732

2833
project_id: str = None
2934

@@ -69,7 +74,9 @@ def to_gcs_credentials(self) -> Dict[str, Any]:
6974
class GcpServiceAccountCredentialsWithoutDefaults(GcpCredentials):
7075
private_key: TSecretValue = None
7176
client_email: str = None
72-
type: Final[str] = "service_account" # noqa: A003
77+
type: Final[str] = dataclasses.field( # noqa: A003
78+
default="service_account", init=False, repr=False, compare=False
79+
)
7380

7481
def parse_native_representation(self, native_value: Any) -> None:
7582
"""Accepts ServiceAccountCredentials as native value. In other case reverts to serialized services.json"""
@@ -121,8 +128,10 @@ def __str__(self) -> str:
121128
@configspec
122129
class GcpOAuthCredentialsWithoutDefaults(GcpCredentials, OAuth2Credentials):
123130
# only desktop app supported
124-
refresh_token: TSecretValue
125-
client_type: Final[str] = "installed"
131+
refresh_token: TSecretValue = None
132+
client_type: Final[str] = dataclasses.field(
133+
default="installed", init=False, repr=False, compare=False
134+
)
126135

127136
def parse_native_representation(self, native_value: Any) -> None:
128137
"""Accepts Google OAuth2 credentials as native value. In other case reverts to serialized oauth client secret json"""
@@ -237,7 +246,7 @@ def __str__(self) -> str:
237246

238247
@configspec
239248
class GcpDefaultCredentials(CredentialsWithDefault, GcpCredentials):
240-
_LAST_FAILED_DEFAULT: float = 0.0
249+
_LAST_FAILED_DEFAULT: ClassVar[float] = 0.0
241250

242251
def parse_native_representation(self, native_value: Any) -> None:
243252
"""Accepts google credentials as native value"""

dlt/common/configuration/specs/known_sections.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
EXTRACT = "extract"
1414
"""extract stage of the pipeline"""
1515

16+
SCHEMA = "schema"
17+
"""schema configuration, ie. normalizers"""
18+
1619
PROVIDERS = "providers"
1720
"""secrets and config providers"""
1821

dlt/common/configuration/specs/run_configuration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import binascii
22
from os.path import isfile, join
33
from pathlib import Path
4-
from typing import Any, Optional, Tuple, IO
4+
from typing import Any, ClassVar, Optional, IO
55
from dlt.common.typing import TSecretStrValue
66

77
from dlt.common.utils import encoding_for_mode, main_module_file_path, reveal_pseudo_secret
@@ -30,7 +30,7 @@ class RunConfiguration(BaseConfiguration):
3030
"""Platform connection"""
3131
dlthub_dsn: Optional[TSecretStrValue] = None
3232

33-
__section__ = "runtime"
33+
__section__: ClassVar[str] = "runtime"
3434

3535
def on_resolved(self) -> None:
3636
# generate pipeline name from the entry point script name

0 commit comments

Comments
 (0)