Skip to content

Commit e41b0f0

Browse files
yan12125ssenchenkohoffa
authored
chore: add support for pydantic 2.x (#3282)
Co-authored-by: Slava Senchenko <[email protected]> Co-authored-by: Christoffer Rehn <[email protected]>
1 parent 5500f10 commit e41b0f0

File tree

6 files changed

+18
-15
lines changed

6 files changed

+18
-15
lines changed

requirements/base.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ jsonschema<5,>=3.2 # TODO: evaluate risk of removing jsonschema 3.x support
33
typing_extensions>=4.4,<5 # 3.7 doesn't have Literal
44

55
# resource validation & schema generation
6-
pydantic~=1.8
6+
pydantic>=1.8,<3

samtranslator/compat.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
try:
2+
from pydantic import v1 as pydantic
3+
except ImportError:
4+
# Unfortunately mypy cannot handle this try/expect pattern, and "type: ignore"
5+
# is the simplest work-around. See: https://github.com/python/mypy/issues/1153
6+
import pydantic # type: ignore
7+
8+
__all__ = ["pydantic"]

samtranslator/internal/schema_source/any_cfn_resource.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import pydantic
2-
1+
from samtranslator.compat import pydantic
32
from samtranslator.internal.schema_source.common import LenientBaseModel
43

54
constr = pydantic.constr

samtranslator/internal/schema_source/common.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from pathlib import Path
44
from typing import Any, Dict, List, Optional, TypeVar, Union
55

6-
import pydantic
7-
from pydantic import Extra, Field
86
from typing_extensions import Literal
97

8+
from samtranslator.compat import pydantic
109
from samtranslator.model.types import PassThrough
1110

1211

@@ -53,7 +52,7 @@ def passthrough_prop(sam_docs_stem: str, sam_docs_name: str, prop_path: List[str
5352
for s in prop_path[1:]:
5453
path.extend(["properties", s])
5554
docs = _DOCS["properties"][sam_docs_stem][sam_docs_name]
56-
return Field(
55+
return pydantic.Field(
5756
title=sam_docs_name,
5857
# We add a custom value to the schema containing the path to the pass-through
5958
# documentation; the dict containing the value is replaced in the final schema
@@ -68,7 +67,7 @@ def passthrough_prop(sam_docs_stem: str, sam_docs_name: str, prop_path: List[str
6867

6968
def _get_prop(stem: str, name: str) -> Any:
7069
docs = _DOCS["properties"][stem][name]
71-
return Field(
70+
return pydantic.Field(
7271
title=name,
7372
# https://code.visualstudio.com/docs/languages/json#_use-rich-formatting-in-hovers
7473
markdownDescription=docs,
@@ -78,7 +77,7 @@ def _get_prop(stem: str, name: str) -> Any:
7877
# By default strict: https://pydantic-docs.helpmanual.io/usage/model_config/#change-behaviour-globally
7978
class BaseModel(LenientBaseModel):
8079
class Config:
81-
extra = Extra.forbid
80+
extra = pydantic.Extra.forbid
8281

8382
def __getattribute__(self, __name: str) -> Any:
8483
"""Overloading get attribute operation to allow access PassThroughProp without using __root__"""

samtranslator/internal/schema_source/schema.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from pathlib import Path
77
from typing import Any, Callable, Dict, List, Optional, Type, Union
88

9-
import pydantic
10-
9+
from samtranslator.compat import pydantic
1110
from samtranslator.internal.schema_source import (
1211
any_cfn_resource,
1312
aws_serverless_api,

samtranslator/model/__init__.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
from contextlib import suppress
66
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, TypeVar
77

8-
from pydantic import BaseModel
9-
from pydantic.error_wrappers import ValidationError
10-
8+
from samtranslator.compat import pydantic
119
from samtranslator.model.exceptions import (
1210
ExpectedType,
1311
InvalidResourceException,
@@ -17,7 +15,7 @@
1715
from samtranslator.model.types import IS_DICT, IS_STR, PassThrough, Validator, any_type, is_type
1816
from samtranslator.plugins import LifeCycleEvents
1917

20-
RT = TypeVar("RT", bound=BaseModel) # return type
18+
RT = TypeVar("RT", bound=pydantic.BaseModel) # return type
2119

2220

2321
class PropertyType:
@@ -348,7 +346,7 @@ def validate_properties_and_return_model(self, cls: Type[RT]) -> RT:
348346
"""
349347
try:
350348
return cls.parse_obj(self._generate_resource_dict()["Properties"])
351-
except ValidationError as e:
349+
except pydantic.error_wrappers.ValidationError as e:
352350
error_properties: str = ""
353351
with suppress(KeyError):
354352
error_properties = ".".join(str(x) for x in e.errors()[0]["loc"])

0 commit comments

Comments
 (0)