Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add v1 namespace #8

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pydantic_compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"BaseModel",
]


from ._shared import PYDANTIC2

if TYPE_CHECKING:
Expand Down
16 changes: 16 additions & 0 deletions src/pydantic_compat/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""This module is a try/except pass-through for the pydantic.v1 namespace.
The pydantic.v1 namespace was added in pydantic v2. It contains the entire
pydantic v1 package. For packages that would like to unpin their 'pydantic<2'
dependency *without* needing to update all of their code to use the pydantic v2 api,
this provides a simple way to delay that update.
This is different from the goal of pydantic_compat on the whole, which is to provide
an API translation layer that allows you to actually use pydantic v2 features
(e.g. rust-backed speed, etc...) while also being compatible with packages that pin
pydantic<2.
"""
try:
from pydantic.v1 import * # noqa
except ImportError:
from pydantic import * # type: ignore # noqa
10 changes: 10 additions & 0 deletions tests/test_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ class Model(PydanticCompatMixin, pydantic.BaseModel):
# test extra
with pytest.raises((ValueError, TypeError)): # (v1, v2)
Model(extra=1)


def test_v1_namespace() -> None:
from pydantic_compat.v1 import BaseModel

class Model(BaseModel):
x: int = 1

m = Model()
assert m.dict() == {"x": 1} # no warning