Skip to content

Commit 8da0709

Browse files
authored
fix: warning on application name not set, correctly handle aliases for snowflake settings (#24)
1 parent a569514 commit 8da0709

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

snowflake_utils/settings.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import os
22
from enum import Enum
3+
from logging import getLogger
34
from typing import Annotated
45

5-
from pydantic import Field, StringConstraints
6+
from pydantic import AliasChoices, Field, StringConstraints
67
from pydantic_settings import BaseSettings, SettingsConfigDict
78
from snowflake.connector import SnowflakeConnection
89
from snowflake.connector import connect as _connect
910

11+
logger = getLogger(__name__)
12+
1013

1114
class Authenticator(str, Enum):
1215
snowflake = "snowflake"
@@ -24,7 +27,7 @@ def __str__(self) -> str:
2427

2528

2629
class SnowflakeSettings(BaseSettings):
27-
model_config = SettingsConfigDict(env_prefix="SNOWFLAKE_")
30+
model_config = SettingsConfigDict(env_prefix="SNOWFLAKE_", populate_by_name=True)
2831

2932
account: str = "snowflake-test"
3033
user: str = "snowlfake"
@@ -33,7 +36,9 @@ class SnowflakeSettings(BaseSettings):
3336
role: str = "snowlfake"
3437
warehouse: str = "snowlfake"
3538
authenticator: Authenticator | OktaDomain = Authenticator.snowflake
36-
schema_name: str | None = Field(default=None, validation_alias="SNOWFLAKE_SCHEMA")
39+
schema_name: str | None = Field(
40+
default=None, validation_alias=AliasChoices("SNOWFLAKE_SCHEMA")
41+
)
3742
private_key_file: str | None = None
3843
private_key_password: str | None = None
3944
application: str | None = None
@@ -47,8 +52,13 @@ def creds(self) -> dict[str, str | None]:
4752
"role": self.role,
4853
"warehouse": self.warehouse,
4954
"authenticator": str(self.authenticator),
50-
"application": self.application,
5155
}
56+
57+
if self.application is not None:
58+
base_creds["application"] = self.application
59+
else:
60+
logger.warning("No Snowflake application name provided!")
61+
5262
if self.authenticator in (Authenticator.externalbrowser):
5363
return base_creds
5464
if self.private_key_file is not None and os.path.exists(self.private_key_file):

tests/test_settings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
from unittest.mock import patch
3+
14
import pytest
25
from pydantic import ValidationError
36

@@ -23,6 +26,17 @@ def test_authenticator_invalid() -> None:
2326
SnowflakeSettings(authenticator="invalid")
2427

2528

29+
def test_schema_name() -> None:
30+
settings = SnowflakeSettings(schema_name="foo")
31+
assert settings.schema_name == "foo"
32+
33+
34+
def test_schema_name_from_env() -> None:
35+
with patch.dict(os.environ, {"SNOWFLAKE_SCHEMA": "bar"}):
36+
settings = SnowflakeSettings()
37+
assert settings.schema_name == "bar"
38+
39+
2640
@pytest.mark.parametrize(
2741
"object_name",
2842
[

0 commit comments

Comments
 (0)