Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d14632c

Browse files
committed
MAINT: Warn on the use of _add_newdocs.py to add docstrings to pure-python objects
This caught the duplication of docstrings between multiarray.py and _add_newdocs
1 parent 45c4a8d commit d14632c

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

numpy/core/function_base.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import functools
44
import warnings
55
import operator
6+
import types
67

78
from . import numeric as _nx
89
from .numeric import (result_type, NaN, shares_memory, MAY_SHARE_BOUNDS,
@@ -427,15 +428,39 @@ def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):
427428
return result.astype(dtype, copy=False)
428429

429430

430-
#always succeed
431-
def _add_docstring(obj, doc):
431+
def _needs_add_docstring(obj):
432+
"""
433+
Returns true if the only way to set the docstring of `obj` from python is
434+
via add_docstring.
435+
436+
This function errs on the side of being overly conservative.
437+
"""
438+
Py_TPFLAGS_HEAPTYPE = 1 << 9
439+
440+
if isinstance(obj, (types.FunctionType, types.MethodType, property)):
441+
return False
442+
443+
if isinstance(obj, type) and obj.__flags__ & Py_TPFLAGS_HEAPTYPE:
444+
return False
445+
446+
return True
447+
448+
449+
def _add_docstring(obj, doc, warn_on_python):
450+
if warn_on_python and not _needs_add_docstring(obj):
451+
warnings.warn(
452+
"add_newdoc was used on a pure-python object {}. "
453+
"Prefer to attach it directly to the source."
454+
.format(obj),
455+
UserWarning,
456+
stacklevel=3)
432457
try:
433458
add_docstring(obj, doc)
434459
except Exception:
435460
pass
436461

437462

438-
def add_newdoc(place, obj, doc):
463+
def add_newdoc(place, obj, doc, warn_on_python=True):
439464
"""
440465
Add documentation to an existing object, typically one defined in C
441466
@@ -457,6 +482,9 @@ def add_newdoc(place, obj, doc):
457482
458483
If a list, then each element of the list should be a tuple of length
459484
two - ``[(method1, docstring1), (method2, docstring2), ...]``
485+
warn_on_python : bool
486+
If True, the default, emit `UserWarning` if this is used to attach
487+
documentation to a pure-python object.
460488
461489
Notes
462490
-----
@@ -480,10 +508,10 @@ def add_newdoc(place, obj, doc):
480508
"""
481509
new = getattr(__import__(place, globals(), {}, [obj]), obj)
482510
if isinstance(doc, str):
483-
_add_docstring(new, doc.strip())
511+
_add_docstring(new, doc.strip(), warn_on_python)
484512
elif isinstance(doc, tuple):
485513
attr, docstring = doc
486-
_add_docstring(getattr(new, attr), docstring.strip())
514+
_add_docstring(getattr(new, attr), docstring.strip(), warn_on_python)
487515
elif isinstance(doc, list):
488516
for attr, docstring in doc:
489-
_add_docstring(getattr(new, attr), docstring.strip())
517+
_add_docstring(getattr(new, attr), docstring.strip(), warn_on_python)

0 commit comments

Comments
 (0)