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

Pydantic v2 integration #1952

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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