wrapt 2.4.2rc1__tar.gz → 2.5.0__tar.gz
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-2.4.2rc1/src/wrapt.egg-info → wrapt-2.5.0}/PKG-INFO +5 -1
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/README.md +28 -1
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/RELEASE.rst +4 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/__init__.py +3 -1
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/_wrappers.c +109 -0
- wrapt-2.5.0/src/wrapt/doc.py +128 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/proxies.py +7 -7
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/signature.py +24 -4
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/weakrefs.py +45 -5
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/wrappers.py +124 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt-stubs/__init__.pyi +28 -5
- {wrapt-2.4.2rc1 → wrapt-2.5.0/src/wrapt.egg-info}/PKG-INFO +5 -1
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt.egg-info/SOURCES.txt +9 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_annotations.py +10 -10
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_attribute_wrapper.py +5 -5
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_copy.py +3 -3
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_find_wrapper.py +2 -2
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_function_wrapper.py +7 -7
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_inplace_operators.py +49 -49
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_lru_cache.py +2 -2
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_object_proxy.py +233 -233
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_object_proxy_hook.py +1 -1
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_object_proxy_self_dict.py +13 -13
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_object_proxy_type_checks.py +35 -35
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_pickle.py +13 -4
- wrapt-2.5.0/tests/core/test_proxy_doc_descriptor.py +208 -0
- wrapt-2.5.0/tests/core/test_proxy_new_arguments.py +149 -0
- wrapt-2.5.0/tests/core/test_proxy_operands.py +397 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_pyerr_clear_propagation.py +8 -8
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_runtime_type_hints.py +9 -9
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_type_module_attribute.py +27 -27
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_unwrap_object.py +3 -3
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_weak_function_proxy.py +84 -0
- wrapt-2.5.0/tests/core/test_with_doc.py +373 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_with_signature.py +151 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_wrapper_chain.py +2 -2
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_object_proxy_arithmetic_t1.py +4 -4
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_object_proxy_context_t1.py +5 -5
- wrapt-2.5.0/tests/mypy/mypy_proxy_new_t1.py +63 -0
- wrapt-2.5.0/tests/mypy/mypy_with_doc_t1.out +4 -0
- wrapt-2.5.0/tests/mypy/mypy_with_doc_t1.py +83 -0
- wrapt-2.5.0/tests/mypy/mypy_wrap_function_wrapper_t4.out +1 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/stress/stress_init_reinit.py +3 -3
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/stress/stress_inplace_ops.py +3 -3
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/stress/stress_read_during_mutate.py +3 -3
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/stress/stress_set_wrapped.py +3 -3
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/LICENSE +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/MANIFEST.in +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/pyproject.toml +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/setup.cfg +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/setup.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/__wrapt__.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/arguments.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/caching.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/decorators.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/exceptions.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/importer.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/patches.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt/synchronization.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt.egg-info/dependency_links.txt +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt.egg-info/not-zip-safe +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt.egg-info/requires.txt +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/src/wrapt.egg-info/top_level.txt +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/conftest.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/compat.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/module1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/module2.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_adapter.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_adapter_annotations.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_adapter_kwonly.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_auto_object_proxy.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_callable_binding_no_args.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_callable_object_proxy.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_class.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_class_mro_entries.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_decorator_state.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_decorators.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_deferred_patching.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_descriptor_get_class_access.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_descriptors.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_enabled_bool_error.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_entry_points.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_formatargspec.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_function.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_inheritance.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_inner_classmethod.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_inner_staticmethod.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_instancemethod.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_lazy_object_proxy.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_memoize.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_missing_sentinel.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_monkey_patching.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_nested_function.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_outer_classmethod.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_outer_staticmethod.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_post_import_hooks.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_resolve_path.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_scoped_function_wrapper.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_synchronized_async.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_synchronized_lock.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_synchronized_markers.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_transient_function_wrapper.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_uninitialized_wrapper.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/core/test_update_attributes.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_auto_object_proxy_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_auto_object_proxy_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_decorator_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_decorator_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t2.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t2.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t3.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t3.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t4.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t4.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t5.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t5.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t6.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t6.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t7.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t7.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t8.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_cls_t8.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t2.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t2.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t3.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t3.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t4.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t4.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t5.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t5.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t6.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t6.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t7.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_function_wrapper_fn_t7.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_object_proxy_arithmetic_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_object_proxy_container_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_object_proxy_container_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_object_proxy_context_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_partial_callable_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_partial_callable_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_patch_function_wrapper_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_patch_function_wrapper_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_patching_primitives_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_patching_primitives_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_post_import_hooks_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_post_import_hooks_t1.py +0 -0
- /wrapt-2.4.2rc1/tests/mypy/mypy_wrap_function_wrapper_t1.out → /wrapt-2.5.0/tests/mypy/mypy_proxy_new_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_synchronized_lock_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_synchronized_lock_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_transient_function_wrapper_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_transient_function_wrapper_t1.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_weak_function_proxy_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_weak_function_proxy_t1.py +0 -0
- /wrapt-2.4.2rc1/tests/mypy/mypy_wrap_function_wrapper_t2.out → /wrapt-2.5.0/tests/mypy/mypy_wrap_function_wrapper_t1.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t1.py +0 -0
- /wrapt-2.4.2rc1/tests/mypy/mypy_wrap_function_wrapper_t3.out → /wrapt-2.5.0/tests/mypy/mypy_wrap_function_wrapper_t2.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t2.py +0 -0
- /wrapt-2.4.2rc1/tests/mypy/mypy_wrap_function_wrapper_t4.out → /wrapt-2.5.0/tests/mypy/mypy_wrap_function_wrapper_t3.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t3.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t4.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t5.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t5.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t6.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t6.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t7.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t7.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t8.out +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/mypy/mypy_wrap_function_wrapper_t8.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/stress/runner.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tests/stress/stress_function_wrapper_call.py +0 -0
- {wrapt-2.4.2rc1 → wrapt-2.5.0}/tox.ini +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: wrapt
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.5.0
|
|
4
4
|
Summary: Module for decorators, wrappers and monkey patching.
|
|
5
5
|
Author-email: Graham Dumpleton <Graham.Dumpleton@gmail.com>
|
|
6
6
|
License-Expression: BSD-2-Clause
|
|
@@ -28,6 +28,10 @@ Requires-Dist: pytest; extra == "dev"
|
|
|
28
28
|
Requires-Dist: setuptools; extra == "dev"
|
|
29
29
|
Dynamic: license-file
|
|
30
30
|
|
|
31
|
+
.. image:: https://raw.githubusercontent.com/GrahamDumpleton/wrapt/develop/docs/_static/wrapt-logo.svg
|
|
32
|
+
:alt: wrapt
|
|
33
|
+
:width: 182px
|
|
34
|
+
|
|
31
35
|
wrapt
|
|
32
36
|
=====
|
|
33
37
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
<img src="https://raw.githubusercontent.com/GrahamDumpleton/wrapt/develop/docs/_static/wrapt-logo.svg" alt="wrapt" width="182">
|
|
2
|
+
|
|
1
3
|
# wrapt
|
|
2
4
|
|
|
3
5
|
[](https://pypi.python.org/pypi/wrapt)
|
|
@@ -94,6 +96,31 @@ For comprehensive documentation, examples, and advanced usage patterns, visit:
|
|
|
94
96
|
|
|
95
97
|
**[wrapt.readthedocs.io](https://wrapt.readthedocs.io/)**
|
|
96
98
|
|
|
99
|
+
## Workshops
|
|
100
|
+
|
|
101
|
+
Two sets of guided, hands-on workshops run in JupyterLab and check your work as you go. Neither needs anything installed.
|
|
102
|
+
|
|
103
|
+
### Python decorators
|
|
104
|
+
|
|
105
|
+
[](https://grahamdumpleton.github.io/decorator-workshops/lab/index.html)
|
|
106
|
+
[](https://mybinder.org/v2/gh/GrahamDumpleton/decorator-workshops/main?urlpath=lab)
|
|
107
|
+
[](https://codespaces.new/GrahamDumpleton/decorator-workshops?quickstart=1)
|
|
108
|
+
|
|
109
|
+
[decorator-workshops](https://github.com/GrahamDumpleton/decorator-workshops) teaches Python decorators using only the standard library, from what a decorator is through to writing your own for functions, methods, classes and coroutines. It is the place to start if decorators are new to you, and covers the ground the wrapt workshops build on. The badges above start the workshops in your browser with no account or server at all, on [mybinder.org](https://mybinder.org), or in [GitHub Codespaces](https://github.com/features/codespaces). The repository README explains each option and how to run the workshops locally.
|
|
110
|
+
|
|
111
|
+
### wrapt
|
|
112
|
+
|
|
113
|
+
[](https://mybinder.org/v2/gh/GrahamDumpleton/wrapt-workshops/main?urlpath=lab)
|
|
114
|
+
[](https://codespaces.new/GrahamDumpleton/wrapt-workshops?quickstart=1)
|
|
115
|
+
|
|
116
|
+
[wrapt-workshops](https://github.com/GrahamDumpleton/wrapt-workshops) holds three collections of workshops on wrapt itself: writing decorators with wrapt, monkey patching with wrapt, and object proxies with wrapt, each shown beside the standard library way of doing the same thing. The badges above start them on [mybinder.org](https://mybinder.org) or in [GitHub Codespaces](https://github.com/features/codespaces). The repository README explains each option and how to run the workshops locally.
|
|
117
|
+
|
|
118
|
+
## Related Projects
|
|
119
|
+
|
|
120
|
+
If the monkey patching side of wrapt is what brought you here, also look at [wrapture](https://github.com/GrahamDumpleton/wrapture). It is a sibling project built on the monkey patching machinery of wrapt, and provides a higher level API on top of it. A clean lifecycle and behaviour vocabulary over `wrap_object()` lets you point at a method by name and stub it, fail it, transform its arguments or result, or wrap it with a decorator, then remove it again. On top of that sit unit testing, where the real code runs and how calls flowed through it is recorded and asserted on, and ad-hoc tracing, where a running application emits a structured call tree with no code changes, with export to OpenTelemetry. The [wrapture documentation](https://wrapture.readthedocs.io/) has the details, and [wrapture-workshops](https://github.com/GrahamDumpleton/wrapture-workshops) has guided workshops of its own.
|
|
121
|
+
|
|
122
|
+
To apply post import hook patches to an application without changing it, [autowrapt](https://github.com/GrahamDumpleton/autowrapt) triggers `wrapt.discover_post_import_hooks()` at interpreter startup. Install it alongside the application and name the entry point group in the `AUTOWRAPT_BOOTSTRAP` environment variable. The mechanism is described under [applying patches at interpreter startup](https://wrapt.readthedocs.io/en/latest/monkey.html#applying-patches-at-interpreter-startup) in the documentation.
|
|
123
|
+
|
|
97
124
|
## Supported Python Versions
|
|
98
125
|
|
|
99
126
|
- Python 3.9+
|
|
@@ -104,7 +131,7 @@ For comprehensive documentation, examples, and advanced usage patterns, visit:
|
|
|
104
131
|
|
|
105
132
|
We welcome contributions! This is a pretty casual process - if you're interested in suggesting changes, improvements, or have found a bug, please reach out via the [GitHub issue tracker](https://github.com/GrahamDumpleton/wrapt/issues/). Whether it's a small fix, new feature idea, or just a question about how something works, feel free to start a discussion.
|
|
106
133
|
|
|
107
|
-
Please note that wrapt is now considered a mature project. We're not expecting any significant new developments or major feature additions. The primary focus is on ensuring that the package continues to work correctly with newer Python versions and maintaining compatibility as the Python ecosystem evolves.
|
|
134
|
+
Please note that wrapt is now considered a mature project. We're not expecting any significant new developments or major feature additions. The primary focus is on ensuring that the package continues to work correctly with newer Python versions and maintaining compatibility as the Python ecosystem evolves. Higher level functionality is being developed in [wrapture](https://github.com/GrahamDumpleton/wrapture) rather than here.
|
|
108
135
|
|
|
109
136
|
### Testing
|
|
110
137
|
|
|
@@ -13,7 +13,7 @@ def _format_version(parts):
|
|
|
13
13
|
)
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
__version_info__ = ("2", "
|
|
16
|
+
__version_info__ = ("2", "5", "0")
|
|
17
17
|
__version__ = _format_version(__version_info__)
|
|
18
18
|
|
|
19
19
|
from .__wrapt__ import (
|
|
@@ -31,6 +31,7 @@ from .decorators import (
|
|
|
31
31
|
bind_state_to_wrapper,
|
|
32
32
|
decorator,
|
|
33
33
|
)
|
|
34
|
+
from .doc import with_doc
|
|
34
35
|
from .exceptions import (
|
|
35
36
|
PathResolutionError,
|
|
36
37
|
TargetModuleNotFoundError,
|
|
@@ -101,6 +102,7 @@ __all__ = (
|
|
|
101
102
|
"mark_as_sync",
|
|
102
103
|
"sync_to_async",
|
|
103
104
|
"synchronized",
|
|
105
|
+
"with_doc",
|
|
104
106
|
"with_signature",
|
|
105
107
|
"discover_post_import_hooks",
|
|
106
108
|
"notify_module_loaded",
|
|
@@ -294,6 +294,77 @@ static int wrapt_type_has_attr(PyTypeObject *type, PyObject *name)
|
|
|
294
294
|
return 0;
|
|
295
295
|
}
|
|
296
296
|
|
|
297
|
+
/* Look for a __doc__ descriptor defined by a derived class. Walks the MRO
|
|
298
|
+
* of the type checking each tp_dict directly for __doc__, skipping the
|
|
299
|
+
* plain docstring entries which every class carries, being a string or
|
|
300
|
+
* None for a class without a docstring. The proxy base types hold a plain
|
|
301
|
+
* string docstring, so any descriptor found must come from a derived
|
|
302
|
+
* class, which is taken to mean it wants to control what __doc__ reports
|
|
303
|
+
* for its instances in place of the default delegation to the wrapped
|
|
304
|
+
* object. Only __doc__ is treated this way, __module__ always delegates.
|
|
305
|
+
* The descriptor is invoked directly rather than falling back to the
|
|
306
|
+
* generic attribute lookup, since that would stop at the None or docstring
|
|
307
|
+
* entry of a derived class which inherits the descriptor from its base.
|
|
308
|
+
* Returns a new reference, or NULL without an exception set if there is
|
|
309
|
+
* no such descriptor. */
|
|
310
|
+
|
|
311
|
+
static PyObject *wrapt_lookup_doc_descriptor(PyTypeObject *type,
|
|
312
|
+
PyObject *name)
|
|
313
|
+
{
|
|
314
|
+
PyObject *mro = type->tp_mro;
|
|
315
|
+
Py_ssize_t i, n;
|
|
316
|
+
|
|
317
|
+
if (mro == NULL)
|
|
318
|
+
return NULL;
|
|
319
|
+
|
|
320
|
+
n = PyTuple_GET_SIZE(mro);
|
|
321
|
+
|
|
322
|
+
for (i = 0; i < n; i++)
|
|
323
|
+
{
|
|
324
|
+
PyTypeObject *t = (PyTypeObject *)PyTuple_GET_ITEM(mro, i);
|
|
325
|
+
PyObject *dict = t->tp_dict;
|
|
326
|
+
PyObject *entry = NULL;
|
|
327
|
+
|
|
328
|
+
if (dict == NULL)
|
|
329
|
+
continue;
|
|
330
|
+
|
|
331
|
+
#if PY_VERSION_HEX >= 0x030D0000
|
|
332
|
+
if (PyDict_GetItemRef(dict, name, &entry) < 0)
|
|
333
|
+
{
|
|
334
|
+
PyErr_WriteUnraisable((PyObject *)t);
|
|
335
|
+
PyErr_Clear();
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
#else
|
|
339
|
+
entry = PyDict_GetItemWithError(dict, name);
|
|
340
|
+
if (entry == NULL && PyErr_Occurred())
|
|
341
|
+
{
|
|
342
|
+
PyErr_WriteUnraisable((PyObject *)t);
|
|
343
|
+
PyErr_Clear();
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
Py_XINCREF(entry);
|
|
347
|
+
#endif
|
|
348
|
+
|
|
349
|
+
if (entry == NULL)
|
|
350
|
+
continue;
|
|
351
|
+
|
|
352
|
+
if (entry == Py_None || PyUnicode_Check(entry))
|
|
353
|
+
{
|
|
354
|
+
Py_DECREF(entry);
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (Py_TYPE(entry)->tp_descr_get)
|
|
359
|
+
return entry;
|
|
360
|
+
|
|
361
|
+
Py_DECREF(entry);
|
|
362
|
+
return NULL;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return NULL;
|
|
366
|
+
}
|
|
367
|
+
|
|
297
368
|
/* Non-error-setting predicate: is this object an instance of any wrapt
|
|
298
369
|
* proxy type? Walks the MRO directly. PyType_GetModule sets an exception
|
|
299
370
|
* for heap types that lack a module association (e.g. pure-Python user
|
|
@@ -3445,7 +3516,22 @@ static PyObject *WraptObjectProxy_getattro(WraptObjectProxyObject *self,
|
|
|
3445
3516
|
if (wrapt_name_equals(name, state->str_module))
|
|
3446
3517
|
return WraptObjectProxy_get_module(self);
|
|
3447
3518
|
if (wrapt_name_equals(name, state->str_doc))
|
|
3519
|
+
{
|
|
3520
|
+
/* A derived class may define its own __doc__ descriptor, in which
|
|
3521
|
+
* case that is consulted in place of the wrapped object. */
|
|
3522
|
+
|
|
3523
|
+
PyObject *descriptor = wrapt_lookup_doc_descriptor(Py_TYPE(self), name);
|
|
3524
|
+
|
|
3525
|
+
if (descriptor)
|
|
3526
|
+
{
|
|
3527
|
+
result = (Py_TYPE(descriptor)->tp_descr_get)(
|
|
3528
|
+
descriptor, (PyObject *)self, (PyObject *)Py_TYPE(self));
|
|
3529
|
+
Py_DECREF(descriptor);
|
|
3530
|
+
return result;
|
|
3531
|
+
}
|
|
3532
|
+
|
|
3448
3533
|
return WraptObjectProxy_get_doc(self);
|
|
3534
|
+
}
|
|
3449
3535
|
|
|
3450
3536
|
object = PyObject_GenericGetAttr((PyObject *)self, name);
|
|
3451
3537
|
|
|
@@ -3527,7 +3613,30 @@ static int WraptObjectProxy_setattro(WraptObjectProxyObject *self,
|
|
|
3527
3613
|
if (wrapt_name_equals(name, state->str_module))
|
|
3528
3614
|
return WraptObjectProxy_set_module(self, value);
|
|
3529
3615
|
if (wrapt_name_equals(name, state->str_doc))
|
|
3616
|
+
{
|
|
3617
|
+
/* A derived class may define its own __doc__ descriptor, in which
|
|
3618
|
+
* case assignment and deletion go to it rather than being forwarded
|
|
3619
|
+
* to the wrapped object. A descriptor without __set__ is stored
|
|
3620
|
+
* against the instance as generic attribute assignment would. */
|
|
3621
|
+
|
|
3622
|
+
PyObject *descriptor = wrapt_lookup_doc_descriptor(Py_TYPE(self), name);
|
|
3623
|
+
|
|
3624
|
+
if (descriptor)
|
|
3625
|
+
{
|
|
3626
|
+
int result;
|
|
3627
|
+
|
|
3628
|
+
if (Py_TYPE(descriptor)->tp_descr_set)
|
|
3629
|
+
result = (Py_TYPE(descriptor)->tp_descr_set)(descriptor,
|
|
3630
|
+
(PyObject *)self, value);
|
|
3631
|
+
else
|
|
3632
|
+
result = PyObject_GenericSetAttr((PyObject *)self, name, value);
|
|
3633
|
+
|
|
3634
|
+
Py_DECREF(descriptor);
|
|
3635
|
+
return result;
|
|
3636
|
+
}
|
|
3637
|
+
|
|
3530
3638
|
return WraptObjectProxy_set_doc(self, value);
|
|
3639
|
+
}
|
|
3531
3640
|
|
|
3532
3641
|
if (wrapt_type_has_attr(Py_TYPE(self), name))
|
|
3533
3642
|
return PyObject_GenericSetAttr((PyObject *)self, name, value);
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""The `with_doc` decorator: override the docstring that introspection tools
|
|
2
|
+
see for a wrapped callable without mutating the wrapped function itself.
|
|
3
|
+
Accepts the docstring directly, or a factory that derives it from the
|
|
4
|
+
wrapped function at decoration time.
|
|
5
|
+
|
|
6
|
+
This is the companion of `with_signature`, which overrides the signature in
|
|
7
|
+
the same way. Assigning to `__doc__` on an ordinary wrapt wrapper writes
|
|
8
|
+
through to the wrapped function, because `__doc__` on every proxy delegates
|
|
9
|
+
to the wrapped object, so a wrapper carrying its own docstring is the only
|
|
10
|
+
way to change what is reported without touching the wrapped function.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .__wrapt__ import BoundFunctionWrapper, FunctionWrapper
|
|
14
|
+
|
|
15
|
+
# Marker for a wrapper which carries no docstring override, so that `__doc__`
|
|
16
|
+
# keeps delegating to the wrapped function. Distinct from None, which is a
|
|
17
|
+
# legitimate docstring override.
|
|
18
|
+
|
|
19
|
+
_NO_DOC_OVERRIDE = object()
|
|
20
|
+
|
|
21
|
+
# The `__doc__` property for a function wrapper which may carry a docstring
|
|
22
|
+
# override in its `_self_doc` slot. The wrapper class must assign this to
|
|
23
|
+
# `__doc__` in its own class body, rather than inherit it from a mixin, and
|
|
24
|
+
# set `_self_doc` in its `__init__()`, to `_NO_DOC_OVERRIDE` when there is
|
|
25
|
+
# none.
|
|
26
|
+
#
|
|
27
|
+
# Both the pure Python metaclass and the C extension attribute hooks
|
|
28
|
+
# recognise a `__doc__` descriptor defined by a derived class and consult it
|
|
29
|
+
# in place of the default delegation to the wrapped object. It has to be in
|
|
30
|
+
# the class dictionary of the wrapper class itself though, because `pydoc`
|
|
31
|
+
# reads `__doc__` using `object.__getattribute__()` to avoid inherited
|
|
32
|
+
# docstrings, and with the C extension that bypasses the attribute hooks and
|
|
33
|
+
# would otherwise find the copy of the wrapped function's docstring which is
|
|
34
|
+
# held in the instance dictionary.
|
|
35
|
+
#
|
|
36
|
+
# Without an override, assignment and deletion write through to the wrapped
|
|
37
|
+
# function as they do for any other wrapt wrapper. With one, assignment
|
|
38
|
+
# replaces the override and deletion removes it, restoring delegation to the
|
|
39
|
+
# wrapped function.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _get_doc(self):
|
|
43
|
+
doc = self._self_doc
|
|
44
|
+
if doc is _NO_DOC_OVERRIDE:
|
|
45
|
+
return self.__wrapped__.__doc__
|
|
46
|
+
return doc
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _set_doc(self, value):
|
|
50
|
+
if self._self_doc is _NO_DOC_OVERRIDE:
|
|
51
|
+
self.__wrapped__.__doc__ = value
|
|
52
|
+
else:
|
|
53
|
+
self._self_doc = value
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _del_doc(self):
|
|
57
|
+
if self._self_doc is _NO_DOC_OVERRIDE:
|
|
58
|
+
del self.__wrapped__.__doc__
|
|
59
|
+
else:
|
|
60
|
+
self._self_doc = _NO_DOC_OVERRIDE
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
_DOC_PROPERTY = property(_get_doc, _set_doc, _del_doc)
|
|
64
|
+
|
|
65
|
+
# The `__doc__` property for the bound wrapper of a function wrapper which
|
|
66
|
+
# may carry a docstring override, reading it from the parent. A bound
|
|
67
|
+
# wrapper is read only, as `__doc__` of a bound method is.
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _get_bound_doc(self):
|
|
71
|
+
doc = self._self_parent._self_doc
|
|
72
|
+
if doc is _NO_DOC_OVERRIDE:
|
|
73
|
+
return self.__wrapped__.__doc__
|
|
74
|
+
return doc
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
_BOUND_DOC_PROPERTY = property(_get_bound_doc)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class _BoundDocFunctionWrapper(BoundFunctionWrapper):
|
|
81
|
+
__doc__ = _BOUND_DOC_PROPERTY
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class _DocFunctionWrapper(FunctionWrapper):
|
|
85
|
+
__doc__ = _DOC_PROPERTY
|
|
86
|
+
|
|
87
|
+
__bound_function_wrapper__ = _BoundDocFunctionWrapper
|
|
88
|
+
|
|
89
|
+
def __init__(self, wrapped, wrapper, doc):
|
|
90
|
+
super().__init__(wrapped, wrapper)
|
|
91
|
+
self._self_doc = doc
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def with_doc(wrapped=None, /, *, doc=None, factory=None):
|
|
95
|
+
"""Override the docstring of a wrapped callable.
|
|
96
|
+
|
|
97
|
+
Exactly one of `doc` or `factory` must be supplied:
|
|
98
|
+
|
|
99
|
+
- `doc`: the docstring to report.
|
|
100
|
+
- `factory`: a callable `factory(wrapped)` invoked at decoration time
|
|
101
|
+
that returns the docstring to report.
|
|
102
|
+
|
|
103
|
+
The resulting wrapper reports the override as its `__doc__`, and so it
|
|
104
|
+
is what `help()` and `pydoc` display. The wrapped function is not
|
|
105
|
+
mutated, and assigning to `__doc__` on the wrapper replaces the override
|
|
106
|
+
rather than writing through to the wrapped function. Calling behaviour
|
|
107
|
+
is unchanged.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
if doc is None and factory is None:
|
|
111
|
+
raise TypeError("with_doc requires one of doc= or factory=")
|
|
112
|
+
if doc is not None and factory is not None:
|
|
113
|
+
raise TypeError("with_doc accepts only one of doc= or factory=")
|
|
114
|
+
|
|
115
|
+
def _decorator(wrapped):
|
|
116
|
+
def _wrapper(wrapped, instance, args, kwargs):
|
|
117
|
+
return wrapped(*args, **kwargs)
|
|
118
|
+
|
|
119
|
+
if factory is not None:
|
|
120
|
+
resolved = factory(wrapped)
|
|
121
|
+
else:
|
|
122
|
+
resolved = doc
|
|
123
|
+
|
|
124
|
+
return _DocFunctionWrapper(wrapped, _wrapper, resolved)
|
|
125
|
+
|
|
126
|
+
if wrapped is None:
|
|
127
|
+
return _decorator
|
|
128
|
+
return _decorator(wrapped)
|
|
@@ -22,9 +22,6 @@ class ObjectProxy(BaseObjectProxy):
|
|
|
22
22
|
def __object_proxy__(self):
|
|
23
23
|
return ObjectProxy
|
|
24
24
|
|
|
25
|
-
def __new__(cls, *args, **kwargs):
|
|
26
|
-
return super().__new__(cls)
|
|
27
|
-
|
|
28
25
|
def __iter__(self):
|
|
29
26
|
return iter(self.__wrapped__)
|
|
30
27
|
|
|
@@ -96,9 +93,10 @@ class AutoObjectProxy(BaseObjectProxy):
|
|
|
96
93
|
compatibility then use `ObjectProxy` instead.
|
|
97
94
|
"""
|
|
98
95
|
|
|
99
|
-
def __new__(cls, wrapped):
|
|
96
|
+
def __new__(cls, wrapped, *args, **kwargs):
|
|
100
97
|
"""Injects special dunder methods into a dynamically created subclass
|
|
101
|
-
as needed based on the wrapped object.
|
|
98
|
+
as needed based on the wrapped object. Any further arguments are
|
|
99
|
+
those a derived class has added to `__init__()` and are ignored.
|
|
102
100
|
"""
|
|
103
101
|
|
|
104
102
|
namespace = {}
|
|
@@ -242,9 +240,11 @@ class LazyObjectProxy(AutoObjectProxy):
|
|
|
242
240
|
when it is first needed.
|
|
243
241
|
"""
|
|
244
242
|
|
|
245
|
-
def __new__(cls, callback=None,
|
|
243
|
+
def __new__(cls, callback=None, *args, interface=..., **kwargs):
|
|
246
244
|
"""Injects special dunder methods into a dynamically created subclass
|
|
247
|
-
as needed based on the wrapped object.
|
|
245
|
+
as needed based on the declared interface of the wrapped object. Any
|
|
246
|
+
further arguments are those a derived class has added to `__init__()`
|
|
247
|
+
and are ignored.
|
|
248
248
|
"""
|
|
249
249
|
|
|
250
250
|
if interface is ...:
|
|
@@ -17,6 +17,7 @@ from .__wrapt__ import (
|
|
|
17
17
|
CallableObjectProxy,
|
|
18
18
|
FunctionWrapper,
|
|
19
19
|
)
|
|
20
|
+
from .doc import _BOUND_DOC_PROPERTY, _DOC_PROPERTY, _NO_DOC_OVERRIDE
|
|
20
21
|
|
|
21
22
|
_MISSING = object()
|
|
22
23
|
|
|
@@ -178,6 +179,8 @@ class _SignatureFunctionSurrogate(_SignatureMixin, CallableObjectProxy):
|
|
|
178
179
|
|
|
179
180
|
|
|
180
181
|
class _BoundSignatureFunctionWrapper(BoundFunctionWrapper):
|
|
182
|
+
__doc__ = _BOUND_DOC_PROPERTY
|
|
183
|
+
|
|
181
184
|
@property
|
|
182
185
|
def __func__(self):
|
|
183
186
|
return _SignatureFunctionSurrogate(
|
|
@@ -190,18 +193,23 @@ class _BoundSignatureFunctionWrapper(BoundFunctionWrapper):
|
|
|
190
193
|
|
|
191
194
|
|
|
192
195
|
class _SignatureFunctionWrapper(_SignatureMixin, FunctionWrapper):
|
|
196
|
+
__doc__ = _DOC_PROPERTY
|
|
197
|
+
|
|
193
198
|
__bound_function_wrapper__ = _BoundSignatureFunctionWrapper
|
|
194
199
|
|
|
195
|
-
def __init__(self, wrapped, wrapper, signature):
|
|
200
|
+
def __init__(self, wrapped, wrapper, signature, doc=_NO_DOC_OVERRIDE):
|
|
196
201
|
super().__init__(wrapped, wrapper)
|
|
197
202
|
self._self_signature = signature
|
|
203
|
+
self._self_doc = doc
|
|
198
204
|
|
|
199
205
|
@property
|
|
200
206
|
def __func__(self):
|
|
201
207
|
return _SignatureFunctionSurrogate(self.__wrapped__, self._self_signature)
|
|
202
208
|
|
|
203
209
|
|
|
204
|
-
def with_signature(
|
|
210
|
+
def with_signature(
|
|
211
|
+
wrapped=None, /, *, prototype=None, signature=None, factory=None, doc=None
|
|
212
|
+
):
|
|
205
213
|
"""Override the signature of a wrapped callable.
|
|
206
214
|
|
|
207
215
|
Exactly one of `prototype`, `signature`, or `factory` must be supplied:
|
|
@@ -209,7 +217,13 @@ def with_signature(wrapped=None, /, *, prototype=None, signature=None, factory=N
|
|
|
209
217
|
- `prototype`: a callable whose signature will be used.
|
|
210
218
|
- `signature`: a prebuilt `inspect.Signature` object.
|
|
211
219
|
- `factory`: a callable `factory(wrapped)` invoked at decoration time
|
|
212
|
-
that returns either a `Signature` or a prototype callable
|
|
220
|
+
that returns either a `Signature` or a prototype callable, or a
|
|
221
|
+
tuple of `(signature_or_prototype, docstring)` to override the
|
|
222
|
+
docstring at the same time.
|
|
223
|
+
|
|
224
|
+
The docstring may also be overridden by supplying `doc`, in which case
|
|
225
|
+
a docstring from the factory is ignored. When neither is supplied the
|
|
226
|
+
wrapper reports the docstring of the wrapped function as normal.
|
|
213
227
|
|
|
214
228
|
The resulting wrapper exposes the override via `__signature__`, and
|
|
215
229
|
derives `__annotations__`, `__defaults__`, `__kwdefaults__`, and the
|
|
@@ -231,19 +245,25 @@ def with_signature(wrapped=None, /, *, prototype=None, signature=None, factory=N
|
|
|
231
245
|
def _wrapper(wrapped, instance, args, kwargs):
|
|
232
246
|
return wrapped(*args, **kwargs)
|
|
233
247
|
|
|
248
|
+
resolved_doc = _NO_DOC_OVERRIDE if doc is None else doc
|
|
249
|
+
|
|
234
250
|
if signature is not None:
|
|
235
251
|
resolved = signature
|
|
236
252
|
elif prototype is not None:
|
|
237
253
|
resolved = _inspect_signature(prototype)
|
|
238
254
|
else:
|
|
239
255
|
produced = factory(wrapped)
|
|
256
|
+
if isinstance(produced, tuple):
|
|
257
|
+
produced, produced_doc = produced
|
|
258
|
+
if doc is None:
|
|
259
|
+
resolved_doc = produced_doc
|
|
240
260
|
resolved = (
|
|
241
261
|
produced
|
|
242
262
|
if isinstance(produced, Signature)
|
|
243
263
|
else _inspect_signature(produced)
|
|
244
264
|
)
|
|
245
265
|
|
|
246
|
-
return _SignatureFunctionWrapper(wrapped, _wrapper, resolved)
|
|
266
|
+
return _SignatureFunctionWrapper(wrapped, _wrapper, resolved, resolved_doc)
|
|
247
267
|
|
|
248
268
|
if wrapped is None:
|
|
249
269
|
return _decorator
|
|
@@ -13,6 +13,14 @@ from .__wrapt__ import BaseObjectProxy, _FunctionWrapperBase
|
|
|
13
13
|
# reference is therefore applied to the instance the method is bound to
|
|
14
14
|
# and the original function. The function is then rebound at the point
|
|
15
15
|
# of a call via the weak function proxy.
|
|
16
|
+
#
|
|
17
|
+
# Where the function is a wrapt function wrapper, such as results from
|
|
18
|
+
# applying a decorator, the same applies but there may be no instance,
|
|
19
|
+
# either because the wrapper was never bound, as for a decorated free
|
|
20
|
+
# function, or because the method was accessed via the class rather
|
|
21
|
+
# than an instance. In that case a weak reference to the class the
|
|
22
|
+
# wrapper was accessed through, the owner, is retained as well so that
|
|
23
|
+
# the function can still be rebound correctly at the point of a call.
|
|
16
24
|
|
|
17
25
|
|
|
18
26
|
def _weak_function_proxy_callback(ref, proxy, callback):
|
|
@@ -62,9 +70,27 @@ class WeakFunctionProxy(BaseObjectProxy):
|
|
|
62
70
|
)
|
|
63
71
|
|
|
64
72
|
self._self_expired = False
|
|
73
|
+
self._self_owner = None
|
|
65
74
|
|
|
66
75
|
if isinstance(wrapped, _FunctionWrapperBase):
|
|
67
|
-
|
|
76
|
+
# A function wrapper may have no instance, either because it
|
|
77
|
+
# was never bound, as for a decorated free function, or
|
|
78
|
+
# because the method was accessed via the class rather than
|
|
79
|
+
# an instance. Only take a weak reference to the instance
|
|
80
|
+
# where there is one. The owner is the class the wrapper was
|
|
81
|
+
# accessed through, and is retained so the function can be
|
|
82
|
+
# rebound with it when called. Without it a classmethod
|
|
83
|
+
# accessed via the class could not be rebound at all, and an
|
|
84
|
+
# instance method accessed via the class would not be able
|
|
85
|
+
# to identify an instance passed as the first argument.
|
|
86
|
+
|
|
87
|
+
instance = wrapped._self_instance
|
|
88
|
+
self._self_instance = (
|
|
89
|
+
weakref.ref(instance, _callback) if instance is not None else None
|
|
90
|
+
)
|
|
91
|
+
owner = wrapped._self_owner
|
|
92
|
+
if owner is not None:
|
|
93
|
+
self._self_owner = weakref.ref(owner, _callback)
|
|
68
94
|
|
|
69
95
|
if wrapped._self_parent is not None:
|
|
70
96
|
# Explicit class in super() is used because the proxy
|
|
@@ -114,10 +140,24 @@ class WeakFunctionProxy(BaseObjectProxy):
|
|
|
114
140
|
if self._self_instance is not None and instance is None:
|
|
115
141
|
raise ReferenceError("weakly-referenced object no longer exists")
|
|
116
142
|
|
|
117
|
-
# If the wrapped function was
|
|
118
|
-
#
|
|
119
|
-
#
|
|
120
|
-
#
|
|
143
|
+
# If the wrapped function was a function wrapper for which the
|
|
144
|
+
# owner was retained, rebind the function against the instance,
|
|
145
|
+
# which may be None, and that owner. This is what a classmethod
|
|
146
|
+
# accessed via the class needs to be rebound at all, and what an
|
|
147
|
+
# instance method accessed via the class needs to recognise an
|
|
148
|
+
# instance passed as the first argument. If the owner has been
|
|
149
|
+
# garbage collected, raise a ReferenceError as for the instance.
|
|
150
|
+
|
|
151
|
+
if self._self_owner is not None:
|
|
152
|
+
owner = self._self_owner()
|
|
153
|
+
if owner is None:
|
|
154
|
+
raise ReferenceError("weakly-referenced object no longer exists")
|
|
155
|
+
return function.__get__(instance, owner)(*args, **kwargs)
|
|
156
|
+
|
|
157
|
+
# Otherwise, if the wrapped function was originally a bound
|
|
158
|
+
# function, for which we retained a reference to the instance and
|
|
159
|
+
# the unbound function, we need to rebind the function and then
|
|
160
|
+
# call it. If not just call the wrapped function.
|
|
121
161
|
|
|
122
162
|
if instance is None:
|
|
123
163
|
return self.__wrapped__(*args, **kwargs)
|