Skip to content

Commit

Permalink
Pydantic v2 integration (#1952)
Browse files Browse the repository at this point in the history
* feat(internal): add pydantic v2 support

Linting fixes.

Update tests/unit/test_kwargs/test_path_params.py

fix signature namespace issue

support min_length and max_length

support min_length and max_length

handle constraints on union types

fix error message tests

chore(signature-model): remove pydantic and attrs signature models

chore(signature model): fix python 3.8 compat

feat: msgspec signature model.

Linting fixes.

Update tests/unit/test_kwargs/test_path_params.py

fix signature namespace issue

support min_length and max_length

support min_length and max_length

handle constraints on union types

fix error message tests

chore(signature-model): remove pydantic and attrs signature models

feat: msgspec signature model.

Linting fixes.

Update tests/unit/test_kwargs/test_path_params.py

fix signature namespace issue

support min_length and max_length

support min_length and max_length

handle constraints on union types

fix error message tests

feat(signature-model): add pydantic v2 support

* feat(internal): handle pydantic errors

---------

Co-authored-by: Peter Schutt <peter.github@proton.me>
  • Loading branch information
Goldziher and peterschutt committed Jul 14, 2023
1 parent 8f97076 commit 6a7af34
Show file tree
Hide file tree
Showing 39 changed files with 922 additions and 556 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ jobs:
fail-fast: true
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
pydantic-version: ["1", "2"]
uses: ./.github/workflows/test.yaml
with:
coverage: ${{ matrix.python-version == '3.11' && matrix.pydantic-version == '2' }}
integration: ${{ matrix.python-version == '3.11' && matrix.pydantic-version == '2' }}
pydantic-version: ${{ matrix.pydantic-version }}
python-version: ${{ matrix.python-version }}
coverage: ${{ matrix.python-version == '3.11' }}
integration: ${{ matrix.python-version == '3.11' }}

test-platform-compat:
if: github.event_name == 'push'
Expand All @@ -44,6 +46,7 @@ jobs:
uses: ./.github/workflows/test.yaml
with:
python-version: "3.11"
pydantic-version: "2"
os: ${{ matrix.os }}

sonar:
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
python-version:
required: true
type: string
pydantic-version:
required: true
type: string
coverage:
required: false
type: boolean
Expand Down Expand Up @@ -45,16 +48,19 @@ jobs:
uses: actions/cache@v3
with:
path: .venv
key: v1-venv-${{ runner.os }}-${{ inputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
key: v1-venv-${{ runner.os }}-${{ inputs.python-version }}-${{ inputs.pydantic-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Load cached pip wheels
if: runner.os == 'Windows'
id: cached-pip-wheels
uses: actions/cache@v3
with:
path: ~/.cache
key: cache-${{ runner.os }}-${{ inputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
key: cache-${{ runner.os }}-${{ inputs.python-version }}-${{ inputs.pydantic-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install --no-interaction
- if: ${{ inputs.pydantic-version == '1' }}
name: Install pydantic v1
run: source .venv/bin/activate && pip install "pydantic>=1.10.10"
- name: Set pythonpath
run: echo "PYTHONPATH=$PWD" >> $GITHUB_ENV
- name: Test
Expand Down
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ repos:
polyfactory,
prometheus_client,
psycopg,
pydantic,
pydantic>=2,
pydantic_extra_types,
pytest,
pytest-lazy-fixture,
pytest-mock,
Expand Down Expand Up @@ -159,7 +160,8 @@ repos:
polyfactory,
prometheus_client,
psycopg,
pydantic,
pydantic>=2,
pydantic_extra_types,
pytest,
pytest-lazy-fixture,
pytest-mock,
Expand Down
11 changes: 3 additions & 8 deletions docs/examples/startup_and_shutdown.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import os
from typing import cast

from pydantic import BaseSettings
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine

from litestar import Litestar


class AppSettings(BaseSettings):
DATABASE_URI: str = "postgresql+asyncpg://postgres:mysecretpassword@pg.db:5432/db"


settings = AppSettings()
DB_URI = os.environ.get("DATABASE_URI", "postgresql+asyncpg://postgres:mysecretpassword@pg.db:5432/db")


def get_db_connection(app: Litestar) -> AsyncEngine:
Expand All @@ -19,7 +14,7 @@ def get_db_connection(app: Litestar) -> AsyncEngine:
If it doesn't exist, creates it and saves it in on the application state object
"""
if not getattr(app.state, "engine", None):
app.state.engine = create_async_engine(settings.DATABASE_URI)
app.state.engine = create_async_engine(DB_URI)
return cast("AsyncEngine", app.state.engine)


Expand Down
1 change: 1 addition & 0 deletions litestar/_kwargs/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from litestar.dto.interface import DTOInterface
from litestar.typing import FieldDefinition


__all__ = (
"body_extractor",
"cookies_extractor",
Expand Down
5 changes: 2 additions & 3 deletions litestar/_kwargs/parameter_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from litestar.enums import ParamType
from litestar.params import ParameterKwarg

__all__ = ("ParameterDefinition", "create_parameter_definition", "merge_parameter_sets")


if TYPE_CHECKING:
from litestar.typing import FieldDefinition

__all__ = ("ParameterDefinition", "create_parameter_definition", "merge_parameter_sets")


class ParameterDefinition(NamedTuple):
"""Tuple defining a kwarg representing a request parameter."""
Expand Down
Loading

0 comments on commit 6a7af34

Please sign in to comment.