uipath-core 0.2.1__py3-none-any.whl → 0.2.2__py3-none-any.whl

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.
@@ -0,0 +1,5 @@
1
+ """Serialization utilities for converting Python objects to various formats."""
2
+
3
+ from .json import serialize_defaults, serialize_json
4
+
5
+ __all__ = ["serialize_defaults", "serialize_json"]
@@ -0,0 +1,150 @@
1
+ """JSON serialization utilities for converting Python objects to JSON formats."""
2
+
3
+ import json
4
+ from dataclasses import asdict, is_dataclass
5
+ from datetime import datetime, timezone
6
+ from enum import Enum
7
+ from typing import Any, cast
8
+ from zoneinfo import ZoneInfo
9
+
10
+ from pydantic import BaseModel
11
+
12
+
13
+ def serialize_defaults(
14
+ obj: Any,
15
+ ) -> dict[str, Any] | list[Any] | str | int | float | bool | None:
16
+ """Convert Python objects to JSON-serializable formats.
17
+
18
+ Handles common Python types that are not natively JSON-serializable:
19
+ - Pydantic models (v1 and v2)
20
+ - Dataclasses
21
+ - Enums
22
+ - Datetime objects
23
+ - Timezone objects
24
+ - Named tuples
25
+ - Sets and tuples
26
+
27
+ This function is designed to be used as the `default` parameter in json.dumps():
28
+ ```python
29
+ import json
30
+ result = json.dumps(obj, default=serialize_defaults)
31
+ ```
32
+
33
+ Or use the convenience function `serialize_json()` which wraps this:
34
+ ```python
35
+ result = serialize_json(obj)
36
+ ```
37
+
38
+ Args:
39
+ obj: The object to serialize
40
+
41
+ Returns:
42
+ A JSON-serializable representation of the object:
43
+ - Pydantic models: dict from model_dump()
44
+ - Dataclasses: dict from asdict()
45
+ - Enums: the enum value (recursively serialized)
46
+ - datetime: ISO format string
47
+ - timezone/ZoneInfo: timezone name
48
+ - sets/tuples: converted to lists
49
+ - named tuples: converted to dict
50
+ - Primitives (None, bool, int, float, str, list, dict): returned unchanged
51
+ - Other types: converted to string with str()
52
+
53
+ Examples:
54
+ >>> from datetime import datetime
55
+ >>> from pydantic import BaseModel
56
+ >>>
57
+ >>> class User(BaseModel):
58
+ ... name: str
59
+ ... created_at: datetime
60
+ >>>
61
+ >>> user = User(name="Alice", created_at=datetime.now())
62
+ >>> import json
63
+ >>> json.dumps(user, default=serialize_defaults)
64
+ '{"name": "Alice", "created_at": "2024-01-01T12:00:00"}'
65
+ >>> # Or use the convenience function
66
+ >>> serialize_json(user)
67
+ '{"name": "Alice", "created_at": "2024-01-01T12:00:00"}'
68
+ """
69
+ # Handle Pydantic BaseModel instances
70
+ if hasattr(obj, "model_dump") and not isinstance(obj, type):
71
+ return obj.model_dump(exclude_none=True, mode="json")
72
+
73
+ # Handle Pydantic model classes - convert to schema representation
74
+ if isinstance(obj, type) and issubclass(obj, BaseModel):
75
+ return {
76
+ "__class__": obj.__name__,
77
+ "__module__": obj.__module__,
78
+ "schema": obj.model_json_schema(),
79
+ }
80
+
81
+ # Handle Pydantic v1 models
82
+ if hasattr(obj, "dict") and not isinstance(obj, type):
83
+ return obj.dict()
84
+
85
+ # Handle objects with to_dict method
86
+ if hasattr(obj, "to_dict") and not isinstance(obj, type):
87
+ return obj.to_dict()
88
+
89
+ # Handle dataclasses
90
+ if is_dataclass(obj) and not isinstance(obj, type):
91
+ return asdict(obj)
92
+
93
+ # Handle enums - recursively serialize the value
94
+ if isinstance(obj, Enum):
95
+ return serialize_defaults(obj.value)
96
+
97
+ # Handle sets and tuples
98
+ if isinstance(obj, (set, tuple)):
99
+ # Check if it's a named tuple (has _asdict method)
100
+ if hasattr(obj, "_asdict") and callable(
101
+ obj._asdict # pyright: ignore[reportAttributeAccessIssue]
102
+ ):
103
+ return cast(
104
+ dict[str, Any],
105
+ obj._asdict(), # pyright: ignore[reportAttributeAccessIssue]
106
+ )
107
+ # Convert to list
108
+ return list(obj)
109
+
110
+ # Handle datetime objects
111
+ if isinstance(obj, datetime):
112
+ return obj.isoformat()
113
+
114
+ # Handle timezone objects
115
+ if isinstance(obj, (timezone, ZoneInfo)):
116
+ return obj.tzname(None)
117
+
118
+ # Allow JSON-serializable primitives to pass through unchanged
119
+ if obj is None or isinstance(obj, (bool, int, float, str, list, dict)):
120
+ return obj
121
+
122
+ # Fallback: convert to string
123
+ return str(obj)
124
+
125
+
126
+ def serialize_json(obj: Any) -> str:
127
+ """Serialize Python object to JSON string.
128
+
129
+ This is a convenience function that wraps json.dumps() with serialize_defaults()
130
+ as the default handler for non-JSON-serializable types.
131
+
132
+ Args:
133
+ obj: The object to serialize to JSON
134
+
135
+ Returns:
136
+ JSON string representation of the object
137
+
138
+ Examples:
139
+ >>> from datetime import datetime
140
+ >>> from pydantic import BaseModel
141
+ >>>
142
+ >>> class Task(BaseModel):
143
+ ... name: str
144
+ ... created: datetime
145
+ >>>
146
+ >>> task = Task(name="Review PR", created=datetime(2024, 1, 15, 10, 30))
147
+ >>> serialize_json(task)
148
+ '{"name": "Review PR", "created": "2024-01-15T10:30:00"}'
149
+ """
150
+ return json.dumps(obj, default=serialize_defaults)
@@ -3,14 +3,11 @@
3
3
  import inspect
