Skip to content

Commit

Permalink
fix: fix pydantic core import (litestar-org#329)
Browse files Browse the repository at this point in the history
* fix: import error for pydantic_core imports

This fixes the issue of a MissingDependencyError being raised in the case of pydantic v1 being used. The reason is that
pydantic_core, which is only available in pydantic v2, is being imported. To remedy this, the ImportError when attempting
to import pydantic_core is quitely ignored.

* ci: remove pydantic_core when testing pydantic v1
  • Loading branch information
guacs committed Aug 9, 2023
1 parent 04a504b commit 725835e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
run: poetry install --no-interaction --extras full
- name: Install pydantic
if: matrix.pydantic-version == '1.10'
run: source .venv/bin/activate && pip install "pydantic==1.10.10"
run: source .venv/bin/activate && pip install "pydantic==1.10.10" && pip uninstall pydantic_core -y
- name: Set pythonpath
run: echo "PYTHONPATH=$PWD" >> $GITHUB_ENV
- name: Test
Expand Down
5 changes: 3 additions & 2 deletions polyfactory/factories/pydantic_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@
try:
from pydantic import VERSION, BaseModel, Json
from pydantic.fields import FieldInfo
from pydantic_core import to_json
except ImportError as e:
raise MissingDependencyException("pydantic is not installed") from e

try:
from pydantic.fields import ModelField # type: ignore[attr-defined]

except ImportError:
ModelField = Any
from pydantic_core import PydanticUndefined as Undefined

with suppress(ImportError):
from pydantic_core import to_json

if TYPE_CHECKING:
from random import Random

Expand Down

0 comments on commit 725835e

Please sign in to comment.