Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 99e20c6

Browse filesBrowse files
Automate signature computation for builtin functions
This improves the compatibility with the inspect module.
1 parent 325b72a commit 99e20c6
Copy full SHA for 99e20c6

File tree

Expand file treeCollapse file tree

3 files changed

+31
-49
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+31
-49
lines changed

‎numpy/core/multiarray.py

Copy file name to clipboardExpand all lines: numpy/core/multiarray.py
-46Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@
7878
@array_function_from_c_func_and_dispatcher(_multiarray_umath.empty_like)
7979
def empty_like(prototype, dtype=None, order=None, subok=None, shape=None):
8080
"""
81-
empty_like(prototype, dtype=None, order='K', subok=True, shape=None)
82-
8381
Return a new array with the same shape and type as a given array.
8482
8583
Parameters
@@ -146,8 +144,6 @@ def empty_like(prototype, dtype=None, order=None, subok=None, shape=None):
146144
@array_function_from_c_func_and_dispatcher(_multiarray_umath.concatenate)
147145
def concatenate(arrays, axis=None, out=None, *, dtype=None, casting=None):
148146
"""
149-
concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
150-
151147
Join a sequence of arrays along an existing axis.
152148
153149
Parameters
@@ -247,8 +243,6 @@ def concatenate(arrays, axis=None, out=None, *, dtype=None, casting=None):
247243
@array_function_from_c_func_and_dispatcher(_multiarray_umath.inner)
248244
def inner(a, b):
249245
"""
250-
inner(a, b, /)
251-
252246
Inner product of two arrays.
253247
254248
Ordinary inner product of vectors for 1-D arrays (without complex
@@ -339,8 +333,6 @@ def inner(a, b):
339333
@array_function_from_c_func_and_dispatcher(_multiarray_umath.where)
340334
def where(condition, x=None, y=None):
341335
"""
342-
where(condition, [x, y], /)
343-
344336
Return elements chosen from `x` or `y` depending on `condition`.
345337
346338
.. note::
@@ -414,8 +406,6 @@ def where(condition, x=None, y=None):
414406
@array_function_from_c_func_and_dispatcher(_multiarray_umath.lexsort)
415407
def lexsort(keys, axis=None):
416408
"""
417-
lexsort(keys, axis=-1)
418-
419409
Perform an indirect stable sort using a sequence of keys.
420410
421411
Given multiple sorting keys, which can be interpreted as columns in a
@@ -496,8 +486,6 @@ def lexsort(keys, axis=None):
496486
@array_function_from_c_func_and_dispatcher(_multiarray_umath.can_cast)
497487
def can_cast(from_, to, casting=None):
498488
"""
499-
can_cast(from_, to, casting='safe')
500-
501489
Returns True if cast between data types can occur according to the
502490
casting rule. If from is a scalar or array scalar, also returns
503491
True if the scalar value can be cast without overflow or truncation
@@ -611,8 +599,6 @@ def can_cast(from_, to, casting=None):
611599
@array_function_from_c_func_and_dispatcher(_multiarray_umath.min_scalar_type)
612600
def min_scalar_type(a):
613601
"""
614-
min_scalar_type(a, /)
615-
616602
For scalar ``a``, returns the data type with the smallest size
617603
and smallest scalar kind which can hold its value. For non-scalar
618604
array ``a``, returns the vector's dtype unmodified.
@@ -662,8 +648,6 @@ def min_scalar_type(a):
662648
@array_function_from_c_func_and_dispatcher(_multiarray_umath.result_type)
663649
def result_type(*arrays_and_dtypes):
664650
"""
665-
result_type(*arrays_and_dtypes)
666-
667651
Returns the type that results from applying the NumPy
668652
type promotion rules to the arguments.
669653
@@ -734,8 +718,6 @@ def result_type(*arrays_and_dtypes):
734718
@array_function_from_c_func_and_dispatcher(_multiarray_umath.dot)
735719
def dot(a, b, out=None):
736720
"""
737-
dot(a, b, out=None)
738-
739721
Dot product of two arrays. Specifically,
740722
741723
- If both `a` and `b` are 1-D arrays, it is inner product of vectors
@@ -823,8 +805,6 @@ def dot(a, b, out=None):
823805
@array_function_from_c_func_and_dispatcher(_multiarray_umath.vdot)
824806
def vdot(a, b):
825807
"""
826-
vdot(a, b, /)
827-
828808
Return the dot product of two vectors.
829809
830810
The vdot(`a`, `b`) function handles complex numbers differently than
@@ -881,8 +861,6 @@ def vdot(a, b):
881861
@array_function_from_c_func_and_dispatcher(_multiarray_umath.bincount)
882862
def bincount(x, weights=None, minlength=None):
883863
"""
884-
bincount(x, /, weights=None, minlength=0)
885-
886864
Count number of occurrences of each value in array of non-negative ints.
887865
888866
The number of bins (of size 1) is one larger than the largest value in
@@ -958,8 +936,6 @@ def bincount(x, weights=None, minlength=None):
958936
@array_function_from_c_func_and_dispatcher(_multiarray_umath.ravel_multi_index)
959937
def ravel_multi_index(multi_index, dims, mode=None, order=None):
960938
"""
961-
ravel_multi_index(multi_index, dims, mode='raise', order='C')
962-
963939
Converts a tuple of index arrays into an array of flat
964940
indices, applying boundary modes to the multi-index.
965941
@@ -1019,8 +995,6 @@ def ravel_multi_index(multi_index, dims, mode=None, order=None):
1019995
@array_function_from_c_func_and_dispatcher(_multiarray_umath.unravel_index)
1020996
def unravel_index(indices, shape=None, order=None):
1021997
"""
1022-
unravel_index(indices, shape, order='C')
1023-
1024998
Converts a flat index or array of flat indices into a tuple
1025999
of coordinate arrays.
10261000
@@ -1069,8 +1043,6 @@ def unravel_index(indices, shape=None, order=None):
10691043
@array_function_from_c_func_and_dispatcher(_multiarray_umath.copyto)
10701044
def copyto(dst, src, casting=None, where=None):
10711045
"""
1072-
copyto(dst, src, casting='same_kind', where=True)
1073-
10741046
Copies values from one array to another, broadcasting as necessary.
10751047
10761048
Raises a TypeError if the `casting` rule is violated, and if
@@ -1104,8 +1076,6 @@ def copyto(dst, src, casting=None, where=None):
11041076
@array_function_from_c_func_and_dispatcher(_multiarray_umath.putmask)
11051077
def putmask(a, mask, values):
11061078
"""
1107-
putmask(a, mask, values)
1108-
11091079
Changes elements of an array based on conditional and input values.
11101080
11111081
Sets ``a.flat[n] = values[n]`` for each n where ``mask.flat[n]==True``.
@@ -1149,8 +1119,6 @@ def putmask(a, mask, values):
11491119
@array_function_from_c_func_and_dispatcher(_multiarray_umath.packbits)
11501120
def packbits(a, axis=None, bitorder='big'):
11511121
"""
1152-
packbits(a, /, axis=None, bitorder='big')
1153-
11541122
Packs the elements of a binary-valued array into bits in a uint8 array.
11551123
11561124
The result is padded to full bytes by inserting zero bits at the end.
@@ -1207,8 +1175,6 @@ def packbits(a, axis=None, bitorder='big'):
12071175
@array_function_from_c_func_and_dispatcher(_multiarray_umath.unpackbits)
12081176
def unpackbits(a, axis=None, count=None, bitorder='big'):
12091177
"""
1210-
unpackbits(a, /, axis=None, count=None, bitorder='big')
1211-
12121178
Unpacks elements of a uint8 array into a binary-valued output array.
12131179
12141180
Each element of `a` represents a bit-field that should be unpacked
@@ -1291,8 +1257,6 @@ def unpackbits(a, axis=None, count=None, bitorder='big'):
12911257
@array_function_from_c_func_and_dispatcher(_multiarray_umath.shares_memory)
12921258
def shares_memory(a, b, max_work=None):
12931259
"""
1294-
shares_memory(a, b, /, max_work=None)
1295-
12961260
Determine if two arrays share memory.
12971261
12981262
.. warning::
@@ -1366,8 +1330,6 @@ def shares_memory(a, b, max_work=None):
13661330
@array_function_from_c_func_and_dispatcher(_multiarray_umath.may_share_memory)
13671331
def may_share_memory(a, b, max_work=None):
13681332
"""
1369-
may_share_memory(a, b, /, max_work=None)
1370-
13711333
Determine if two arrays might share memory
13721334
13731335
A return of True does not necessarily mean that the two arrays
@@ -1407,8 +1369,6 @@ def may_share_memory(a, b, max_work=None):
14071369
@array_function_from_c_func_and_dispatcher(_multiarray_umath.is_busday)
14081370
def is_busday(dates, weekmask=None, holidays=None, busdaycal=None, out=None):
14091371
"""
1410-
is_busday(dates, weekmask='1111100', holidays=None, busdaycal=None, out=None)
1411-
14121372
Calculates which of the given dates are valid days, and which are not.
14131373
14141374
.. versionadded:: 1.7.0
@@ -1462,8 +1422,6 @@ def is_busday(dates, weekmask=None, holidays=None, busdaycal=None, out=None):
14621422
def busday_offset(dates, offsets, roll=None, weekmask=None, holidays=None,
14631423
busdaycal=None, out=None):
14641424
"""
1465-
busday_offset(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None)
1466-
14671425
First adjusts the date to fall on a valid day according to
14681426
the ``roll`` rule, then applies offsets to the given dates
14691427
counted in valid days.
@@ -1556,8 +1514,6 @@ def busday_offset(dates, offsets, roll=None, weekmask=None, holidays=None,
15561514
def busday_count(begindates, enddates, weekmask=None, holidays=None,
15571515
busdaycal=None, out=None):
15581516
"""
1559-
busday_count(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None)
1560-
15611517
Counts the number of valid days between `begindates` and
15621518
`enddates`, not including the day of `enddates`.
15631519
@@ -1624,8 +1580,6 @@ def busday_count(begindates, enddates, weekmask=None, holidays=None,
16241580
_multiarray_umath.datetime_as_string)
16251581
def datetime_as_string(arr, unit=None, timezone=None, casting=None):
16261582
"""
1627-
datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind')
1628-
16291583
Convert an array of datetimes into an array of strings.
16301584
16311585
Parameters

‎numpy/core/overrides.py

Copy file name to clipboardExpand all lines: numpy/core/overrides.py
+15-3Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import collections
33
import functools
44
import os
5-
import textwrap
5+
import inspect
66

77
from numpy.core._multiarray_umath import (
88
add_docstring, implement_array_function, _get_implementing_args)
@@ -86,6 +86,18 @@ def set_array_function_like_doc(public_api):
8686
ArgSpec = collections.namedtuple('ArgSpec', 'args varargs keywords defaults')
8787

8888

89+
def add_docstring_from_dispatcher(implementation, dispatcher):
90+
"""Use dispatcher to populate doc and text signature of implementation"""
91+
doc = dispatcher.__doc__
92+
if doc is None:
93+
return
94+
add_docstring(implementation, '{}{}\n--\n\n{}'.format(
95+
dispatcher.__name__,
96+
inspect.signature(dispatcher),
97+
doc
98+
))
99+
100+
89101
def verify_matching_signatures(implementation, dispatcher):
90102
"""Verify that a dispatcher function has the right signature."""
91103
implementation_spec = ArgSpec(*getargspec(implementation))
@@ -162,7 +174,7 @@ def array_function_dispatch(dispatcher, module=None, verify=True,
162174
if not ARRAY_FUNCTION_ENABLED:
163175
def decorator(implementation):
164176
if docs_from_dispatcher:
165-
add_docstring(implementation, dispatcher.__doc__)
177+
add_docstring_from_dispatcher(implementation, dispatcher)
166178
if module is not None:
167179
implementation.__module__ = module
168180
return implementation
@@ -173,7 +185,7 @@ def decorator(implementation):
173185
verify_matching_signatures(implementation, dispatcher)
174186

175187
if docs_from_dispatcher:
176-
add_docstring(implementation, dispatcher.__doc__)
188+
add_docstring_from_dispatcher(implementation, dispatcher)
177189

178190
@functools.wraps(implementation)
179191
def public_api(*args, **kwargs):

‎numpy/tests/test_public_api.py

Copy file name to clipboardExpand all lines: numpy/tests/test_public_api.py
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import subprocess
33
import pkgutil
44
import types
5+
import inspect
56
import importlib
67
import warnings
78

@@ -459,3 +460,18 @@ def check_importable(module_name):
459460
raise AssertionError("Modules that are not really public but looked "
460461
"public and can not be imported: "
461462
"{}".format(module_names))
463+
464+
465+
@pytest.mark.skipif(sys.flags.optimize >= 2, reason='docstring stripped')
466+
def test_all_function_have_signature():
467+
allowlist = {'concatenate', 'where'}
468+
469+
for key, value in inspect.getmembers(numpy):
470+
if not inspect.isfunction(value):
471+
continue
472+
if key in allowlist:
473+
continue
474+
try:
475+
inspect.signature(value)
476+
except ValueError:
477+
raise AssertionError("No signature found for {}".format(key))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.