forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompat.py
42 lines (28 loc) · 1.18 KB
/
compat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import functools
@functools.lru_cache(maxsize=None)
def pydantic_version() -> int:
from pydantic import __version__
version = __version__.split(".")
return int(version[0])
@functools.lru_cache(maxsize=None)
def disable_pydantic_v2_warning():
"""
Disables the Pydantic version 2 warning by filtering out the related warnings.
This function checks the version of Pydantic currently installed and if it is version 2,
it filters out the PydanticDeprecationWarning and PydanticDeprecatedSince20 warnings
to suppress them.
Since we only need to run the code once, we are using lru_cache to improve performance.
Note: This function assumes that Pydantic is installed.
Usage:
disable_pydantic_v2_warning()
"""
try:
from pydantic import __version__
version = __version__.split(".")
if int(version[0]) == 2:
import warnings
from pydantic import PydanticDeprecatedSince20, PydanticDeprecationWarning
warnings.filterwarnings("ignore", category=PydanticDeprecationWarning)
warnings.filterwarnings("ignore", category=PydanticDeprecatedSince20)
except ImportError:
pass