MCPcopy Create free account
hub / github.com/dot-agent/nextpy / serializer

Function serializer

nextpy/utils/serializers.py:23–60  ·  view source on GitHub ↗

Decorator to add a serializer for a given type. Args: fn: The function to decorate. Returns: The decorated function. Raises: ValueError: If the function does not take a single argument.

(fn: Serializer)

Source from the content-addressed store, hash-verified

21
22
23def serializer(fn: Serializer) -> Serializer:
24 """Decorator to add a serializer for a given type.
25
26 Args:
27 fn: The function to decorate.
28
29 Returns:
30 The decorated function.
31
32 Raises:
33 ValueError: If the function does not take a single argument.
34 """
35 # Get the global serializers.
36 global SERIALIZERS
37
38 # Check the type hints to get the type of the argument.
39 type_hints = get_type_hints(fn)
40 args = [arg for arg in type_hints if arg != "return"]
41
42 # Make sure the function takes a single argument.
43 if len(args) != 1:
44 raise ValueError("Serializer must take a single argument.")
45
46 # Get the type of the argument.
47 type_ = type_hints[args[0]]
48
49 # Make sure the type is not already registered.
50 registered_fn = SERIALIZERS.get(type_)
51 if registered_fn is not None and registered_fn != fn:
52 raise ValueError(
53 f"Serializer for type {type_} is already registered as {registered_fn.__qualname__}."
54 )
55
56 # Register the serializer.
57 SERIALIZERS[type_] = fn
58
59 # Return the function.
60 return fn
61
62
63def serialize(value: Any) -> SerializedType | None:

Callers 1

serializeFunction · 0.85

Calls 2

lenFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected