|
6 | 6 | """
|
7 | 7 |
|
8 | 8 | import collections
|
| 9 | +import contextlib |
9 | 10 | import importlib
|
| 11 | +import inspect |
10 | 12 | import sys
|
11 | 13 | import textwrap
|
12 | 14 | import types
|
@@ -673,4 +675,30 @@ def prod(x, /, *args, axis=None, **kwargs):
|
673 | 675 |
|
674 | 676 | mod.prod = prod
|
675 | 677 |
|
| 678 | + preface = [ |
| 679 | + "The following is the documentation for the corresponding " |
| 680 | + f"attribute of `{xp.__name__}`.", |
| 681 | + "The behavior on pint-wrapped arrays is the same for dimensionless " |
| 682 | + "quantities, and may differ for quantities with units.\n\n", |
| 683 | + ] |
| 684 | + preface = "\n".join(preface) |
| 685 | + for attribute in mod.__dict__: |
| 686 | + # Add documentation if it is not already present |
| 687 | + if getattr(mod, attribute).__doc__: |
| 688 | + continue |
| 689 | + |
| 690 | + xp_attr = getattr(xp, attribute, None) |
| 691 | + mod_attr = getattr(mod, attribute, None) |
| 692 | + if xp_attr is not None and mod_attr is not None: |
| 693 | + if hasattr(xp_attr, "__doc__"): |
| 694 | + with contextlib.suppress(AttributeError, TypeError): |
| 695 | + xp_doc = xp_attr.__doc__ |
| 696 | + getattr(mod, attribute).__doc__ = preface + xp_doc |
| 697 | + |
| 698 | + with contextlib.suppress(ValueError, TypeError): |
| 699 | + mod_attr.__signature__ = inspect.signature(xp_attr) |
| 700 | + |
| 701 | + with contextlib.suppress(AttributeError, TypeError): |
| 702 | + mod_attr.__name__ = xp_attr.__name__ |
| 703 | + |
676 | 704 | return mod
|
0 commit comments