Skip to content

Commit

Permalink
feat: add v1 namespace (#8)
Browse files Browse the repository at this point in the history
* feat: add v1 namespace

* add test

* back to star

* update module doc
  • Loading branch information
tlambert03 authored Oct 16, 2023
1 parent c8b911b commit 33b2662
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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

0 comments on commit 33b2662

Please sign in to comment.