Skip to content

Commit f176556

Browse files
committed
Remove pydantic settings for pydantic compatibility
1 parent 0abf871 commit f176556

6 files changed

Lines changed: 257 additions & 238 deletions

File tree

.github/workflows/build_backend.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ jobs:
160160
run: ./gradlew :backend:integrationTest --info -Ptestcontainers
161161

162162
build-python:
163-
name: Build Python
163+
name: "Python ${{ matrix.python-version }}${{ matrix.pydantic_v1 && ' (Pydantic V1)' || ''}} on ${{ matrix.os }}${{matrix.experimental && ' (Non failing)' || '' }}"
164164
needs: pre-check
165165
if: ${{ needs.pre-check.outputs.python_at_least != 'PYTHON_AT_LEAST' }}
166166
runs-on: ${{ matrix.os }}
@@ -222,11 +222,6 @@ jobs:
222222
working-directory: python-client
223223
run: pdm run lint
224224

225-
- name: Install pydantic settings (Dirty hack, do not merge !)
226-
if: ${{ !matrix.pydantic_v1 }}
227-
working-directory: python-client
228-
run: pdm run pip install "pydantic_settings>2"
229-
230225
- name: Install pydantic v1
231226
if: ${{ matrix.pydantic_v1 }}
232227
working-directory: python-client

python-client/giskard/settings.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1+
from typing import Optional
2+
13
import os
24
from pathlib import Path
3-
from typing import Optional
4-
from packaging import version
55

66
import pydantic
7-
# See https://linear.app/giskard/issue/GSK-1745/upgrade-pydantic-to-20
7+
from packaging import version
8+
from pydantic import BaseModel
9+
10+
# See https://linear.app/giskard/issue/GSK-1745/upgrade-pydantic-to-20
811
IS_PYDANTIC_V2 = version.parse(pydantic.version.VERSION) >= version.parse("2.0")
912

1013
if IS_PYDANTIC_V2:
11-
# Package have been moved out in Pydantic v2
12-
from pydantic_settings import BaseSettings
14+
FIELD_ATTR = "model_fields"
1315
else:
14-
from pydantic.env_settings import BaseSettings
16+
FIELD_ATTR = "__fields__"
1517

1618

1719
def expand_env_var(env_var: Optional[str]) -> Optional[str]:
18-
if not env_var:
19-
return env_var
20-
while True:
21-
interpolated = os.path.expanduser(os.path.expandvars(str(env_var)))
22-
if interpolated == env_var:
23-
return interpolated
24-
else:
25-
env_var = interpolated
20+
current = env_var
21+
previous = None
22+
while current != previous:
23+
previous = current
24+
current = os.path.expandvars(current)
25+
return current
2626

2727

28-
class Settings(BaseSettings):
28+
class Settings(BaseModel):
2929
home: str = "~/giskard-home"
3030
ws_port: int = 9000
3131
ws_path: str = "/ml-worker"
@@ -42,7 +42,13 @@ class Config:
4242

4343
@property
4444
def home_dir(self) -> Path:
45-
return Path(expand_env_var(self.home))
45+
return Path(expand_env_var(self.home)).resolve()
46+
47+
@classmethod
48+
def build_from_env(cls) -> "Settings":
49+
return Settings(
50+
**{k: os.getenv(cls.Config.env_prefix + k.upper(), v.default) for k, v in getattr(cls, FIELD_ATTR).items()}
51+
)
4652

4753

48-
settings = Settings()
54+
settings = Settings.build_from_env()

0 commit comments

Comments
 (0)