4
4
  import json
5
5
  from collections.abc import Callable
6
- from dataclasses import asdict, is_dataclass
7
- from datetime import datetime, timezone
8
- from enum import Enum
9
- from typing import Any, Mapping, Optional, cast
10
- from zoneinfo import ZoneInfo
6
+ from typing import Any, Mapping, Optional
11
7
 
12
8
  from opentelemetry.trace import Span
13
- from pydantic import BaseModel
9
+
10
+ from uipath.core.serialization import serialize_json
14
11
 
15
12
 
16
13
  def get_supported_params(
@@ -31,64 +28,19 @@ def get_supported_params(
31
28
  return supported
32
29
 
33
30
 
34
- def _simple_serialize_defaults(
35
- obj: Any,
36
- ) -> dict[str, Any] | list[Any] | str | int | float | bool | None:
37
- # Handle Pydantic BaseModel instances
38
- if hasattr(obj, "model_dump") and not isinstance(obj, type):
39
- return obj.model_dump(exclude_none=True, mode="json")
40
-
41
- # Handle classes - convert to schema representation
42
- if isinstance(obj, type) and issubclass(obj, BaseModel):
43
- return {
44
- "__class__": obj.__name__,
45
- "__module__": obj.__module__,
46
- "schema": obj.model_json_schema(),
47
- }
48
- if hasattr(obj, "dict") and not isinstance(obj, type):
49
- return obj.dict()
50
- if hasattr(obj, "to_dict") and not isinstance(obj, type):
51
- return obj.to_dict()
52
-
53
- # Handle dataclasses
54
- if is_dataclass(obj) and not isinstance(obj, type):
55
- return asdict(obj)
56
-
57
- # Handle enums
58
- if isinstance(obj, Enum):
59
- return _simple_serialize_defaults(obj.value)
60
-
61
- if isinstance(obj, (set, tuple)):
62
- if hasattr(obj, "_asdict") and callable(obj._asdict): # pyright: ignore[reportAttributeAccessIssue]
63
- return cast(dict[str, Any], obj._asdict()) # pyright: ignore[reportAttributeAccessIssue]
64
- return list(obj)
65
-
66
- if isinstance(obj, datetime):
67
- return obj.isoformat()
68
-
69
- if isinstance(obj, (timezone, ZoneInfo)):
70
- return obj.tzname(None)
71
-
72
- # Allow JSON-serializable primitives to pass through unchanged
73
- if obj is None or isinstance(obj, (bool, int, float, str)):
74
- return obj
75
-
76
- return str(obj)
77
-
78
-
79
31
  def format_args_for_trace_json(
80
32
  signature: inspect.Signature, *args: Any, **kwargs: Any
81
33
  ) -> str:
82
34
  """Return a JSON string of inputs from the function signature."""
83
35
  result = format_args_for_trace(signature, *args, **kwargs)
84
- return json.dumps(result, default=_simple_serialize_defaults)
36
+ return serialize_json(result)
85
37
 
86
38
 
87
39
  def format_object_for_trace_json(
88
40
  input_object: Any,
89
41
  ) -> str:
90
42
  """Return a JSON string of inputs from the function signature."""
91
- return json.dumps(input_object, default=_simple_serialize_defaults)
43
+ return serialize_json(input_object)
92
44
 
93
45
 
94
46
  def format_args_for_trace(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: uipath-core
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: UiPath Core abstractions
5
5
  Project-URL: Homepage, https://uipath.com
6
6
  Project-URL: Repository, https://github.com/UiPath/uipath-core-python
@@ -18,15 +18,17 @@ uipath/core/guardrails/__init__.py,sha256=hUCmD4y5te2iy01YnJlBuf2RWvqxmsNzoyOamX
18
18
  uipath/core/guardrails/_deterministic_guardrails_service.py,sha256=61ROXYmX3rjBfFUp7E83fm4Lk7h1DlJeukpZm7uzVqQ,6355
19
19
  uipath/core/guardrails/_evaluators.py,sha256=10tIRUufxoy9MkZPb-ytjsCSCIfIqQSOYXyEl4G7PSw,15649
20
20
  uipath/core/guardrails/guardrails.py,sha256=sTxsNHilEX908aF-WtQWVOQ4dCCwMgAepDebH35ngvo,5430
21
+ uipath/core/serialization/__init__.py,sha256=thAMx0WY03vnqhKCDVc9burqKWDxSNn3R-CJQU_1LIs,186
22
+ uipath/core/serialization/json.py,sha256=S0-ykGfroleRC0gJBNuTyJCx-GB8e6ZG9uTUKiD4PSc,4791
21
23
  uipath/core/tracing/__init__.py,sha256=Y0wfRSGCVcDoFYTmTkVQElu5JE91h4dTNkKQnd86dBM,568
22
- uipath/core/tracing/_utils.py,sha256=FiCFGOFa4czruhlSF87Q5Q4jX9KKPHZiw8k14K7W5v4,6636
24
+ uipath/core/tracing/_utils.py,sha256=VnDd-oMwZXPKa7bdda2bFLSZXXXhJ8d4RQ4g7Mx1otU,4915
23
25
  uipath/core/tracing/decorators.py,sha256=JDNULkUu-Ufg8pJslG4i6Q2pmqaGNDS8NFRPgFs29Dw,11937
24
26
  uipath/core/tracing/exporters.py,sha256=FClouEEQfk3F8J7G_NFoarDJM3R0-gA5jUxA5xRHx5s,1562
25
27
  uipath/core/tracing/processors.py,sha256=XlMKA_AWwrtC-0ytnAHxl4P9kXlQdsjcn8zwnSpZKts,2500
26
28
  uipath/core/tracing/span_utils.py,sha256=LZXNdnI0-fhKe49CLPsvMJIfh9zdzk8rK4g4YN5RfDU,13064
27
29
  uipath/core/tracing/trace_manager.py,sha256=vmPup-C2pSgwpSdcCzAjzP_nTtRWZgqlxnNT50ygOfk,3843
28
30
  uipath/core/tracing/types.py,sha256=8A8fFuWqMGk0SzIrFbMajmY6LA3w57h-YA602OctCrI,634
29
- uipath_core-0.2.1.dist-info/METADATA,sha256=zPRzoolLStzjDDepr4C2iN46ZGrinRfrR-UyxjC1yak,938
30
- uipath_core-0.2.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
31
- uipath_core-0.2.1.dist-info/licenses/LICENSE,sha256=-KBavWXepyDjimmzH5fVAsi-6jNVpIKFc2kZs0Ri4ng,1058
32
- uipath_core-0.2.1.dist-info/RECORD,,
31
+ uipath_core-0.2.2.dist-info/METADATA,sha256=pjzOsvo24CI1AOHmZ2pHLp0kXx9YtZk0fLkBHntjrTw,938
32
+ uipath_core-0.2.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
33
+ uipath_core-0.2.2.dist-info/licenses/LICENSE,sha256=-KBavWXepyDjimmzH5fVAsi-6jNVpIKFc2kZs0Ri4ng,1058
34
+ uipath_core-0.2.2.dist-info/RECORD,,