From 33b2662c20de8047bc7f44de1cdbda7b73f76d3d Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Mon, 16 Oct 2023 09:20:17 -0400 Subject: [PATCH] feat: add v1 namespace (#8) * feat: add v1 namespace * add test * back to star * update module doc --- src/pydantic_compat/__init__.py | 1 + src/pydantic_compat/v1.py | 16 ++++++++++++++++ tests/test_base_model.py | 10 ++++++++++ 3 files changed, 27 insertions(+) create mode 100644 src/pydantic_compat/v1.py diff --git a/src/pydantic_compat/__init__.py b/src/pydantic_compat/__init__.py index 221899d..8f904c9 100644 --- a/src/pydantic_compat/__init__.py +++ b/src/pydantic_compat/__init__.py @@ -26,6 +26,7 @@ "BaseModel", ] + from ._shared import PYDANTIC2 if TYPE_CHECKING: diff --git a/src/pydantic_compat/v1.py b/src/pydantic_compat/v1.py new file mode 100644 index 0000000..d532dfb --- /dev/null +++ b/src/pydantic_compat/v1.py @@ -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 diff --git a/tests/test_base_model.py b/tests/test_base_model.py index 97fee39..abf989c 100644 --- a/tests/test_base_model.py +++ b/tests/test_base_model.py @@ -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