methodtenable 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- methodtenable-0.0.1/PKG-INFO +26 -0
- methodtenable-0.0.1/README.md +0 -0
- methodtenable-0.0.1/pyproject.toml +79 -0
- methodtenable-0.0.1/src/methodtenable/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/core/__init__.py +27 -0
- methodtenable-0.0.1/src/methodtenable/core/datetime_utils.py +28 -0
- methodtenable-0.0.1/src/methodtenable/core/pydantic_utilities.py +226 -0
- methodtenable-0.0.1/src/methodtenable/core/serialization.py +276 -0
- methodtenable-0.0.1/src/methodtenable/py.typed +0 -0
- methodtenable-0.0.1/src/methodtenable/resources/__init__.py +8 -0
- methodtenable-0.0.1/src/methodtenable/resources/common/__init__.py +9 -0
- methodtenable-0.0.1/src/methodtenable/resources/common/secret_config.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/common/severity.py +5 -0
- methodtenable-0.0.1/src/methodtenable/resources/common/state.py +5 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/__init__.py +41 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/__init__.py +41 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/__init__.py +37 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_asset_export_chunk_request.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_asset_export_chunk_response.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_asset_export_filters.py +33 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_asset_export_status_request.py +17 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_asset_export_status_response.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_asset_v_2_export_request.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_asset_v_2_export_response.py +17 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/api_vm_assset_export_report.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/asset_network.py +23 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/asset_scan.py +23 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/asset_source.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/asset_tag.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/asset_timestamps.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/network_interface.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/asset/resources/export/tenable_asset.py +35 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/__init__.py +41 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/__init__.py +41 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/__init__.py +37 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_chunk_request.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_chunk_response.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_filters.py +25 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_report.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_request.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_response.py +17 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_status_request.py +17 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/api_vm_vulnerability_export_status_response.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/tenable_vulnerability.py +38 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/vulnerability_asset.py +33 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/vulnerability_plugin.py +77 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/vulnerability_port.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/vulnerability_scan.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/vulnerability_severity.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/vm/resources/vulnerability/resources/export/vulnerability_tag.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/__init__.py +39 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/__init__.py +39 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/__init__.py +35 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_chunk_request.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_chunk_response.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_filters.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_report.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_request.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_response.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_status_request.py +17 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/api_was_findings_export_status_response.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/was_attachment.py +22 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/was_finding.py +55 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/was_finding_asset.py +24 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/was_finding_details.py +26 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/was_finding_plugin.py +65 -0
- methodtenable-0.0.1/src/methodtenable/resources/utils/resources/api/resources/was/resources/finding/resources/export/was_finding_scan.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/__init__.py +27 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/__init__.py +27 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/__init__.py +23 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/asset.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/asset_details.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/asset_export_result.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/asset_list.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/asset_type.py +5 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/source_type.py +5 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/vm_asset_export_config.py +37 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/asset/resources/export/vm_asset_export_report.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/__init__.py +29 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/__init__.py +29 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/__init__.py +25 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vm_vulnerability_export_config.py +34 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vm_vulnerability_export_report.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vulnerability.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vulnerability_asset_info.py +25 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vulnerability_details.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vulnerability_export_result.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vulnerability_info.py +28 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vulnerability_list.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/vm/resources/vulnerability/resources/export/vulnerability_port.py +19 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/__init__.py +7 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/__init__.py +29 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/__init__.py +29 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/__init__.py +25 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_asset.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_finding.py +25 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_finding_details.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_finding_info.py +20 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_finding_list.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_findings_export_config.py +29 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_findings_export_report.py +21 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_findings_export_result.py +18 -0
- methodtenable-0.0.1/src/methodtenable/resources/was/resources/finding/resources/export/was_http_method.py +7 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: methodtenable
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary:
|
|
5
|
+
Requires-Python: >=3.8,<4.0
|
|
6
|
+
Classifier: Intended Audience :: Developers
|
|
7
|
+
Classifier: Operating System :: MacOS
|
|
8
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Operating System :: POSIX
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Dist: pydantic (>=1.9.2)
|
|
23
|
+
Requires-Dist: pydantic-core (>=2.18.2)
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
|
|
File without changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "methodtenable"
|
|
3
|
+
|
|
4
|
+
[tool.poetry]
|
|
5
|
+
name = "methodtenable"
|
|
6
|
+
version = "v0.0.1"
|
|
7
|
+
description = ""
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
authors = []
|
|
10
|
+
keywords = []
|
|
11
|
+
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Programming Language :: Python",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3.8",
|
|
17
|
+
"Programming Language :: Python :: 3.9",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Operating System :: POSIX",
|
|
23
|
+
"Operating System :: MacOS",
|
|
24
|
+
"Operating System :: POSIX :: Linux",
|
|
25
|
+
"Operating System :: Microsoft :: Windows",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
"Typing :: Typed"
|
|
28
|
+
]
|
|
29
|
+
packages = [
|
|
30
|
+
{ include = "methodtenable", from = "src"}
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[tool.poetry.dependencies]
|
|
34
|
+
python = "^3.8"
|
|
35
|
+
pydantic = ">= 1.9.2"
|
|
36
|
+
pydantic-core = ">=2.18.2"
|
|
37
|
+
|
|
38
|
+
[tool.poetry.group.dev.dependencies]
|
|
39
|
+
mypy = "==1.13.0"
|
|
40
|
+
pytest = "^7.4.0"
|
|
41
|
+
pytest-asyncio = "^0.23.5"
|
|
42
|
+
python-dateutil = "^2.9.0"
|
|
43
|
+
types-python-dateutil = "^2.9.0.20240316"
|
|
44
|
+
ruff = "==0.11.5"
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
testpaths = [ "tests" ]
|
|
48
|
+
asyncio_mode = "auto"
|
|
49
|
+
|
|
50
|
+
[tool.mypy]
|
|
51
|
+
plugins = ["pydantic.mypy"]
|
|
52
|
+
|
|
53
|
+
[tool.ruff]
|
|
54
|
+
line-length = 120
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint]
|
|
57
|
+
select = [
|
|
58
|
+
"E", # pycodestyle errors
|
|
59
|
+
"F", # pyflakes
|
|
60
|
+
"I", # isort
|
|
61
|
+
]
|
|
62
|
+
ignore = [
|
|
63
|
+
"E402", # Module level import not at top of file
|
|
64
|
+
"E501", # Line too long
|
|
65
|
+
"E711", # Comparison to `None` should be `cond is not None`
|
|
66
|
+
"E712", # Avoid equality comparisons to `True`; use `if ...:` checks
|
|
67
|
+
"E721", # Use `is` and `is not` for type comparisons, or `isinstance()` for insinstance checks
|
|
68
|
+
"E722", # Do not use bare `except`
|
|
69
|
+
"E731", # Do not assign a `lambda` expression, use a `def`
|
|
70
|
+
"F821", # Undefined name
|
|
71
|
+
"F841" # Local variable ... is assigned to but never used
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint.isort]
|
|
75
|
+
section-order = ["future", "standard-library", "third-party", "first-party"]
|
|
76
|
+
|
|
77
|
+
[build-system]
|
|
78
|
+
requires = ["poetry-core"]
|
|
79
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
# isort: skip_file
|
|
4
|
+
|
|
5
|
+
from .datetime_utils import serialize_datetime
|
|
6
|
+
from .pydantic_utilities import (
|
|
7
|
+
IS_PYDANTIC_V2,
|
|
8
|
+
UniversalBaseModel,
|
|
9
|
+
UniversalRootModel,
|
|
10
|
+
parse_obj_as,
|
|
11
|
+
universal_field_validator,
|
|
12
|
+
universal_root_validator,
|
|
13
|
+
update_forward_refs,
|
|
14
|
+
)
|
|
15
|
+
from .serialization import FieldMetadata
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"FieldMetadata",
|
|
19
|
+
"IS_PYDANTIC_V2",
|
|
20
|
+
"UniversalBaseModel",
|
|
21
|
+
"UniversalRootModel",
|
|
22
|
+
"parse_obj_as",
|
|
23
|
+
"serialize_datetime",
|
|
24
|
+
"universal_field_validator",
|
|
25
|
+
"universal_root_validator",
|
|
26
|
+
"update_forward_refs",
|
|
27
|
+
]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
import datetime as dt
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def serialize_datetime(v: dt.datetime) -> str:
|
|
7
|
+
"""
|
|
8
|
+
Serialize a datetime including timezone info.
|
|
9
|
+
|
|
10
|
+
Uses the timezone info provided if present, otherwise uses the current runtime's timezone info.
|
|
11
|
+
|
|
12
|
+
UTC datetimes end in "Z" while all other timezones are represented as offset from UTC, e.g. +05:00.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def _serialize_zoned_datetime(v: dt.datetime) -> str:
|
|
16
|
+
if v.tzinfo is not None and v.tzinfo.tzname(None) == dt.timezone.utc.tzname(None):
|
|
17
|
+
# UTC is a special case where we use "Z" at the end instead of "+00:00"
|
|
18
|
+
return v.isoformat().replace("+00:00", "Z")
|
|
19
|
+
else:
|
|
20
|
+
# Delegate to the typical +/- offset format
|
|
21
|
+
return v.isoformat()
|
|
22
|
+
|
|
23
|
+
if v.tzinfo is not None:
|
|
24
|
+
return _serialize_zoned_datetime(v)
|
|
25
|
+
else:
|
|
26
|
+
local_tz = dt.datetime.now().astimezone().tzinfo
|
|
27
|
+
localized_dt = v.replace(tzinfo=local_tz)
|
|
28
|
+
return _serialize_zoned_datetime(localized_dt)
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
# nopycln: file
|
|
4
|
+
import datetime as dt
|
|
5
|
+
from collections import defaultdict
|
|
6
|
+
from typing import Any, Callable, Dict, List, Mapping, Tuple, Type, TypeVar, Union, cast
|
|
7
|
+
|
|
8
|
+
import pydantic
|
|
9
|
+
|
|
10
|
+
IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.")
|
|
11
|
+
|
|
12
|
+
if IS_PYDANTIC_V2:
|
|
13
|
+
from pydantic.v1.datetime_parse import parse_date as parse_date
|
|
14
|
+
from pydantic.v1.datetime_parse import parse_datetime as parse_datetime
|
|
15
|
+
from pydantic.v1.fields import ModelField as ModelField
|
|
16
|
+
from pydantic.v1.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore[attr-defined]
|
|
17
|
+
from pydantic.v1.typing import get_args as get_args
|
|
18
|
+
from pydantic.v1.typing import get_origin as get_origin
|
|
19
|
+
from pydantic.v1.typing import is_literal_type as is_literal_type
|
|
20
|
+
from pydantic.v1.typing import is_union as is_union
|
|
21
|
+
else:
|
|
22
|
+
from pydantic.datetime_parse import parse_date as parse_date # type: ignore[no-redef]
|
|
23
|
+
from pydantic.datetime_parse import parse_datetime as parse_datetime # type: ignore[no-redef]
|
|
24
|
+
from pydantic.fields import ModelField as ModelField # type: ignore[attr-defined, no-redef]
|
|
25
|
+
from pydantic.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore[no-redef]
|
|
26
|
+
from pydantic.typing import get_args as get_args # type: ignore[no-redef]
|
|
27
|
+
from pydantic.typing import get_origin as get_origin # type: ignore[no-redef]
|
|
28
|
+
from pydantic.typing import is_literal_type as is_literal_type # type: ignore[no-redef]
|
|
29
|
+
from pydantic.typing import is_union as is_union # type: ignore[no-redef]
|
|
30
|
+
|
|
31
|
+
from .datetime_utils import serialize_datetime
|
|
32
|
+
from typing_extensions import TypeAlias
|
|
33
|
+
|
|
34
|
+
T = TypeVar("T")
|
|
35
|
+
Model = TypeVar("Model", bound=pydantic.BaseModel)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def parse_obj_as(type_: Type[T], object_: Any) -> T:
|
|
39
|
+
if IS_PYDANTIC_V2:
|
|
40
|
+
adapter = pydantic.TypeAdapter(type_) # type: ignore[attr-defined]
|
|
41
|
+
return adapter.validate_python(object_)
|
|
42
|
+
return pydantic.parse_obj_as(type_, object_)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def to_jsonable_with_fallback(obj: Any, fallback_serializer: Callable[[Any], Any]) -> Any:
|
|
46
|
+
if IS_PYDANTIC_V2:
|
|
47
|
+
from pydantic_core import to_jsonable_python
|
|
48
|
+
|
|
49
|
+
return to_jsonable_python(obj, fallback=fallback_serializer)
|
|
50
|
+
return fallback_serializer(obj)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class UniversalBaseModel(pydantic.BaseModel):
|
|
54
|
+
class Config:
|
|
55
|
+
populate_by_name = True
|
|
56
|
+
smart_union = True
|
|
57
|
+
allow_population_by_field_name = True
|
|
58
|
+
json_encoders = {dt.datetime: serialize_datetime}
|
|
59
|
+
# Allow fields beginning with `model_` to be used in the model
|
|
60
|
+
protected_namespaces = ()
|
|
61
|
+
|
|
62
|
+
def json(self, **kwargs: Any) -> str:
|
|
63
|
+
kwargs_with_defaults = {
|
|
64
|
+
"by_alias": True,
|
|
65
|
+
"exclude_unset": True,
|
|
66
|
+
**kwargs,
|
|
67
|
+
}
|
|
68
|
+
if IS_PYDANTIC_V2:
|
|
69
|
+
return super().model_dump_json(**kwargs_with_defaults) # type: ignore[misc]
|
|
70
|
+
return super().json(**kwargs_with_defaults)
|
|
71
|
+
|
|
72
|
+
def dict(self, **kwargs: Any) -> Dict[str, Any]:
|
|
73
|
+
"""
|
|
74
|
+
Override the default dict method to `exclude_unset` by default. This function patches
|
|
75
|
+
`exclude_unset` to work include fields within non-None default values.
|
|
76
|
+
"""
|
|
77
|
+
# Note: the logic here is multiplexed given the levers exposed in Pydantic V1 vs V2
|
|
78
|
+
# Pydantic V1's .dict can be extremely slow, so we do not want to call it twice.
|
|
79
|
+
#
|
|
80
|
+
# We'd ideally do the same for Pydantic V2, but it shells out to a library to serialize models
|
|
81
|
+
# that we have less control over, and this is less intrusive than custom serializers for now.
|
|
82
|
+
if IS_PYDANTIC_V2:
|
|
83
|
+
kwargs_with_defaults_exclude_unset = {
|
|
84
|
+
**kwargs,
|
|
85
|
+
"by_alias": True,
|
|
86
|
+
"exclude_unset": True,
|
|
87
|
+
"exclude_none": False,
|
|
88
|
+
}
|
|
89
|
+
kwargs_with_defaults_exclude_none = {
|
|
90
|
+
**kwargs,
|
|
91
|
+
"by_alias": True,
|
|
92
|
+
"exclude_none": True,
|
|
93
|
+
"exclude_unset": False,
|
|
94
|
+
}
|
|
95
|
+
return deep_union_pydantic_dicts(
|
|
96
|
+
super().model_dump(**kwargs_with_defaults_exclude_unset), # type: ignore[misc]
|
|
97
|
+
super().model_dump(**kwargs_with_defaults_exclude_none), # type: ignore[misc]
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
_fields_set = self.__fields_set__.copy()
|
|
101
|
+
|
|
102
|
+
fields = _get_model_fields(self.__class__)
|
|
103
|
+
for name, field in fields.items():
|
|
104
|
+
if name not in _fields_set:
|
|
105
|
+
default = _get_field_default(field)
|
|
106
|
+
|
|
107
|
+
# If the default values are non-null act like they've been set
|
|
108
|
+
# This effectively allows exclude_unset to work like exclude_none where
|
|
109
|
+
# the latter passes through intentionally set none values.
|
|
110
|
+
if default is not None or ("exclude_unset" in kwargs and not kwargs["exclude_unset"]):
|
|
111
|
+
_fields_set.add(name)
|
|
112
|
+
|
|
113
|
+
if default is not None:
|
|
114
|
+
self.__fields_set__.add(name)
|
|
115
|
+
|
|
116
|
+
kwargs_with_defaults_exclude_unset_include_fields = {
|
|
117
|
+
"by_alias": True,
|
|
118
|
+
"exclude_unset": True,
|
|
119
|
+
"include": _fields_set,
|
|
120
|
+
**kwargs,
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return super().dict(**kwargs_with_defaults_exclude_unset_include_fields)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _union_list_of_pydantic_dicts(source: List[Any], destination: List[Any]) -> List[Any]:
|
|
127
|
+
converted_list: List[Any] = []
|
|
128
|
+
for i, item in enumerate(source):
|
|
129
|
+
destination_value = destination[i]
|
|
130
|
+
if isinstance(item, dict):
|
|
131
|
+
converted_list.append(deep_union_pydantic_dicts(item, destination_value))
|
|
132
|
+
elif isinstance(item, list):
|
|
133
|
+
converted_list.append(_union_list_of_pydantic_dicts(item, destination_value))
|
|
134
|
+
else:
|
|
135
|
+
converted_list.append(item)
|
|
136
|
+
return converted_list
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def deep_union_pydantic_dicts(source: Dict[str, Any], destination: Dict[str, Any]) -> Dict[str, Any]:
|
|
140
|
+
for key, value in source.items():
|
|
141
|
+
node = destination.setdefault(key, {})
|
|
142
|
+
if isinstance(value, dict):
|
|
143
|
+
deep_union_pydantic_dicts(value, node)
|
|
144
|
+
# Note: we do not do this same processing for sets given we do not have sets of models
|
|
145
|
+
# and given the sets are unordered, the processing of the set and matching objects would
|
|
146
|
+
# be non-trivial.
|
|
147
|
+
elif isinstance(value, list):
|
|
148
|
+
destination[key] = _union_list_of_pydantic_dicts(value, node)
|
|
149
|
+
else:
|
|
150
|
+
destination[key] = value
|
|
151
|
+
|
|
152
|
+
return destination
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
if IS_PYDANTIC_V2:
|
|
156
|
+
|
|
157
|
+
class V2RootModel(UniversalBaseModel, pydantic.RootModel): # type: ignore[name-defined, type-arg]
|
|
158
|
+
pass
|
|
159
|
+
|
|
160
|
+
UniversalRootModel: TypeAlias = V2RootModel
|
|
161
|
+
else:
|
|
162
|
+
UniversalRootModel: TypeAlias = UniversalBaseModel # type: ignore[misc, no-redef]
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def encode_by_type(o: Any) -> Any:
|
|
166
|
+
encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(tuple)
|
|
167
|
+
for type_, encoder in encoders_by_type.items():
|
|
168
|
+
encoders_by_class_tuples[encoder] += (type_,)
|
|
169
|
+
|
|
170
|
+
if type(o) in encoders_by_type:
|
|
171
|
+
return encoders_by_type[type(o)](o)
|
|
172
|
+
for encoder, classes_tuple in encoders_by_class_tuples.items():
|
|
173
|
+
if isinstance(o, classes_tuple):
|
|
174
|
+
return encoder(o)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def update_forward_refs(model: Type["Model"], **localns: Any) -> None:
|
|
178
|
+
if IS_PYDANTIC_V2:
|
|
179
|
+
model.model_rebuild(raise_errors=False) # type: ignore[attr-defined]
|
|
180
|
+
else:
|
|
181
|
+
model.update_forward_refs(**localns)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# Mirrors Pydantic's internal typing
|
|
185
|
+
AnyCallable = Callable[..., Any]
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def universal_root_validator(pre: bool = False) -> Callable[[AnyCallable], AnyCallable]:
|
|
189
|
+
def decorator(func: AnyCallable) -> AnyCallable:
|
|
190
|
+
if IS_PYDANTIC_V2:
|
|
191
|
+
return cast(AnyCallable, pydantic.model_validator(mode="before" if pre else "after")(func)) # type: ignore[attr-defined]
|
|
192
|
+
return cast(AnyCallable, pydantic.root_validator(pre=pre)(func)) # type: ignore[call-overload]
|
|
193
|
+
|
|
194
|
+
return decorator
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def universal_field_validator(field_name: str, pre: bool = False) -> Callable[[AnyCallable], AnyCallable]:
|
|
198
|
+
def decorator(func: AnyCallable) -> AnyCallable:
|
|
199
|
+
if IS_PYDANTIC_V2:
|
|
200
|
+
return cast(AnyCallable, pydantic.field_validator(field_name, mode="before" if pre else "after")(func)) # type: ignore[attr-defined]
|
|
201
|
+
return cast(AnyCallable, pydantic.validator(field_name, pre=pre)(func))
|
|
202
|
+
|
|
203
|
+
return decorator
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
PydanticField = Union[ModelField, pydantic.fields.FieldInfo]
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _get_model_fields(model: Type["Model"]) -> Mapping[str, PydanticField]:
|
|
210
|
+
if IS_PYDANTIC_V2:
|
|
211
|
+
return cast(Mapping[str, PydanticField], model.model_fields) # type: ignore[attr-defined]
|
|
212
|
+
return cast(Mapping[str, PydanticField], model.__fields__)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _get_field_default(field: PydanticField) -> Any:
|
|
216
|
+
try:
|
|
217
|
+
value = field.get_default() # type: ignore[union-attr]
|
|
218
|
+
except:
|
|
219
|
+
value = field.default
|
|
220
|
+
if IS_PYDANTIC_V2:
|
|
221
|
+
from pydantic_core import PydanticUndefined
|
|
222
|
+
|
|
223
|
+
if value == PydanticUndefined:
|
|
224
|
+
return None
|
|
225
|
+
return value
|
|
226
|
+
return value
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
import collections
|
|
4
|
+
import inspect
|
|
5
|
+
import typing
|
|
6
|
+
|
|
7
|
+
import pydantic
|
|
8
|
+
import typing_extensions
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FieldMetadata:
|
|
12
|
+
"""
|
|
13
|
+
Metadata class used to annotate fields to provide additional information.
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
class MyDict(TypedDict):
|
|
17
|
+
field: typing.Annotated[str, FieldMetadata(alias="field_name")]
|
|
18
|
+
|
|
19
|
+
Will serialize: `{"field": "value"}`
|
|
20
|
+
To: `{"field_name": "value"}`
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
alias: str
|
|
24
|
+
|
|
25
|
+
def __init__(self, *, alias: str) -> None:
|
|
26
|
+
self.alias = alias
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def convert_and_respect_annotation_metadata(
|
|
30
|
+
*,
|
|
31
|
+
object_: typing.Any,
|
|
32
|
+
annotation: typing.Any,
|
|
33
|
+
inner_type: typing.Optional[typing.Any] = None,
|
|
34
|
+
direction: typing.Literal["read", "write"],
|
|
35
|
+
) -> typing.Any:
|
|
36
|
+
"""
|
|
37
|
+
Respect the metadata annotations on a field, such as aliasing. This function effectively
|
|
38
|
+
manipulates the dict-form of an object to respect the metadata annotations. This is primarily used for
|
|
39
|
+
TypedDicts, which cannot support aliasing out of the box, and can be extended for additional
|
|
40
|
+
utilities, such as defaults.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
object_ : typing.Any
|
|
45
|
+
|
|
46
|
+
annotation : type
|
|
47
|
+
The type we're looking to apply typing annotations from
|
|
48
|
+
|
|
49
|
+
inner_type : typing.Optional[type]
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
typing.Any
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
if object_ is None:
|
|
57
|
+
return None
|
|
58
|
+
if inner_type is None:
|
|
59
|
+
inner_type = annotation
|
|
60
|
+
|
|
61
|
+
clean_type = _remove_annotations(inner_type)
|
|
62
|
+
# Pydantic models
|
|
63
|
+
if (
|
|
64
|
+
inspect.isclass(clean_type)
|
|
65
|
+
and issubclass(clean_type, pydantic.BaseModel)
|
|
66
|
+
and isinstance(object_, typing.Mapping)
|
|
67
|
+
):
|
|
68
|
+
return _convert_mapping(object_, clean_type, direction)
|
|
69
|
+
# TypedDicts
|
|
70
|
+
if typing_extensions.is_typeddict(clean_type) and isinstance(object_, typing.Mapping):
|
|
71
|
+
return _convert_mapping(object_, clean_type, direction)
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
typing_extensions.get_origin(clean_type) == typing.Dict
|
|
75
|
+
or typing_extensions.get_origin(clean_type) == dict
|
|
76
|
+
or clean_type == typing.Dict
|
|
77
|
+
) and isinstance(object_, typing.Dict):
|
|
78
|
+
key_type = typing_extensions.get_args(clean_type)[0]
|
|
79
|
+
value_type = typing_extensions.get_args(clean_type)[1]
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
key: convert_and_respect_annotation_metadata(
|
|
83
|
+
object_=value,
|
|
84
|
+
annotation=annotation,
|
|
85
|
+
inner_type=value_type,
|
|
86
|
+
direction=direction,
|
|
87
|
+
)
|
|
88
|
+
for key, value in object_.items()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
# If you're iterating on a string, do not bother to coerce it to a sequence.
|
|
92
|
+
if not isinstance(object_, str):
|
|
93
|
+
if (
|
|
94
|
+
typing_extensions.get_origin(clean_type) == typing.Set
|
|
95
|
+
or typing_extensions.get_origin(clean_type) == set
|
|
96
|
+
or clean_type == typing.Set
|
|
97
|
+
) and isinstance(object_, typing.Set):
|
|
98
|
+
inner_type = typing_extensions.get_args(clean_type)[0]
|
|
99
|
+
return {
|
|
100
|
+
convert_and_respect_annotation_metadata(
|
|
101
|
+
object_=item,
|
|
102
|
+
annotation=annotation,
|
|
103
|
+
inner_type=inner_type,
|
|
104
|
+
direction=direction,
|
|
105
|
+
)
|
|
106
|
+
for item in object_
|
|
107
|
+
}
|
|
108
|
+
elif (
|
|
109
|
+
(
|
|
110
|
+
typing_extensions.get_origin(clean_type) == typing.List
|
|
111
|
+
or typing_extensions.get_origin(clean_type) == list
|
|
112
|
+
or clean_type == typing.List
|
|
113
|
+
)
|
|
114
|
+
and isinstance(object_, typing.List)
|
|
115
|
+
) or (
|
|
116
|
+
(
|
|
117
|
+
typing_extensions.get_origin(clean_type) == typing.Sequence
|
|
118
|
+
or typing_extensions.get_origin(clean_type) == collections.abc.Sequence
|
|
119
|
+
or clean_type == typing.Sequence
|
|
120
|
+
)
|
|
121
|
+
and isinstance(object_, typing.Sequence)
|
|
122
|
+
):
|
|
123
|
+
inner_type = typing_extensions.get_args(clean_type)[0]
|
|
124
|
+
return [
|
|
125
|
+
convert_and_respect_annotation_metadata(
|
|
126
|
+
object_=item,
|
|
127
|
+
annotation=annotation,
|
|
128
|
+
inner_type=inner_type,
|
|
129
|
+
direction=direction,
|
|
130
|
+
)
|
|
131
|
+
for item in object_
|
|
132
|
+
]
|
|
133
|
+
|
|
134
|
+
if typing_extensions.get_origin(clean_type) == typing.Union:
|
|
135
|
+
# We should be able to ~relatively~ safely try to convert keys against all
|
|
136
|
+
# member types in the union, the edge case here is if one member aliases a field
|
|
137
|
+
# of the same name to a different name from another member
|
|
138
|
+
# Or if another member aliases a field of the same name that another member does not.
|
|
139
|
+
for member in typing_extensions.get_args(clean_type):
|
|
140
|
+
object_ = convert_and_respect_annotation_metadata(
|
|
141
|
+
object_=object_,
|
|
142
|
+
annotation=annotation,
|
|
143
|
+
inner_type=member,
|
|
144
|
+
direction=direction,
|
|
145
|
+
)
|
|
146
|
+
return object_
|
|
147
|
+
|
|
148
|
+
annotated_type = _get_annotation(annotation)
|
|
149
|
+
if annotated_type is None:
|
|
150
|
+
return object_
|
|
151
|
+
|
|
152
|
+
# If the object is not a TypedDict, a Union, or other container (list, set, sequence, etc.)
|
|
153
|
+
# Then we can safely call it on the recursive conversion.
|
|
154
|
+
return object_
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _convert_mapping(
|
|
158
|
+
object_: typing.Mapping[str, object],
|
|
159
|
+
expected_type: typing.Any,
|
|
160
|
+
direction: typing.Literal["read", "write"],
|
|
161
|
+
) -> typing.Mapping[str, object]:
|
|
162
|
+
converted_object: typing.Dict[str, object] = {}
|
|
163
|
+
try:
|
|
164
|
+
annotations = typing_extensions.get_type_hints(expected_type, include_extras=True)
|
|
165
|
+
except NameError:
|
|
166
|
+
# The TypedDict contains a circular reference, so
|
|
167
|
+
# we use the __annotations__ attribute directly.
|
|
168
|
+
annotations = getattr(expected_type, "__annotations__", {})
|
|
169
|
+
aliases_to_field_names = _get_alias_to_field_name(annotations)
|
|
170
|
+
for key, value in object_.items():
|
|
171
|
+
if direction == "read" and key in aliases_to_field_names:
|
|
172
|
+
dealiased_key = aliases_to_field_names.get(key)
|
|
173
|
+
if dealiased_key is not None:
|
|
174
|
+
type_ = annotations.get(dealiased_key)
|
|
175
|
+
else:
|
|
176
|
+
type_ = annotations.get(key)
|
|
177
|
+
# Note you can't get the annotation by the field name if you're in read mode, so you must check the aliases map
|
|
178
|
+
#
|
|
179
|
+
# So this is effectively saying if we're in write mode, and we don't have a type, or if we're in read mode and we don't have an alias
|
|
180
|
+
# then we can just pass the value through as is
|
|
181
|
+
if type_ is None:
|
|
182
|
+
converted_object[key] = value
|
|
183
|
+
elif direction == "read" and key not in aliases_to_field_names:
|
|
184
|
+
converted_object[key] = convert_and_respect_annotation_metadata(
|
|
185
|
+
object_=value, annotation=type_, direction=direction
|
|
186
|
+
)
|
|
187
|
+
else:
|
|
188
|
+
converted_object[_alias_key(key, type_, direction, aliases_to_field_names)] = (
|
|
189
|
+
convert_and_respect_annotation_metadata(object_=value, annotation=type_, direction=direction)
|
|
190
|
+
)
|
|
191
|
+
return converted_object
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _get_annotation(type_: typing.Any) -> typing.Optional[typing.Any]:
|
|
195
|
+
maybe_annotated_type = typing_extensions.get_origin(type_)
|
|
196
|
+
if maybe_annotated_type is None:
|
|
197
|
+
return None
|
|
198
|
+
|
|
199
|
+
if maybe_annotated_type == typing_extensions.NotRequired:
|
|
200
|
+
type_ = typing_extensions.get_args(type_)[0]
|
|
201
|
+
maybe_annotated_type = typing_extensions.get_origin(type_)
|
|
202
|
+
|
|
203
|
+
if maybe_annotated_type == typing_extensions.Annotated:
|
|
204
|
+
return type_
|
|
205
|
+
|
|
206
|
+
return None
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _remove_annotations(type_: typing.Any) -> typing.Any:
|
|
210
|
+
maybe_annotated_type = typing_extensions.get_origin(type_)
|
|
211
|
+
if maybe_annotated_type is None:
|
|
212
|
+
return type_
|
|
213
|
+
|
|
214
|
+
if maybe_annotated_type == typing_extensions.NotRequired:
|
|
215
|
+
return _remove_annotations(typing_extensions.get_args(type_)[0])
|
|
216
|
+
|
|
217
|
+
if maybe_annotated_type == typing_extensions.Annotated:
|
|
218
|
+
return _remove_annotations(typing_extensions.get_args(type_)[0])
|
|
219
|
+
|
|
220
|
+
return type_
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def get_alias_to_field_mapping(type_: typing.Any) -> typing.Dict[str, str]:
|
|
224
|
+
annotations = typing_extensions.get_type_hints(type_, include_extras=True)
|
|
225
|
+
return _get_alias_to_field_name(annotations)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def get_field_to_alias_mapping(type_: typing.Any) -> typing.Dict[str, str]:
|
|
229
|
+
annotations = typing_extensions.get_type_hints(type_, include_extras=True)
|
|
230
|
+
return _get_field_to_alias_name(annotations)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _get_alias_to_field_name(
|
|
234
|
+
field_to_hint: typing.Dict[str, typing.Any],
|
|
235
|
+
) -> typing.Dict[str, str]:
|
|
236
|
+
aliases = {}
|
|
237
|
+
for field, hint in field_to_hint.items():
|
|
238
|
+
maybe_alias = _get_alias_from_type(hint)
|
|
239
|
+
if maybe_alias is not None:
|
|
240
|
+
aliases[maybe_alias] = field
|
|
241
|
+
return aliases
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _get_field_to_alias_name(
|
|
245
|
+
field_to_hint: typing.Dict[str, typing.Any],
|
|
246
|
+
) -> typing.Dict[str, str]:
|
|
247
|
+
aliases = {}
|
|
248
|
+
for field, hint in field_to_hint.items():
|
|
249
|
+
maybe_alias = _get_alias_from_type(hint)
|
|
250
|
+
if maybe_alias is not None:
|
|
251
|
+
aliases[field] = maybe_alias
|
|
252
|
+
return aliases
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _get_alias_from_type(type_: typing.Any) -> typing.Optional[str]:
|
|
256
|
+
maybe_annotated_type = _get_annotation(type_)
|
|
257
|
+
|
|
258
|
+
if maybe_annotated_type is not None:
|
|
259
|
+
# The actual annotations are 1 onward, the first is the annotated type
|
|
260
|
+
annotations = typing_extensions.get_args(maybe_annotated_type)[1:]
|
|
261
|
+
|
|
262
|
+
for annotation in annotations:
|
|
263
|
+
if isinstance(annotation, FieldMetadata) and annotation.alias is not None:
|
|
264
|
+
return annotation.alias
|
|
265
|
+
return None
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _alias_key(
|
|
269
|
+
key: str,
|
|
270
|
+
type_: typing.Any,
|
|
271
|
+
direction: typing.Literal["read", "write"],
|
|
272
|
+
aliases_to_field_names: typing.Dict[str, str],
|
|
273
|
+
) -> str:
|
|
274
|
+
if direction == "read":
|
|
275
|
+
return aliases_to_field_names.get(key, key)
|
|
276
|
+
return _get_alias_from_type(type_=type_) or key
|
|
File without changes
|