-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This uses the `pydantic.v1` compat module (well really, it's just a in tree copy of the latest pydantic v1 release) so we can also support pydantic v2. Major changes to root models make it infeasible to support pydantic v2 natively without dropping support for v1.
- Loading branch information
Showing
21 changed files
with
130 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright (C) 2023 Maxwell G <[email protected]> | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or | ||
# https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
""" | ||
Utilities to help maintain compatibility between Pydantic v1 and Pydantic v2 | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
from typing import TYPE_CHECKING, Any | ||
|
||
import pydantic as p | ||
import pydantic.version | ||
from packaging.version import Version | ||
|
||
pydantic_version = Version(p.version.VERSION) | ||
HAS_PYDANTIC_V2 = pydantic_version.major == 2 | ||
|
||
if TYPE_CHECKING or HAS_PYDANTIC_V2: | ||
from pydantic import v1 | ||
|
||
else: | ||
v1 = p | ||
|
||
_PYDANTIC_FIELD_RENAMES: dict[str, tuple[str, Callable | None]] = { | ||
"min_items": ("min_length", None), | ||
"max_items": ("max_length", None), | ||
"regex": ("pattern", None), | ||
"allow_mutation": ("frozen", lambda v: not v), | ||
} | ||
|
||
|
||
def Field(*args: Any, **kwargs: Any) -> Any: | ||
""" | ||
Compatibility shim between pydantic v1 and pydantic v2's `Field`. | ||
""" | ||
|
||
if HAS_PYDANTIC_V2: | ||
for key, value in tuple(kwargs.items()): | ||
if key in _PYDANTIC_FIELD_RENAMES: | ||
new_key, transform = _PYDANTIC_FIELD_RENAMES[key] | ||
if transform: | ||
value = transform(value) | ||
kwargs[new_key] = value | ||
del kwargs[key] | ||
return pydantic.Field(*args, **kwargs) | ||
|
||
|
||
__all__ = ("pydantic_version", "HAS_PYDANTIC_V2", "Field", "v1") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters