wrapt 2.1.0rc1__cp314-cp314-win_arm64.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.

Potentially problematic release.


This version of wrapt might be problematic. Click here for more details.

wrapt/__init__.py ADDED
@@ -0,0 +1,64 @@
1
+ """
2
+ Wrapt is a library for decorators, wrappers and monkey patching.
3
+ """
4
+
5
+ __version_info__ = ("2", "1", "0", "rc1")
6
+ __version__ = ".".join(__version_info__)
7
+
8
+ from .__wrapt__ import (
9
+ BaseObjectProxy,
10
+ BoundFunctionWrapper,
11
+ CallableObjectProxy,
12
+ FunctionWrapper,
13
+ PartialCallableObjectProxy,
14
+ partial,
15
+ )
16
+ from .decorators import AdapterFactory, adapter_factory, decorator, synchronized
17
+ from .importer import (
18
+ discover_post_import_hooks,
19
+ notify_module_loaded,
20
+ register_post_import_hook,
21
+ when_imported,
22
+ )
23
+ from .patches import (
24
+ apply_patch,
25
+ function_wrapper,
26
+ patch_function_wrapper,
27
+ resolve_path,
28
+ transient_function_wrapper,
29
+ wrap_function_wrapper,
30
+ wrap_object,
31
+ wrap_object_attribute,
32
+ )
33
+ from .proxies import AutoObjectProxy, LazyObjectProxy, ObjectProxy, lazy_import
34
+ from .weakrefs import WeakFunctionProxy
35
+
36
+ __all__ = (
37
+ "AutoObjectProxy",
38
+ "BaseObjectProxy",
39
+ "BoundFunctionWrapper",
40
+ "CallableObjectProxy",
41
+ "FunctionWrapper",
42
+ "LazyObjectProxy",
43
+ "ObjectProxy",
44
+ "PartialCallableObjectProxy",
45
+ "partial",
46
+ "AdapterFactory",
47
+ "adapter_factory",
48
+ "decorator",
49
+ "synchronized",
50
+ "discover_post_import_hooks",
51
+ "notify_module_loaded",
52
+ "register_post_import_hook",
53
+ "when_imported",
54
+ "apply_patch",
55
+ "function_wrapper",
56
+ "lazy_import",
57
+ "patch_function_wrapper",
58
+ "resolve_path",
59
+ "transient_function_wrapper",
60
+ "wrap_function_wrapper",
61
+ "wrap_object",
62
+ "wrap_object_attribute",
63
+ "WeakFunctionProxy",
64
+ )
wrapt/__init__.pyi ADDED
@@ -0,0 +1,388 @@
1
+ import sys
2
+
3
+ if sys.version_info >= (3, 10):
4
+ from inspect import FullArgSpec
5
+ from types import ModuleType, TracebackType
6
+ from typing import (
7
+ Any,
8
+ Callable,
9
+ Concatenate,
10
+ Generic,
11
+ ParamSpec,
12
+ Protocol,
13
+ TypeVar,
14
+ overload,
15
+ )
16
+
17
+ P = ParamSpec("P")
18
+ R = TypeVar("R", covariant=True)
19
+
20
+ T = TypeVar("T", bound=Any)
21
+
22
+ # Need two sets of ParamSpec/TypeVar for generics in some cases to ensure
23
+ # that mypy, pyrefly and ty all work correctly. More specifically need a
24
+ # separate set for cases where extracting the type or instance from the
25
+ # first argument of a callable where binding is involved.
26
+
27
+ P1 = ParamSpec("P1")
28
+ R1 = TypeVar("R1", covariant=True)
29
+
30
+ T1 = TypeVar("T1", bound=Any)
31
+
32
+ P2 = ParamSpec("P2")
33
+ R2 = TypeVar("R2", covariant=True)
34
+
35
+ T2 = TypeVar("T2", bound=Any)
36
+
37
+ class Boolean(Protocol):
38
+ def __bool__(self) -> bool: ...
39
+
40
+ # ObjectProxy
41
+
42
+ class BaseObjectProxy(Generic[T]):
43
+ __wrapped__: T
44
+ def __init__(self, wrapped: T) -> None: ...
45
+
46
+ class ObjectProxy(BaseObjectProxy[T]):
47
+ def __init__(self, wrapped: T) -> None: ...
48
+
49
+ class AutoObjectProxy(BaseObjectProxy[T]):
50
+ def __init__(self, wrapped: T) -> None: ...
51
+
52
+ # LazyObjectProxy
53
+
54
+ class LazyObjectProxy(AutoObjectProxy[T]):
55
+ def __init__(
56
+ self, callback: Callable[[], T] | None, *, interface: Any = ...
57
+ ) -> None: ...
58
+
59
+ @overload
60
+ def lazy_import(name: str) -> LazyObjectProxy[ModuleType]: ...
61
+ @overload
62
+ def lazy_import(
63
+ name: str, attribute: str, *, interface: Any = ...
64
+ ) -> LazyObjectProxy[Any]: ...
65
+
66
+ # CallableObjectProxy
67
+
68
+ class CallableObjectProxy(BaseObjectProxy[T]):
69
+ def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
70
+
71
+ # PartialCallableObjectProxy
72
+
73
+ class PartialCallableObjectProxy:
74
+ def __init__(
75
+ self, func: Callable[..., Any], *args: Any, **kwargs: Any
76
+ ) -> None: ...
77
+ def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
78
+
79
+ def partial(
80
+ func: Callable[..., Any], /, *args: Any, **kwargs: Any
81
+ ) -> Callable[..., Any]: ...
82
+
83
+ # WeakFunctionProxy
84
+
85
+ class WeakFunctionProxy:
86
+ def __init__(
87
+ self,
88
+ wrapped: Callable[..., Any],
89
+ callback: Callable[..., Any] | None = None,
90
+ ) -> None: ...
91
+ def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
92
+
93
+ # FunctionWrapper
94
+
95
+ WrappedFunction = Callable[P, R]
96
+
97
+ GenericCallableWrapperFunction = Callable[
98
+ [WrappedFunction[P, R], Any, tuple[Any, ...], dict[str, Any]], R
99
+ ]
100
+
101
+ ClassMethodWrapperFunction = Callable[
102
+ [type[Any], WrappedFunction[P, R], Any, tuple[Any, ...], dict[str, Any]], R
103
+ ]
104
+
105
+ InstanceMethodWrapperFunction = Callable[
106
+ [Any, WrappedFunction[P, R], Any, tuple[Any, ...], dict[str, Any]], R
107
+ ]
108
+
109
+ WrapperFunction = (
110
+ GenericCallableWrapperFunction[P, R]
111
+ | ClassMethodWrapperFunction[P, R]
112
+ | InstanceMethodWrapperFunction[P, R]
113
+ )
114
+
115
+ class _FunctionWrapperBase(ObjectProxy[WrappedFunction[P, R]]):
116
+ _self_instance: Any
117
+ _self_wrapper: WrapperFunction[P, R]
118
+ _self_enabled: bool | Boolean | Callable[[], bool] | None
119
+ _self_binding: str
120
+ _self_parent: Any
121
+ _self_owner: Any
122
+
123
+ class BoundFunctionWrapper(_FunctionWrapperBase[P1, R1]):
124
+ def __call__(self, *args: P1.args, **kwargs: P1.kwargs) -> R1: ...
125
+
126
+ # Note that for following overloads, testing with mypy and ty they still do
127
+ # not handle static methods being decorated but to best knowledge this is
128
+ # a limitation in those type checkers. Testing with pyrefly fails on any
129
+ # type of bound method. Testing with pyright handles case correctly.
130
+ #
131
+ # Also, note that use of T2, P2 and R2 in first two cases is also required
132
+ # to ensure correct handling by mypy and ty, so do not change to use of T1,
133
+ # P1 and R1.
134
+
135
+ @overload
136
+ def __get__( # Required to ensure mypy, pyrefly and ty works correctly
137
+ self: BoundFunctionWrapper[Concatenate[T2, P2], R2],
138
+ instance: T2,
139
+ owner: type[T2] | None = None,
140
+ ) -> BoundFunctionWrapper[P2, R2]: ...
141
+ @overload
142
+ def __get__( # Required to ensure mypy, pyrefly and ty works correctly
143
+ self: BoundFunctionWrapper[Concatenate[T2, P2], R2],
144
+ instance: T2,
145
+ owner: type[Any] | None = None,
146
+ ) -> BoundFunctionWrapper[P2, R2]: ...
147
+ @overload
148
+ def __get__( # Required to ensure mypy, pyrefly and ty works correctly
149
+ self, instance: None, owner: type[T1] | None = None
150
+ ) -> BoundFunctionWrapper[P1, R1]: ...
151
+ @overload
152
+ def __get__( # Required to ensure pyright works correctly
153
+ self, instance: T1, owner: type[T1] | None = None
154
+ ) -> BoundFunctionWrapper[P1, R1]: ...
155
+
156
+ class FunctionWrapper(_FunctionWrapperBase[P1, R1]):
157
+ def __init__(
158
+ self,
159
+ wrapped: WrappedFunction[P1, R1],
160
+ wrapper: WrapperFunction[P1, R1],
161
+ enabled: bool | Boolean | Callable[[], bool] | None = None,
162
+ ) -> None: ...
163
+ def __call__(self, *args: P1.args, **kwargs: P1.kwargs) -> R1: ...
164
+
165
+ # Note that for following overloads, testing with mypy and ty they still do
166
+ # not handle static methods being decorated but to best knowledge this is
167
+ # a limitation in those type checkers. Testing with pyrefly fails on any
168
+ # type of bound method. Testing with pyright handles case correctly.
169
+ #
170
+ # Also, note that use of T2, P2 and R2 in first two cases is also required
171
+ # to ensure correct handling by mypy and ty, so do not change to use of T1,
172
+ # P1 and R1.
173
+
174
+ @overload
175
+ def __get__( # Required to ensure mypy, pyrefly and ty works correctly
176
+ self: FunctionWrapper[Concatenate[T2, P2], R2],
177
+ instance: T2,
178
+ owner: type[Any] | None = None,
179
+ ) -> BoundFunctionWrapper[P2, R2]: ...
180
+ @overload
181
+ def __get__( # Required to ensure mypy, pyrefly and ty works correctly
182
+ self: FunctionWrapper[Concatenate[T2, P2], R2],
183
+ instance: T2,
184
+ owner: type[T2] | None = None,
185
+ ) -> BoundFunctionWrapper[P2, R2]: ...
186
+ @overload
187
+ def __get__( # Required to ensure mypy, pyrefly and ty works correctly
188
+ self, instance: None, owner: type[T1] | None = None
189
+ ) -> BoundFunctionWrapper[P1, R1]: ...
190
+ @overload
191
+ def __get__( # Required to ensure pyright works correctly
192
+ self, instance: T1, owner: type[T1] | None = None
193
+ ) -> BoundFunctionWrapper[P1, R1]: ...
194
+
195
+ # AdapterFactory/adapter_factory()
196
+
197
+ class AdapterFactory(Protocol):
198
+ def __call__(
199
+ self, wrapped: Callable[..., Any]
200
+ ) -> str | FullArgSpec | Callable[..., Any]: ...
201
+
202
+ def adapter_factory(wrapped: Callable[..., Any]) -> AdapterFactory: ...
203
+
204
+ # decorator()
205
+
206
+ class Descriptor(Protocol):
207
+ def __get__(self, instance: Any, owner: type[Any] | None = None) -> Any: ...
208
+
209
+ class FunctionDecorator:
210
+ @overload
211
+ def __call__(
212
+ self,
213
+ callable: (
214
+ Callable[P, R]
215
+ | Callable[Concatenate[type[T], P], R] # Required for pylance
216
+ # | Callable[Concatenate[Any, P], R] # Breaks mypy, pyrefly and ty
217
+ | Callable[[type[T]], R] # Required for pylance
218
+ ),
219
+ ) -> FunctionWrapper[P, R]: ...
220
+ @overload
221
+ def __call__(self, callable: Descriptor) -> FunctionWrapper[P, Any]: ...
222
+
223
+ class PartialFunctionDecorator:
224
+ @overload
225
+ def __call__(
226
+ self, wrapper: GenericCallableWrapperFunction[P, R], /
227
+ ) -> FunctionDecorator: ...
228
+ @overload
229
+ def __call__(
230
+ self, wrapper: ClassMethodWrapperFunction[P, R], /
231
+ ) -> FunctionDecorator: ...
232
+ @overload
233
+ def __call__(
234
+ self, wrapper: InstanceMethodWrapperFunction[P, R], /
235
+ ) -> FunctionDecorator: ...
236
+
237
+ # ... Decorator applied to class type.
238
+
239
+ @overload
240
+ def decorator(wrapper: type[T], /) -> FunctionDecorator: ...
241
+
242
+ # ... Decorator applied to function or method.
243
+
244
+ @overload
245
+ def decorator(
246
+ wrapper: GenericCallableWrapperFunction[P, R], /
247
+ ) -> FunctionDecorator: ...
248
+ @overload
249
+ def decorator(
250
+ wrapper: ClassMethodWrapperFunction[P, R], /
251
+ ) -> FunctionDecorator: ...
252
+ @overload
253
+ def decorator(
254
+ wrapper: InstanceMethodWrapperFunction[P, R], /
255
+ ) -> FunctionDecorator: ...
256
+
257
+ # ... Positional arguments.
258
+
259
+ @overload
260
+ def decorator(
261
+ *,
262
+ enabled: bool | Boolean | Callable[[], bool] | None = None,
263
+ adapter: str | FullArgSpec | AdapterFactory | Callable[..., Any] | None = None,
264
+ proxy: type[FunctionWrapper[Any, Any]] | None = None,
265
+ ) -> PartialFunctionDecorator: ...
266
+
267
+ # function_wrapper()
268
+
269
+ @overload
270
+ def function_wrapper(wrapper: type[Any]) -> FunctionDecorator: ...
271
+ @overload
272
+ def function_wrapper(
273
+ wrapper: GenericCallableWrapperFunction[P, R],
274
+ ) -> FunctionDecorator: ...
275
+ @overload
276
+ def function_wrapper(
277
+ wrapper: ClassMethodWrapperFunction[P, R],
278
+ ) -> FunctionDecorator: ...
279
+ @overload
280
+ def function_wrapper(
281
+ wrapper: InstanceMethodWrapperFunction[P, R],
282
+ ) -> FunctionDecorator: ...
283
+ # @overload
284
+ # def function_wrapper(wrapper: Any) -> FunctionDecorator: ... # Don't use, breaks stuff.
285
+
286
+ # wrap_function_wrapper()
287
+
288
+ def wrap_function_wrapper(
289
+ target: ModuleType | type[Any] | Any | str,
290
+ name: str,
291
+ wrapper: WrapperFunction[P, R],
292
+ ) -> FunctionWrapper[P, R]: ...
293
+
294
+ # patch_function_wrapper()
295
+
296
+ class WrapperDecorator:
297
+ def __call__(self, wrapper: WrapperFunction[P, R]) -> FunctionWrapper[P, R]: ...
298
+
299
+ def patch_function_wrapper(
300
+ target: ModuleType | type[Any] | Any | str,
301
+ name: str,
302
+ enabled: bool | Boolean | Callable[[], bool] | None = None,
303
+ ) -> WrapperDecorator: ...
304
+
305
+ # transient_function_wrapper()
306
+
307
+ class TransientDecorator:
308
+ def __call__(self, wrapper: WrapperFunction[P, R]) -> FunctionDecorator: ...
309
+
310
+ def transient_function_wrapper(
311
+ target: ModuleType | type[Any] | Any | str, name: str
312
+ ) -> TransientDecorator: ...
313
+
314
+ # resolve_path()
315
+
316
+ def resolve_path(
317
+ target: ModuleType | type[Any] | Any | str, name: str
318
+ ) -> tuple[ModuleType | type[Any] | Any, str, Callable[..., Any]]: ...
319
+
320
+ # apply_patch()
321
+
322
+ def apply_patch(
323
+ parent: ModuleType | type[Any] | Any,
324
+ attribute: str,
325
+ replacement: Any,
326
+ ) -> None: ...
327
+
328
+ # wrap_object()
329
+
330
+ WrapperFactory = Callable[
331
+ [Callable[..., Any], tuple[Any, ...], dict[str, Any]], type[ObjectProxy[Any]]
332
+ ]
333
+
334
+ def wrap_object(
335
+ target: ModuleType | type[Any] | Any | str,
336
+ name: str,
337
+ factory: WrapperFactory | type[ObjectProxy[Any]],
338
+ args: tuple[Any, ...],
339
+ kwargs: dict[str, Any],
340
+ ) -> Any: ...
341
+
342
+ # wrap_object_attribute()
343
+
344
+ def wrap_object_attribute(
345
+ target: ModuleType | type[Any] | Any | str,
346
+ name: str,
347
+ factory: WrapperFactory | type[ObjectProxy[Any]],
348
+ args: tuple[Any, ...] = (),
349
+ kwargs: dict[str, Any] = {},
350
+ ) -> Any: ...
351
+
352
+ # register_post_import_hook()
353
+
354
+ def register_post_import_hook(
355
+ hook: Callable[[ModuleType], Any] | str, name: str
356
+ ) -> None: ...
357
+
358
+ # discover_post_import_hooks()
359
+
360
+ def discover_post_import_hooks(group: str) -> None: ...
361
+
362
+ # notify_module_loaded()
363
+
364
+ def notify_module_loaded(module: ModuleType) -> None: ...
365
+
366
+ # when_imported()
367
+
368
+ class ImportHookDecorator:
369
+ def __call__(self, hook: Callable[[ModuleType], Any]) -> Callable[..., Any]: ...
370
+
371
+ def when_imported(name: str) -> ImportHookDecorator: ...
372
+
373
+ # synchronized()
374
+
375
+ class SynchronizedObject:
376
+ def __call__(self, wrapped: Callable[P, R]) -> Callable[P, R]: ...
377
+ def __enter__(self) -> Any: ...
378
+ def __exit__(
379
+ self,
380
+ exc_type: type[BaseException] | None,
381
+ exc_value: BaseException | None,
382
+ traceback: TracebackType | None,
383
+ ) -> bool | None: ...
384
+
385
+ @overload
386
+ def synchronized(wrapped: Callable[P, R]) -> Callable[P, R]: ...
387
+ @overload
388
+ def synchronized(wrapped: Any) -> SynchronizedObject: ...
wrapt/__wrapt__.py ADDED
@@ -0,0 +1,42 @@
1
+ """This module is used to switch between C and Python implementations of the
2
+ wrappers.
3
+ """
4
+
5
+ import os
6
+
7
+ from .wrappers import BoundFunctionWrapper, CallableObjectProxy, FunctionWrapper
8
+ from .wrappers import ObjectProxy as BaseObjectProxy
9
+ from .wrappers import PartialCallableObjectProxy, _FunctionWrapperBase
10
+
11
+ # Try to use C extensions if not disabled.
12
+
13
+ _using_c_extension = False
14
+
15
+ _use_extensions = not os.environ.get("WRAPT_DISABLE_EXTENSIONS")
16
+
17
+ if _use_extensions:
18
+ try:
19
+ from ._wrappers import ( # type: ignore[no-redef,import-not-found]
20
+ BoundFunctionWrapper,
21
+ CallableObjectProxy,
22
+ FunctionWrapper,
23
+ )
24
+ from ._wrappers import ObjectProxy as BaseObjectProxy # type: ignore[no-redef]
25
+ from ._wrappers import ( # type: ignore[no-redef,import-not-found]
26
+ PartialCallableObjectProxy,
27
+ _FunctionWrapperBase,
28
+ )
29
+
30
+ _using_c_extension = True
31
+ except ImportError:
32
+ # C extensions not available, using Python implementations
33
+ pass
34
+
35
+
36
+ def partial(*args, **kwargs):
37
+ """Create a callable object proxy with partial application of the given
38
+ arguments and keywords. This behaves the same as `functools.partial`, but
39
+ implemented using the `ObjectProxy` class to provide better support for
40
+ introspection.
41
+ """
42
+ return PartialCallableObjectProxy(*args, **kwargs)