-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathpydantic_patch.py
More file actions
29 lines (21 loc) · 1.01 KB
/
Copy pathpydantic_patch.py
File metadata and controls
29 lines (21 loc) · 1.01 KB
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
"""Pydantic compatibility patches for Python 3.12+.
Patches pydantic.typing.evaluate_forwardref for forward reference evaluation
compatibility with newer Python versions.
"""
from __future__ import annotations
import sys
from typing import Any
def patched_evaluate_forwardref(
forward_ref: Any, globalns: dict[str, Any], localns: dict[str, Any] | None = None
) -> None: # pragma: no cover
"""Evaluate a forward reference with Python 3.12+ compatibility."""
try:
return forward_ref._evaluate(globalns, localns or None, set()) # pragma: no cover # noqa: SLF001
except TypeError:
# Fallback for Python 3.12 compatibility
return forward_ref._evaluate(globalns, localns or None, set(), recursive_guard=set()) # noqa: SLF001
def apply_patch() -> None:
"""Apply the pydantic patch for Python 3.12+ if needed."""
if sys.version_info >= (3, 12):
import pydantic.typing # noqa: PLC0415
pydantic.typing.evaluate_forwardref = patched_evaluate_forwardref # ty: ignore