Skip to content

Commit 1ed0c1c

Browse files
committed
🐛 Fix support for pydantic v1
1 parent 9aec08e commit 1ed0c1c

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

.github/workflows/test.yml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ jobs:
2929
runs-on: ubuntu-latest
3030
strategy:
3131
matrix:
32-
python-version:
33-
- "3.8"
34-
- "3.9"
35-
- "3.10"
36-
- "3.11"
37-
- "3.12"
38-
- "3.13"
39-
- "3.14"
32+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
33+
pydantic-version: ["v2"]
34+
include:
35+
# Add Pydantic v1 tests for select Python versions
36+
- python-version: "3.8"
37+
pydantic-version: "v1"
38+
- python-version: "3.10"
39+
pydantic-version: "v1"
40+
- python-version: "3.12"
41+
pydantic-version: "v1"
4042
fail-fast: false
4143
steps:
4244
- name: Dump GitHub context
@@ -63,19 +65,23 @@ jobs:
6365
with:
6466
limit-access-to-actor: true
6567
- name: Install Dependencies
66-
run: uv pip install -r requirements-tests.txt
68+
run: |
69+
uv pip install -r requirements-tests.txt
70+
if [ "${{ matrix.pydantic-version }}" = "v1" ]; then
71+
uv pip install "pydantic<2.0.0"
72+
fi
6773
- name: Lint
6874
run: bash scripts/lint.sh
6975
- run: mkdir coverage
7076
- name: Test
7177
run: bash scripts/test.sh
7278
env:
73-
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}
74-
CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }}
79+
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version }}
80+
CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version }}
7581
- name: Store coverage files
7682
uses: actions/upload-artifact@v5
7783
with:
78-
name: coverage-${{ matrix.python-version }}
84+
name: coverage-${{ matrix.python-version }}-pydantic-${{ matrix.pydantic-version }}
7985
path: coverage
8086
include-hidden-files: true
8187

src/fastapi_cli/config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
from typing import Any, Dict, Optional
44

55
from pydantic import BaseModel
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):
1115
entrypoint: Optional[str] = None
@@ -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 PYDANTIC_V2:
48+
return cls.model_validate(config)
49+
else:
50+
return cls.parse_obj(config) # type: ignore[attr-defined]

0 commit comments

Comments
 (0)