|
24 | 24 | from .enums.list_format import ListFormat |
25 | 25 | from .enums.sentinel import Sentinel |
26 | 26 | from .models.encode_options import EncodeOptions |
27 | | -from .models.undefined import Undefined |
| 27 | +from .models.undefined import UNDEFINED, Undefined |
28 | 28 | from .models.weak_wrapper import WeakWrapper |
29 | 29 | from .utils.utils import Utils |
30 | 30 |
|
@@ -60,15 +60,15 @@ def encode(value: t.Any, options: EncodeOptions = EncodeOptions()) -> str: |
60 | 60 | if isinstance(value, t.Mapping): |
61 | 61 | obj = deepcopy(value) |
62 | 62 | elif isinstance(value, (list, tuple)): |
63 | | - obj = {str(key): value for key, value in enumerate(value)} |
| 63 | + obj = {str(i): item for i, item in enumerate(value)} |
64 | 64 | else: |
65 | 65 | obj = {} |
66 | 66 |
|
67 | 67 | # Early exit if there's nothing to emit. |
68 | 68 | if not obj: |
69 | 69 | return "" |
70 | 70 |
|
71 | | - keys: t.List[t.Any] = [] |
| 71 | + keys: t.List[str] = [] |
72 | 72 |
|
73 | 73 | # If an iterable filter is provided for the root, restrict emission to those keys. |
74 | 74 | obj_keys: t.Optional[t.List[t.Any]] = None |
@@ -218,14 +218,14 @@ def _encode( |
218 | 218 |
|
219 | 219 | # Infer comma round-trip when using the COMMA generator and the flag was not explicitly provided. |
220 | 220 | if comma_round_trip is None: |
221 | | - comma_round_trip = generate_array_prefix == ListFormat.COMMA.generator |
| 221 | + comma_round_trip = generate_array_prefix is ListFormat.COMMA.generator |
222 | 222 |
|
223 | 223 | # Choose a formatter if one wasn't provided (based on the selected format). |
224 | 224 | if formatter is None: |
225 | 225 | formatter = format.formatter |
226 | 226 |
|
227 | | - # Work on a copy to avoid mutating caller state during normalization. |
228 | | - obj: t.Any = deepcopy(value) |
| 227 | + # Work with the original; we never mutate in place (we build new lists/maps when normalizing). |
| 228 | + obj: t.Any = value |
229 | 229 |
|
230 | 230 | # --- Cycle detection via chained side-channel ----------------------------------------- |
231 | 231 | obj_wrapper: WeakWrapper = WeakWrapper(value) |
@@ -255,7 +255,7 @@ def _encode( |
255 | 255 | # Normalize datetimes both for scalars and (in COMMA mode) list elements. |
256 | 256 | if isinstance(obj, datetime): |
257 | 257 | obj = serialize_date(obj) if callable(serialize_date) else obj.isoformat() |
258 | | - elif generate_array_prefix == ListFormat.COMMA.generator and isinstance(obj, (list, tuple)): |
| 258 | + elif generate_array_prefix is ListFormat.COMMA.generator and isinstance(obj, (list, tuple)): |
259 | 259 | if callable(serialize_date): |
260 | 260 | obj = [serialize_date(x) if isinstance(x, datetime) else x for x in obj] |
261 | 261 | else: |
@@ -289,23 +289,22 @@ def _encode( |
289 | 289 | if encode_values_only and callable(encoder): |
290 | 290 | obj = Utils.apply(obj, encoder) |
291 | 291 | if obj: |
292 | | - obj_keys_value = ",".join([str(e) if e is not None else "" for e in obj]) |
| 292 | + obj_keys_value = ",".join(("" if e is None else str(e)) for e in obj) |
293 | 293 | obj_keys = [{"value": obj_keys_value if obj_keys_value else None}] |
294 | 294 | else: |
295 | | - obj_keys = [{"value": Undefined()}] |
| 295 | + obj_keys = [{"value": UNDEFINED}] |
296 | 296 | elif isinstance(filter, (list, tuple)): |
297 | 297 | # Iterable filter restricts traversal to a fixed key/index set. |
298 | 298 | obj_keys = list(filter) |
299 | 299 | else: |
300 | 300 | # Default: enumerate keys/indices from mappings or sequences. |
301 | | - keys: t.List[t.Any] |
302 | 301 | if isinstance(obj, t.Mapping): |
303 | 302 | keys = list(obj.keys()) |
304 | 303 | elif isinstance(obj, (list, tuple)): |
305 | | - keys = [index for index in range(len(obj))] |
| 304 | + keys = list(range(len(obj))) |
306 | 305 | else: |
307 | 306 | keys = [] |
308 | | - obj_keys = sorted(keys, key=cmp_to_key(sort)) if sort is not None else list(keys) |
| 307 | + obj_keys = sorted(keys, key=cmp_to_key(sort)) if sort is not None else keys |
309 | 308 |
|
310 | 309 | # Percent-encode literal dots in key names when requested. |
311 | 310 | encoded_prefix: str = prefix.replace(".", "%2E") if encode_dot_in_keys else prefix |
@@ -372,7 +371,7 @@ def _encode( |
372 | 371 | comma_round_trip=comma_round_trip, |
373 | 372 | encoder=( |
374 | 373 | None |
375 | | - if generate_array_prefix == ListFormat.COMMA.generator |
| 374 | + if generate_array_prefix is ListFormat.COMMA.generator |
376 | 375 | and encode_values_only |
377 | 376 | and isinstance(obj, (list, tuple)) |
378 | 377 | else encoder |
|
0 commit comments