Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pydantic v2 #795

Merged
merged 11 commits into from
Dec 8, 2023
35 changes: 34 additions & 1 deletion .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:

- name: Install dependencies
run: poetry install

- name: run ci checks
run: make ci

Expand All @@ -53,6 +53,39 @@ jobs:
with:
path: dist/*.whl

# Run tests against pydantic v1 until we drop support.
test-pydantic-v1:
sambhav marked this conversation as resolved.
Show resolved Hide resolved
name: test py${{ matrix.python-version }} (pydantic v1) on ${{ matrix.os }}
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]

runs-on: ${{ matrix.os }}

steps:
- name: checkout
uses: actions/checkout@v3

- name: Install poetry
run: pipx install poetry

- name: setup python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"

- name: Install dependencies
run: |
poetry install
poetry run pip install "pydantic<2"
sambhav marked this conversation as resolved.
Show resolved Hide resolved

- name: run ci checks
run: make ci

concurrency:
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
cancel-in-progress: true
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ workflows-models: ## Generate the Workflows models portion of Argo Workflows
--snake-case-field \
--target-python-version 3.8 \
--output src/hera/workflows/models \
--base-class hera.shared._base_model.BaseModel \
--output-model-type pydantic.BaseModel \
--base-class hera.shared._pydantic.BaseModel \
--input-file-type jsonschema \
--wrap-string-literal \
--disable-appending-item-suffix \
--disable-timestamp \
--use-default-kwarg
@find src/hera/workflows/models/ -name '*.py' -exec sed -i.bak 's/from pydantic import Field/from hera.shared._pydantic import Field/' {} +
@find src/hera/workflows/models/ -name '*.bak' -delete
@poetry run python scripts/models.py $(OPENAPI_SPEC_URL) workflows
@poetry run stubgen -o src -p hera.workflows.models && find src/hera/workflows/models -name '__init__.pyi' -delete
@rm $(SPEC_PATH)
Expand All @@ -74,11 +78,15 @@ events-models: ## Generate the Events models portion of Argo Workflows
--snake-case-field \
--target-python-version 3.8 \
--output src/hera/events/models \
--base-class hera.shared._base_model.BaseModel \
--output-model-type pydantic.BaseModel \
--base-class hera.shared._pydantic.BaseModel \
--input-file-type jsonschema \
--wrap-string-literal \
--disable-appending-item-suffix \
--disable-timestamp \
--use-default-kwarg
@find src/hera/events/models/ -name '*.py' -exec sed -i.bak 's/from pydantic import Field/from hera.shared._pydantic import Field/' {} +
@find src/hera/events/models/ -name '*.bak' -delete
@poetry run python scripts/models.py $(OPENAPI_SPEC_URL) events
@poetry run stubgen -o src -p hera.events.models && find src/hera/events/models -name '__init__.pyi' -delete
@rm $(SPEC_PATH)
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/workflows/scripts/callable_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
```python linenums="1"
from typing import List, Union

from hera.shared.serialization import serialize

try:
from typing import Annotated # type: ignore
except ImportError:
Expand Down Expand Up @@ -99,7 +101,7 @@
with Workflow(name="my-workflow") as w:
with Steps(name="my-steps") as s:
my_function(arguments={"input": Input(a=2, b="bar", c=42)})
str_function(arguments={"input": Input(a=2, b="bar", c=42).json()})
str_function(arguments={"input": serialize(Input(a=2, b="bar", c=42))})
another_function(arguments={"inputs": [Input(a=2, b="bar", c=42), Input(a=2, b="bar", c=42.0)]})
function_kebab(arguments={"a-but-kebab": 3, "b-but-kebab": "bar"})
function_kebab_object(arguments={"input-value": Input(a=3, b="bar", c="42")})
Expand Down
4 changes: 3 additions & 1 deletion examples/workflows/scripts/callable_script.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List, Union

from hera.shared.serialization import serialize

try:
from typing import Annotated # type: ignore
except ImportError:
Expand Down Expand Up @@ -89,7 +91,7 @@ def function_kebab_object(annotated_input_value: Annotated[Input, Parameter(name
with Workflow(name="my-workflow") as w:
with Steps(name="my-steps") as s:
my_function(arguments={"input": Input(a=2, b="bar", c=42)})
str_function(arguments={"input": Input(a=2, b="bar", c=42).json()})
str_function(arguments={"input": serialize(Input(a=2, b="bar", c=42))})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be changed to serialize function in hera, which correctly handles dumping both v1 and v2 objects.

another_function(arguments={"inputs": [Input(a=2, b="bar", c=42), Input(a=2, b="bar", c=42.0)]})
function_kebab(arguments={"a-but-kebab": 3, "b-but-kebab": "bar"})
function_kebab_object(arguments={"input-value": Input(a=3, b="bar", c="42")})
Loading