Skip to content

Commit

Permalink
[DOP-13337] Add Pydantic v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed Mar 5, 2024
1 parent e29a0c1 commit f05c058
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
23 changes: 15 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ env:

jobs:
tests:
name: Run ${{ matrix.mark }} tests (Python ${{ matrix.python-version }} on ${{ matrix.os }})
name: Run tests (Python ${{ matrix.python-version }}, Pydantic ${{ matrix.pydantic-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ['3.7', '3.12']
os: [ubuntu-latest]
include:
- python-version: '3.7'
pydantic-version: '1'
os: ubuntu-latest

- python-version: '3.12'
pydantic-version: '2'
os: ubuntu-latest

steps:
- name: Checkout code
Expand All @@ -47,18 +53,18 @@ jobs:
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-python-${{ matrix.python-version }}-tests-${{ hashFiles('requirements*.txt') }}
key: ${{ runner.os }}-python-${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version}}-tests-${{ hashFiles('requirements*.txt') }}
restore-keys: |
${{ runner.os }}-python-${{ matrix.python-version }}-tests-${{ hashFiles('requirements*.txt') }}
${{ runner.os }}-python-${{ matrix.python-version }}-tests-
${{ runner.os }}-python-${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version}}-tests-${{ hashFiles('requirements*.txt') }}
${{ runner.os }}-python-${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version}}-tests-
${{ runner.os }}-python
${{ runner.os }}-
- name: Upgrade pip
run: python -m pip install --upgrade pip setuptools wheel

- name: Install dependencies
run: pip install -I -r requirements.txt -r requirements-test.txt
run: pip install -I -r requirements.txt -r requirements-test.txt "pydantic==${{ matrix.pydantic-version }}.*"

- name: Build package
run: |
Expand All @@ -68,14 +74,15 @@ jobs:
- name: Run tests
run: |
mkdir reports/ || echo "Directory exists"
# required to register package in etl-entities
pip install -e . --no-build-isolation
source .env.local
./run_tests.sh
- name: Upload coverage results
uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.python-version }}-os-${{ matrix.os }}
name: coverage-python-${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version }}-os-${{ matrix.os }}
path: reports/*

- name: Shutdown Backend Container
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/next_release/14.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Pydantic v2 support.
32 changes: 20 additions & 12 deletions horizon_hwm_store/horizon_hwm_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
HWMUpdateRequestV1,
NamespacePaginateQueryV1,
)
from pydantic import Field, PrivateAttr

try:
from pydantic.v1 import Field, PrivateAttr, root_validator
except ImportError:
from pydantic import ( # type: ignore[no-redef, assignment]
Field,
PrivateAttr,
root_validator,
)


@register_hwm_store_class("horizon")
Expand Down Expand Up @@ -142,17 +150,7 @@ def main(config):
retry: RetryConfig = Field(default_factory=RetryConfig)
timeout: TimeoutConfig = Field(default_factory=TimeoutConfig)
_client: HorizonClientSync = PrivateAttr()
_namespace_id: Optional[int] = PrivateAttr()

def __init__(self, **kwargs):
super().__init__(**kwargs)
self._client = HorizonClientSync( # noqa: WPS601
base_url=self.api_url,
auth=self.auth,
retry=self.retry,
timeout=self.timeout,
)
self._namespace_id = None # noqa: WPS601
_namespace_id: Optional[int] = PrivateAttr(default=None)

def get_hwm(self, name: str) -> Optional[HWM]:
namespace_id = self._get_namespace_id()
Expand Down Expand Up @@ -203,6 +201,16 @@ def check(self) -> HorizonHWMStore:
self._get_namespace_id()
return self

@root_validator(skip_on_failure=True)
def _set_client(cls, values): # noqa: N805
values["_client"] = HorizonClientSync( # noqa: WPS601
base_url=values["api_url"],
auth=values["auth"],
retry=values["retry"],
timeout=values["timeout"],
)
return values

def _get_namespace_id(self) -> int:
"""
Fetch the ID of the namespace. Raises an exception if the namespace doesn't exist.
Expand Down
2 changes: 1 addition & 1 deletion requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
autodoc_pydantic<2
autodoc_pydantic
furo
numpydoc
sphinx
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
data-horizon[client-sync]>=0.0.8,<0.1
etl-entities>=2.1.0,<2.3.0
etl-entities>=2.1.0,<2.4.0

0 comments on commit f05c058

Please sign in to comment.