MCPcopy
hub / github.com/reflex-dev/reflex / wrapper

Function wrapper

packages/reflex-base/src/reflex_base/utils/serializers.py:80–127  ·  view source on GitHub ↗
(fn: SERIALIZED_FUNCTION)

Source from the content-addressed store, hash-verified

78 """
79
80 def wrapper(fn: SERIALIZED_FUNCTION) -> SERIALIZED_FUNCTION:
81 # Check the type hints to get the type of the argument.
82 type_hints = get_type_hints(fn)
83 args = [arg for arg in type_hints if arg != "return"]
84
85 # Make sure the function takes a single argument.
86 if len(args) != 1:
87 msg = "Serializer must take a single argument."
88 raise ValueError(msg)
89
90 # Get the type of the argument.
91 type_ = type_hints[args[0]]
92
93 # Make sure the type is not already registered.
94 registered_fn = SERIALIZERS.get(type_)
95 if registered_fn is not None and registered_fn != fn and overwrite is not True:
96 message = f"Overwriting serializer for type {type_} from {registered_fn.__module__}:{registered_fn.__qualname__} to {fn.__module__}:{fn.__qualname__}."
97 if overwrite is False:
98 raise ValueError(message)
99 caller_frame = next(
100 filter(
101 lambda frame: frame.filename != __file__,
102 inspect.getouterframes(inspect.currentframe()),
103 ),
104 None,
105 )
106 file_info = (
107 f"(at {caller_frame.filename}:{caller_frame.lineno})"
108 if caller_frame
109 else ""
110 )
111 console.warn(
112 f"{message} Call rx.serializer with `overwrite=True` if this is intentional. {file_info}"
113 )
114
115 to_type = to or type_hints.get("return")
116
117 # Apply type transformation if requested
118 if to_type:
119 SERIALIZER_TYPES[type_] = to_type
120 get_serializer_type.cache_clear()
121
122 # Register the serializer.
123 SERIALIZERS[type_] = fn
124 get_serializer.cache_clear()
125
126 # Return the function.
127 return fn
128
129 if fn is not None:
130 return wrapper(fn)

Callers 1

serializerFunction · 0.70

Calls 2

get_type_hintsFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected