-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
74 lines (66 loc) · 1.98 KB
/
Copy path__init__.py
File metadata and controls
74 lines (66 loc) · 1.98 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from importlib.metadata import version, PackageNotFoundError
from packaging.version import parse as parse_version
from .py_schema import (
PyApplication,
PyCallable,
PyCallableParameter,
PyClass,
PyClassAttribute,
PyComment,
PyExternalSymbol,
PyImport,
PyModule,
PyVariableDeclaration,
)
__all__ = [
"PyApplication",
"PyExternalSymbol",
"PyImport",
"PyComment",
"PyModule",
"PyClass",
"PyVariableDeclaration",
"PyCallable",
"PyClassAttribute",
"PyCallableParameter",
]
try:
pydantic_version = version("pydantic")
except PackageNotFoundError:
pydantic_version = "0.0.0" # fallback or raise if appropriate
PYDANTIC_V2 = parse_version(pydantic_version) >= parse_version("2.0.0")
if not PYDANTIC_V2:
# Safe to pass localns
PyCallable.update_forward_refs(PyClass=PyClass)
PyClass.update_forward_refs(PyCallable=PyCallable)
PyModule.update_forward_refs(PyCallable=PyCallable, PyClass=PyClass)
PyApplication.update_forward_refs(
PyCallable=PyCallable,
PyClass=PyClass,
PyModule=PyModule
)
# Compatibility helpers for Pydantic v1/v2
def model_dump_json(model, **kwargs):
"""Compatibility helper for JSON serialization."""
if PYDANTIC_V2:
return model.model_dump_json(**kwargs)
else:
# Map Pydantic v2 parameters to v1 equivalents
v1_kwargs = {}
if 'indent' in kwargs:
v1_kwargs['indent'] = kwargs['indent']
if 'separators' in kwargs:
# In v1, separators is passed to dumps_kwargs
v1_kwargs['separators'] = kwargs['separators']
return model.json(**v1_kwargs)
def model_validate_json(model_class, json_data):
"""Compatibility helper for JSON deserialization."""
if PYDANTIC_V2:
return model_class.model_validate_json(json_data)
else:
return model_class.parse_raw(json_data)
__all__.extend([
"PYDANTIC_V2",
"model_dump_json",
"model_validate_json"
])