Skip to content

Commit

Permalink
add compat for pydantic v2 (#169)
Browse files Browse the repository at this point in the history
* add compat for pydantic v2

* add news

* fix test
  • Loading branch information
jaimergp authored Dec 11, 2023
1 parent d4f7eb0 commit 13d1c4d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
10 changes: 8 additions & 2 deletions menuinst/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
from pprint import pprint
from typing import Dict, List, Literal, Optional, Union

from pydantic import BaseModel as _BaseModel
from pydantic import Field, conlist, constr
try:
from pydantic.v1 import BaseModel as _BaseModel
from pydantic.v1 import Field, conlist, constr
except ImportError:
# pydantic v1
from pydantic import BaseModel as _BaseModel
from pydantic import Field, conlist, constr


log = getLogger(__name__)

Expand Down
19 changes: 19 additions & 0 deletions news/169-pydantic
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* Add support for `pydantic` v2 in `menuinst._schema`. (#166 via #169)
2 changes: 1 addition & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test:
- conda
- pytest
- pytest-cov
- pydantic <2.0a0
- pydantic
- hypothesis
- hypothesis-jsonschema
source_files:
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
python
pip
conda
pydantic<2.0a0
pydantic
pytest
pytest-cov
hypothesis
Expand Down
9 changes: 6 additions & 3 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

# from hypothesis import given, settings, HealthCheck
# from hypothesis_jsonschema import from_schema
from pydantic import ValidationError
from pydantic import ValidationError as ValidationErrorV2
from pydantic.v1 import ValidationError as ValidationErrorV1

from menuinst._schema import BasePlatformSpecific, MenuItem, validate

Expand All @@ -14,10 +15,12 @@
# assert value


@pytest.mark.parametrize("path", (DATA / "jsons").glob("*.json"))
@pytest.mark.parametrize(
"path", [pytest.param(path, id=path.name) for path in sorted((DATA / "jsons").glob("*.json"))]
)
def test_examples(path):
if "invalid" in path.name:
with pytest.raises(ValidationError):
with pytest.raises((ValidationErrorV1, ValidationErrorV2)):
assert validate(path)
else:
assert validate(path)
Expand Down

0 comments on commit 13d1c4d

Please sign in to comment.