wrapt 2.4.0.dev2__cp315-cp315-win_amd64.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.
wrapt/proxies.py ADDED
@@ -0,0 +1,370 @@
1
+ """Variants of ObjectProxy for different use cases."""
2
+
3
+ from collections.abc import Callable
4
+ from types import ModuleType
5
+
6
+ from .__wrapt__ import BaseObjectProxy
7
+ from .synchronization import synchronized
8
+
9
+ # Define ObjectProxy which for compatibility adds `__iter__()` support which
10
+ # has been removed from `BaseObjectProxy`.
11
+
12
+
13
+ class ObjectProxy(BaseObjectProxy):
14
+ """A generic object proxy which forwards special methods as needed.
15
+ For backwards compatibility this class adds support for `__iter__()`. If
16
+ you don't need backward compatibility for `__iter__()` support then it is
17
+ preferable to use `BaseObjectProxy` directly. If you want automatic
18
+ support for special dunder methods for callables, iterators, and async,
19
+ then use `AutoObjectProxy`."""
20
+
21
+ @property
22
+ def __object_proxy__(self):
23
+ return ObjectProxy
24
+
25
+ def __new__(cls, *args, **kwargs):
26
+ return super().__new__(cls)
27
+
28
+ def __iter__(self):
29
+ return iter(self.__wrapped__)
30
+
31
+
32
+ # Define variant of ObjectProxy which can automatically adjust to the wrapped
33
+ # object and add special dunder methods.
34
+
35
+
36
+ def __wrapper_call__(*args, **kwargs):
37
+ def _unpack_self(self, *args):
38
+ return self, args
39
+
40
+ self, args = _unpack_self(*args)
41
+
42
+ return self.__wrapped__(*args, **kwargs)
43
+
44
+
45
+ def __wrapper_iter__(self):
46
+ return iter(self.__wrapped__)
47
+
48
+
49
+ def __wrapper_next__(self):
50
+ return self.__wrapped__.__next__()
51
+
52
+
53
+ def __wrapper_aiter__(self):
54
+ return self.__wrapped__.__aiter__()
55
+
56
+
57
+ async def __wrapper_anext__(self):
58
+ return await self.__wrapped__.__anext__()
59
+
60
+
61
+ def __wrapper_length_hint__(self):
62
+ return self.__wrapped__.__length_hint__()
63
+
64
+
65
+ def __wrapper_fspath__(self):
66
+ return self.__wrapped__.__fspath__()
67
+
68
+
69
+ def __wrapper_await__(self):
70
+ return (yield from self.__wrapped__.__await__())
71
+
72
+
73
+ def __wrapper_get__(self, instance, owner):
74
+ return self.__wrapped__.__get__(instance, owner)
75
+
76
+
77
+ def __wrapper_set__(self, instance, value):
78
+ return self.__wrapped__.__set__(instance, value)
79
+
80
+
81
+ def __wrapper_delete__(self, instance):
82
+ return self.__wrapped__.__delete__(instance)
83
+
84
+
85
+ def __wrapper_set_name__(self, owner, name):
86
+ return self.__wrapped__.__set_name__(owner, name)
87
+
88
+
89
+ class AutoObjectProxy(BaseObjectProxy):
90
+ """An object proxy which can automatically adjust to the wrapped object
91
+ and add special dunder methods as needed. Note that this creates a new
92
+ class for each instance, so it has much higher memory overhead than using
93
+ `BaseObjectProxy` directly. If you know what special dunder methods you need
94
+ then it is preferable to use `BaseObjectProxy` directly and add them to a
95
+ subclass as needed. If you only need `__iter__()` support for backwards
96
+ compatibility then use `ObjectProxy` instead.
97
+ """
98
+
99
+ def __new__(cls, wrapped):
100
+ """Injects special dunder methods into a dynamically created subclass
101
+ as needed based on the wrapped object.
102
+ """
103
+
104
+ namespace = {}
105
+
106
+ wrapped_attrs = dir(wrapped)
107
+ class_attrs = set(dir(cls))
108
+
109
+ if callable(wrapped) and "__call__" not in class_attrs:
110
+ namespace["__call__"] = __wrapper_call__
111
+
112
+ if "__iter__" in wrapped_attrs and "__iter__" not in class_attrs:
113
+ namespace["__iter__"] = __wrapper_iter__
114
+
115
+ if "__next__" in wrapped_attrs and "__next__" not in class_attrs:
116
+ namespace["__next__"] = __wrapper_next__
117
+
118
+ if "__aiter__" in wrapped_attrs and "__aiter__" not in class_attrs:
119
+ namespace["__aiter__"] = __wrapper_aiter__
120
+
121
+ if "__anext__" in wrapped_attrs and "__anext__" not in class_attrs:
122
+ namespace["__anext__"] = __wrapper_anext__
123
+
124
+ if "__length_hint__" in wrapped_attrs and "__length_hint__" not in class_attrs:
125
+ namespace["__length_hint__"] = __wrapper_length_hint__
126
+
127
+ if "__fspath__" in wrapped_attrs and "__fspath__" not in class_attrs:
128
+ namespace["__fspath__"] = __wrapper_fspath__
129
+
130
+ # Note that not providing compatibility with generator-based coroutines
131
+ # (PEP 342) here as they are removed in Python 3.11+ and were deprecated
132
+ # in 3.8.
133
+
134
+ if "__await__" in wrapped_attrs and "__await__" not in class_attrs:
135
+ namespace["__await__"] = __wrapper_await__
136
+
137
+ if "__get__" in wrapped_attrs and "__get__" not in class_attrs:
138
+ namespace["__get__"] = __wrapper_get__
139
+
140
+ if "__set__" in wrapped_attrs and "__set__" not in class_attrs:
141
+ namespace["__set__"] = __wrapper_set__
142
+
143
+ if "__delete__" in wrapped_attrs and "__delete__" not in class_attrs:
144
+ namespace["__delete__"] = __wrapper_delete__
145
+
146
+ if "__set_name__" in wrapped_attrs and "__set_name__" not in class_attrs:
147
+ namespace["__set_name__"] = __wrapper_set_name__
148
+
149
+ name = cls.__name__
150
+
151
+ if cls is AutoObjectProxy:
152
+ name = BaseObjectProxy.__name__
153
+
154
+ # Explicit class in super() is required here to ensure __new__
155
+ # is called on the parent of AutoObjectProxy, not the dynamically
156
+ # created subclass.
157
+ return super(AutoObjectProxy, cls).__new__(type(name, (cls,), namespace))
158
+
159
+ def __wrapped_setattr_fixups__(self):
160
+ """Adjusts special dunder methods on the class as needed based on the
161
+ wrapped object, when `__wrapped__` is changed.
162
+ """
163
+
164
+ cls = type(self)
165
+ class_attrs = set(dir(cls))
166
+
167
+ if callable(self.__wrapped__):
168
+ if "__call__" not in class_attrs:
169
+ cls.__call__ = __wrapper_call__
170
+ elif getattr(cls, "__call__", None) is __wrapper_call__:
171
+ delattr(cls, "__call__")
172
+
173
+ if hasattr(self.__wrapped__, "__iter__"):
174
+ if "__iter__" not in class_attrs:
175
+ cls.__iter__ = __wrapper_iter__
176
+ elif getattr(cls, "__iter__", None) is __wrapper_iter__:
177
+ delattr(cls, "__iter__")
178
+
179
+ if hasattr(self.__wrapped__, "__next__"):
180
+ if "__next__" not in class_attrs:
181
+ cls.__next__ = __wrapper_next__
182
+ elif getattr(cls, "__next__", None) is __wrapper_next__:
183
+ delattr(cls, "__next__")
184
+
185
+ if hasattr(self.__wrapped__, "__aiter__"):
186
+ if "__aiter__" not in class_attrs:
187
+ cls.__aiter__ = __wrapper_aiter__
188
+ elif getattr(cls, "__aiter__", None) is __wrapper_aiter__:
189
+ delattr(cls, "__aiter__")
190
+
191
+ if hasattr(self.__wrapped__, "__anext__"):
192
+ if "__anext__" not in class_attrs:
193
+ cls.__anext__ = __wrapper_anext__
194
+ elif getattr(cls, "__anext__", None) is __wrapper_anext__:
195
+ delattr(cls, "__anext__")
196
+
197
+ if hasattr(self.__wrapped__, "__length_hint__"):
198
+ if "__length_hint__" not in class_attrs:
199
+ cls.__length_hint__ = __wrapper_length_hint__
200
+ elif getattr(cls, "__length_hint__", None) is __wrapper_length_hint__:
201
+ delattr(cls, "__length_hint__")
202
+
203
+ if hasattr(self.__wrapped__, "__fspath__"):
204
+ if "__fspath__" not in class_attrs:
205
+ cls.__fspath__ = __wrapper_fspath__
206
+ elif getattr(cls, "__fspath__", None) is __wrapper_fspath__:
207
+ delattr(cls, "__fspath__")
208
+
209
+ if hasattr(self.__wrapped__, "__await__"):
210
+ if "__await__" not in class_attrs:
211
+ cls.__await__ = __wrapper_await__
212
+ elif getattr(cls, "__await__", None) is __wrapper_await__:
213
+ delattr(cls, "__await__")
214
+
215
+ if hasattr(self.__wrapped__, "__get__"):
216
+ if "__get__" not in class_attrs:
217
+ cls.__get__ = __wrapper_get__
218
+ elif getattr(cls, "__get__", None) is __wrapper_get__:
219
+ delattr(cls, "__get__")
220
+
221
+ if hasattr(self.__wrapped__, "__set__"):
222
+ if "__set__" not in class_attrs:
223
+ cls.__set__ = __wrapper_set__
224
+ elif getattr(cls, "__set__", None) is __wrapper_set__:
225
+ delattr(cls, "__set__")
226
+
227
+ if hasattr(self.__wrapped__, "__delete__"):
228
+ if "__delete__" not in class_attrs:
229
+ cls.__delete__ = __wrapper_delete__
230
+ elif getattr(cls, "__delete__", None) is __wrapper_delete__:
231
+ delattr(cls, "__delete__")
232
+
233
+ if hasattr(self.__wrapped__, "__set_name__"):
234
+ if "__set_name__" not in class_attrs:
235
+ cls.__set_name__ = __wrapper_set_name__
236
+ elif getattr(cls, "__set_name__", None) is __wrapper_set_name__:
237
+ delattr(cls, "__set_name__")
238
+
239
+
240
+ class LazyObjectProxy(AutoObjectProxy):
241
+ """An object proxy which can generate/create the wrapped object on demand
242
+ when it is first needed.
243
+ """
244
+
245
+ def __new__(cls, callback=None, *, interface=...):
246
+ """Injects special dunder methods into a dynamically created subclass
247
+ as needed based on the wrapped object.
248
+ """
249
+
250
+ if interface is ...:
251
+ interface = type(None)
252
+
253
+ namespace = {}
254
+
255
+ interface_attrs = dir(interface)
256
+ class_attrs = set(dir(cls))
257
+
258
+ if "__call__" in interface_attrs and "__call__" not in class_attrs:
259
+ namespace["__call__"] = __wrapper_call__
260
+
261
+ if "__iter__" in interface_attrs and "__iter__" not in class_attrs:
262
+ namespace["__iter__"] = __wrapper_iter__
263
+
264
+ if "__next__" in interface_attrs and "__next__" not in class_attrs:
265
+ namespace["__next__"] = __wrapper_next__
266
+
267
+ if "__aiter__" in interface_attrs and "__aiter__" not in class_attrs:
268
+ namespace["__aiter__"] = __wrapper_aiter__
269
+
270
+ if "__anext__" in interface_attrs and "__anext__" not in class_attrs:
271
+ namespace["__anext__"] = __wrapper_anext__
272
+
273
+ if (
274
+ "__length_hint__" in interface_attrs
275
+ and "__length_hint__" not in class_attrs
276
+ ):
277
+ namespace["__length_hint__"] = __wrapper_length_hint__
278
+
279
+ # Note that not providing compatibility with generator-based coroutines
280
+ # (PEP 342) here as they are removed in Python 3.11+ and were deprecated
281
+ # in 3.8.
282
+
283
+ if "__await__" in interface_attrs and "__await__" not in class_attrs:
284
+ namespace["__await__"] = __wrapper_await__
285
+
286
+ if "__get__" in interface_attrs and "__get__" not in class_attrs:
287
+ namespace["__get__"] = __wrapper_get__
288
+
289
+ if "__set__" in interface_attrs and "__set__" not in class_attrs:
290
+ namespace["__set__"] = __wrapper_set__
291
+
292
+ if "__delete__" in interface_attrs and "__delete__" not in class_attrs:
293
+ namespace["__delete__"] = __wrapper_delete__
294
+
295
+ if "__set_name__" in interface_attrs and "__set_name__" not in class_attrs:
296
+ namespace["__set_name__"] = __wrapper_set_name__
297
+
298
+ name = cls.__name__
299
+
300
+ # Explicit class in super() is required here to ensure __new__
301
+ # is called on the parent of AutoObjectProxy, not the dynamically
302
+ # created subclass.
303
+ return super(AutoObjectProxy, cls).__new__(type(name, (cls,), namespace))
304
+
305
+ def __init__(self, callback=None, *, interface=...):
306
+ """Initialize the object proxy with wrapped object as `None` but due
307
+ to presence of special `__wrapped_factory__` attribute addded first,
308
+ this will actually trigger the deferred creation of the wrapped object
309
+ when first needed.
310
+ """
311
+
312
+ if callback is not None:
313
+ self.__wrapped_factory__ = callback
314
+
315
+ super().__init__(None)
316
+
317
+ __wrapped_get_called__ = False
318
+
319
+ def __wrapped_factory__(self):
320
+ return None
321
+
322
+ def __wrapped_get__(self):
323
+ """Gets the wrapped object, creating it if necessary."""
324
+
325
+ # We synchronize on the class type, which will be unique to this instance
326
+ # since we inherit from `AutoObjectProxy` which creates a new class
327
+ # for each instance. If we synchronize on `self` or the method then
328
+ # we can end up in infinite recursion via `__getattr__()`.
329
+
330
+ with synchronized(type(self)):
331
+ # We were called because `__wrapped__` was not set, but because of
332
+ # multiple threads we may find that it has been set by the time
333
+ # we get the lock. So check again now whether `__wrapped__` is set.
334
+ # If it is then just return it, otherwise call the factory to
335
+ # create it.
336
+
337
+ if self.__wrapped_get_called__:
338
+ return self.__wrapped__
339
+
340
+ self.__wrapped__ = self.__wrapped_factory__()
341
+
342
+ self.__wrapped_get_called__ = True
343
+
344
+ return self.__wrapped__
345
+
346
+
347
+ def lazy_import(name, attribute=None, *, interface=...):
348
+ """Lazily imports the module `name`, returning a `LazyObjectProxy` which
349
+ will import the module when it is first needed. When `name is a dotted name,
350
+ then the full dotted name is imported and the last module is taken as the
351
+ target. If `attribute` is provided then it is used to retrieve an attribute
352
+ from the module.
353
+ """
354
+
355
+ if attribute is not None:
356
+ if interface is ...:
357
+ interface = Callable
358
+ else:
359
+ if interface is ...:
360
+ interface = ModuleType
361
+
362
+ def _import():
363
+ module = __import__(name, fromlist=[""])
364
+
365
+ if attribute is not None:
366
+ return getattr(module, attribute)
367
+
368
+ return module
369
+
370
+ return LazyObjectProxy(_import, interface=interface)
wrapt/signature.py ADDED
@@ -0,0 +1,250 @@
1
+ """The `with_signature` decorator: override the signature (and related
2
+ introspection attributes) of a wrapped callable without mutating the wrapped
3
+ function itself. Accepts a prototype callable, a prebuilt `inspect.Signature`,
4
+ or a factory that derives a signature from the wrapped function at decoration
5
+ time.
6
+
7
+ This replaces the need for the `adapter=` argument of `wrapt.decorator`,
8
+ which is planned for deprecation.
9
+ """
10
+
11
+ from inspect import CO_VARARGS, CO_VARKEYWORDS, Parameter, Signature
12
+ from inspect import signature as _inspect_signature
13
+
14
+ from .__wrapt__ import (
15
+ BaseObjectProxy,
16
+ BoundFunctionWrapper,
17
+ CallableObjectProxy,
18
+ FunctionWrapper,
19
+ )
20
+
21
+ _MISSING = object()
22
+
23
+
24
+ def _cached(attr):
25
+ """Read-only property that memoizes its derivation in a `_self_*` slot.
26
+
27
+ `functools.cached_property` is unusable on ObjectProxy subclasses: wrapt
28
+ overrides `__dict__` with a property returning the wrapped object's dict,
29
+ so cached_property's `instance.__dict__[name] = value` would mutate the
30
+ wrapped function. Storing under a `_self_*` name routes through wrapt's
31
+ own setattr handling and lands in the proxy's real instance dict. Using
32
+ a `@property` (data descriptor) also ensures we win over any value
33
+ copied into the instance dict by `FunctionWrapper.__init__`.
34
+ """
35
+ slot = f"_self_cached_{attr.lstrip('_')}"
36
+
37
+ def getter(self):
38
+ value = getattr(self, slot, _MISSING)
39
+ if value is _MISSING:
40
+ value = self._derive(attr)
41
+ self.__self_setattr__(slot, value)
42
+ return value
43
+
44
+ return property(getter)
45
+
46
+
47
+ def _derive_annotations(sig):
48
+ ann = {
49
+ p.name: p.annotation
50
+ for p in sig.parameters.values()
51
+ if p.annotation is not Parameter.empty
52
+ }
53
+ if sig.return_annotation is not Signature.empty:
54
+ ann["return"] = sig.return_annotation
55
+ return ann
56
+
57
+
58
+ def _derive_defaults(sig):
59
+ defaults = tuple(
60
+ p.default
61
+ for p in sig.parameters.values()
62
+ if p.kind in (Parameter.POSITIONAL_ONLY, Parameter.POSITIONAL_OR_KEYWORD)
63
+ and p.default is not Parameter.empty
64
+ )
65
+ return defaults or None
66
+
67
+
68
+ def _derive_kwdefaults(sig):
69
+ kwdefaults = {
70
+ p.name: p.default
71
+ for p in sig.parameters.values()
72
+ if p.kind is Parameter.KEYWORD_ONLY and p.default is not Parameter.empty
73
+ }
74
+ return kwdefaults or None
75
+
76
+
77
+ def _derive_varnames(sig):
78
+ pos_only, pos_or_kw, kw_only = [], [], []
79
+ var_pos = var_kw = None
80
+ for p in sig.parameters.values():
81
+ if p.kind is Parameter.POSITIONAL_ONLY:
82
+ pos_only.append(p.name)
83
+ elif p.kind is Parameter.POSITIONAL_OR_KEYWORD:
84
+ pos_or_kw.append(p.name)
85
+ elif p.kind is Parameter.VAR_POSITIONAL:
86
+ var_pos = p.name
87
+ elif p.kind is Parameter.KEYWORD_ONLY:
88
+ kw_only.append(p.name)
89
+ elif p.kind is Parameter.VAR_KEYWORD:
90
+ var_kw = p.name
91
+ names = pos_only + pos_or_kw + kw_only
92
+ if var_pos:
93
+ names.append(var_pos)
94
+ if var_kw:
95
+ names.append(var_kw)
96
+ return tuple(names)
97
+
98
+
99
+ class _SignatureCode(BaseObjectProxy):
100
+ """Code-object proxy deriving argument-related co_* attrs from a Signature.
101
+
102
+ Non-argument bits (co_flags for coroutine/generator, co_filename, etc.)
103
+ fall through to the wrapped function's real __code__.
104
+ """
105
+
106
+ def __init__(self, wrapped, signature):
107
+ super().__init__(wrapped)
108
+ self._self_signature = signature
109
+
110
+ def _derive(self, attr):
111
+ sig = self._self_signature
112
+ if attr == "co_argcount":
113
+ kinds = (Parameter.POSITIONAL_ONLY, Parameter.POSITIONAL_OR_KEYWORD)
114
+ return sum(p.kind in kinds for p in sig.parameters.values())
115
+ if attr == "co_posonlyargcount":
116
+ return sum(
117
+ p.kind is Parameter.POSITIONAL_ONLY for p in sig.parameters.values()
118
+ )
119
+ if attr == "co_kwonlyargcount":
120
+ return sum(
121
+ p.kind is Parameter.KEYWORD_ONLY for p in sig.parameters.values()
122
+ )
123
+ if attr == "co_varnames":
124
+ return _derive_varnames(sig)
125
+ if attr == "co_flags":
126
+ kinds = {p.kind for p in sig.parameters.values()}
127
+ flags = self.__wrapped__.co_flags & ~(CO_VARARGS | CO_VARKEYWORDS)
128
+ if Parameter.VAR_POSITIONAL in kinds:
129
+ flags |= CO_VARARGS
130
+ if Parameter.VAR_KEYWORD in kinds:
131
+ flags |= CO_VARKEYWORDS
132
+ return flags
133
+ raise AttributeError(attr)
134
+
135
+ co_argcount = _cached("co_argcount")
136
+ co_posonlyargcount = _cached("co_posonlyargcount")
137
+ co_kwonlyargcount = _cached("co_kwonlyargcount")
138
+ co_varnames = _cached("co_varnames")
139
+ co_flags = _cached("co_flags")
140
+
141
+
142
+ class _SignatureMixin:
143
+ """Shared Signature-derived introspection attrs for wrapper + surrogate."""
144
+
145
+ def _derive(self, attr):
146
+ sig = self._self_signature
147
+ if attr == "__annotations__":
148
+ return _derive_annotations(sig)
149
+ if attr == "__defaults__":
150
+ return _derive_defaults(sig)
151
+ if attr == "__kwdefaults__":
152
+ return _derive_kwdefaults(sig)
153
+ if attr == "__code__":
154
+ return _SignatureCode(self.__wrapped__.__code__, sig)
155
+ raise AttributeError(attr)
156
+
157
+ @property
158
+ def __signature__(self):
159
+ return self._self_signature
160
+
161
+ __annotations__ = _cached("__annotations__")
162
+ __defaults__ = _cached("__defaults__")
163
+ __kwdefaults__ = _cached("__kwdefaults__")
164
+ __code__ = _cached("__code__")
165
+
166
+
167
+ class _SignatureFunctionSurrogate(_SignatureMixin, CallableObjectProxy):
168
+ """Function surrogate exposing Signature-derived introspection attrs.
169
+
170
+ Used as __func__ of a bound wrapper so that inspect.signature -- which
171
+ treats the bound wrapper as a MethodType and consults __func__ -- sees
172
+ our override and strips self/cls correctly.
173
+ """
174
+
175
+ def __init__(self, wrapped, signature):
176
+ super().__init__(wrapped)
177
+ self._self_signature = signature
178
+
179
+
180
+ class _BoundSignatureFunctionWrapper(BoundFunctionWrapper):
181
+ @property
182
+ def __func__(self):
183
+ return _SignatureFunctionSurrogate(
184
+ self.__wrapped__.__func__, self._self_parent._self_signature
185
+ )
186
+
187
+ @property
188
+ def __signature__(self):
189
+ return self._self_parent._self_signature
190
+
191
+
192
+ class _SignatureFunctionWrapper(_SignatureMixin, FunctionWrapper):
193
+ __bound_function_wrapper__ = _BoundSignatureFunctionWrapper
194
+
195
+ def __init__(self, wrapped, wrapper, signature):
196
+ super().__init__(wrapped, wrapper)
197
+ self._self_signature = signature
198
+
199
+ @property
200
+ def __func__(self):
201
+ return _SignatureFunctionSurrogate(self.__wrapped__, self._self_signature)
202
+
203
+
204
+ def with_signature(wrapped=None, /, *, prototype=None, signature=None, factory=None):
205
+ """Override the signature of a wrapped callable.
206
+
207
+ Exactly one of `prototype`, `signature`, or `factory` must be supplied:
208
+
209
+ - `prototype`: a callable whose signature will be used.
210
+ - `signature`: a prebuilt `inspect.Signature` object.
211
+ - `factory`: a callable `factory(wrapped)` invoked at decoration time
212
+ that returns either a `Signature` or a prototype callable.
213
+
214
+ The resulting wrapper exposes the override via `__signature__`, and
215
+ derives `__annotations__`, `__defaults__`, `__kwdefaults__`, and the
216
+ argument-related attributes of `__code__` from the same source. The
217
+ wrapped function is not mutated. Calling behaviour is unchanged.
218
+ """
219
+
220
+ specified = sum(x is not None for x in (prototype, signature, factory))
221
+ if specified == 0:
222
+ raise TypeError(
223
+ "with_signature requires one of prototype=, signature=, or factory="
224
+ )
225
+ if specified > 1:
226
+ raise TypeError(
227
+ "with_signature accepts only one of prototype=, signature=, or factory="
228
+ )
229
+
230
+ def _decorator(wrapped):
231
+ def _wrapper(wrapped, instance, args, kwargs):
232
+ return wrapped(*args, **kwargs)
233
+
234
+ if signature is not None:
235
+ resolved = signature
236
+ elif prototype is not None:
237
+ resolved = _inspect_signature(prototype)
238
+ else:
239
+ produced = factory(wrapped)
240
+ resolved = (
241
+ produced
242
+ if isinstance(produced, Signature)
243
+ else _inspect_signature(produced)
244
+ )
245
+
246
+ return _SignatureFunctionWrapper(wrapped, _wrapper, resolved)
247
+
248
+ if wrapped is None:
249
+ return _decorator
250
+ return _decorator(wrapped)