Skip to content

Commit

Permalink
Add pydantic 2 compatybility (#26)
Browse files Browse the repository at this point in the history
* pydantic2 compatybility

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add type ignore

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Czaki and pre-commit-ci[bot] authored Feb 6, 2024
1 parent 66a8dab commit a3788c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest]
python-version: [3.8, 3.9, '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test = [
"pytest > 7.0.0",
"pydantic",
"numpy",
"napari; python_version < '3.11'",
"napari",
]
cbor = [
"cbor2"
Expand Down
10 changes: 9 additions & 1 deletion src/local_migrator/_serialize_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class BaseModel: # type: ignore [no-redef]
pass


try:
from pydantic.v1 import BaseModel as BaseModelV1
except ImportError:

class BaseModelV1: # type: ignore [no-redef]
pass


try:
from numpy import floating, integer, ndarray
except ImportError: # pragma: no cover
Expand Down Expand Up @@ -80,7 +88,7 @@ def object_encoder(obj: typing.Any): # noqa: PLR0911
if isinstance(obj, ndarray):
return obj.tolist()

if isinstance(obj, BaseModel):
if isinstance(obj, (BaseModel, BaseModelV1)):
try:
dkt = dict(obj)
except (ValueError, TypeError):
Expand Down

0 comments on commit a3788c6

Please sign in to comment.