Skip to content

Commit 0ad2173

Browse files
patrick91pre-commit-ci[bot]tiangolo
authored
🐛 Fix support for Pydantic v1 (#240)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent 9aec08e commit 0ad2173

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ jobs:
3737
- "3.12"
3838
- "3.13"
3939
- "3.14"
40+
pydantic-version:
41+
- "v2"
42+
include:
43+
- python-version: "3.8"
44+
pydantic-version: "v1"
45+
- python-version: "3.9"
46+
pydantic-version: "v1"
47+
- python-version: "3.10"
48+
pydantic-version: "v1"
4049
fail-fast: false
4150
steps:
4251
- name: Dump GitHub context
@@ -64,18 +73,21 @@ jobs:
6473
limit-access-to-actor: true
6574
- name: Install Dependencies
6675
run: uv pip install -r requirements-tests.txt
76+
- name: Install Pydantic v1
77+
if: matrix.pydantic-version == 'v1'
78+
run: uv pip install "pydantic<2.0.0"
6779
- name: Lint
6880
run: bash scripts/lint.sh
6981
- run: mkdir coverage
7082
- name: Test
7183
run: bash scripts/test.sh
7284
env:
73-
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}
74-
CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }}
85+
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version }}
86+
CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version }}
7587
- name: Store coverage files
7688
uses: actions/upload-artifact@v5
7789
with:
78-
name: coverage-${{ matrix.python-version }}
90+
name: coverage-${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version }}
7991
path: coverage
8092
include-hidden-files: true
8193

src/fastapi_cli/config.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
from pathlib import Path
33
from typing import Any, Dict, Optional
44

5-
from pydantic import BaseModel
5+
from pydantic import BaseModel, StrictStr
6+
from pydantic.version import VERSION as PYDANTIC_VERSION
67

78
logger = logging.getLogger(__name__)
89

10+
PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(".")[:2])
11+
PYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2
12+
913

1014
class FastAPIConfig(BaseModel):
11-
entrypoint: Optional[str] = None
15+
entrypoint: Optional[StrictStr] = None
1216

1317
@classmethod
1418
def _read_pyproject_toml(cls) -> Dict[str, Any]:
@@ -39,4 +43,8 @@ def resolve(cls, entrypoint: Optional[str] = None) -> "FastAPIConfig":
3943
if entrypoint is not None:
4044
config["entrypoint"] = entrypoint
4145

42-
return FastAPIConfig.model_validate(config)
46+
# Pydantic v2 uses model_validate, v1 uses parse_obj
47+
if not PYDANTIC_V2:
48+
return cls.parse_obj(config) # type: ignore[no-any-return, unused-ignore]
49+
50+
return cls.model_validate(config) # type: ignore[no-any-return, unused-ignore, attr-defined]

0 commit comments

Comments
 (0)