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

Skip to content

BUG: Functions in numpy.random have no signature (in the sense of inspect.signature) #21908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
soegaard opened this issue Jul 2, 2022 · 4 comments · Fixed by #21975
Closed

Comments

@soegaard
Copy link

soegaard commented Jul 2, 2022

Describe the issue:

While writing bindings for Numpy I discovered that functions in numpy.random are
missing signatures (in the sense of inspect.signature).

My use case: I am using function signatures to automatically generate Racket bindings for large parts of Numpy.

The following output were generated by the script signature-random.py.

Functions where inspect.signature fails

Functions where inspect.signature works

__RandomState_ctor

Builtins (where inspect.signature fails)

beta
binomial
bytes
chisquare
choice
default_rng
dirichlet
exponential
f
gamma
geometric
get_state
gumbel
hypergeometric
laplace
logistic
lognormal
logseries
multinomial
multivariate_normal
negative_binomial
noncentral_chisquare
noncentral_f
normal
pareto
permutation
poisson
power
rand
randint
randn
random
random_integers
random_sample
ranf
rayleigh
sample
seed
set_state
shuffle
standard_cauchy
standard_exponential
standard_gamma
standard_normal
standard_t
triangular
uniform
vonmises
wald
weibull
zipf

Reproduce the code example:

import inspect
import numpy

###
### This script finds the functions in `numpy.random` that have no signatures.
###


# Attempt to get the signature for `f`.
# Return `False` when no signature is available.
def sign(f):
   try:
     return inspect.signature(f)
   except:
     return False

mod = numpy.random
 
print("Functions where inspect.signature fails")
print("---")
for x in dir(mod):
    f = getattr(mod,x)
    if inspect.isfunction(f):
       if not sign(f):
          print(x)

print("Functions where inspect.signature works")
print("---")
for x in dir(mod):
    f = getattr(mod,x)
    if inspect.isfunction(f):
        if sign(f):
            print(x)

print("")
print("Builtins (where inspect.signature fails)")
print("---")
for x in dir(mod):
    f = getattr(mod,x)
    if (not inspect.isfunction(f)) and inspect.isbuiltin(f):
       if not sign(f):
          print(x)

Error message:

No response

NumPy/Python version information:

import sys, numpy; print(numpy.version, sys.version)
1.22.4 3.10.4 (main, Apr 26 2022, 19:42:59) [Clang 13.1.6 (clang-1316.0.21.2)]

@bashtage
Copy link
Contributor

It works on python functions, and fails on Cython functions.

bashtage added a commit to bashtage/numpy that referenced this issue Jul 12, 2022
Add binding so that inspect will work correctly

closes numpy#21908
@bashtage
Copy link
Contributor

See Cython documentation on directives for informatio as to why these don't work.

@soegaard
Copy link
Author

See Cython documentation on directives for informatio as to why these don't work.

Do you mean before or after commit ENH: Add binding for random pyx files ?

@bashtage
Copy link
Contributor

They work after setting binding to True. Currently binding is not set so False. Setting them to true isn't totally straight forward since this option produces some unused functions which blocks CI.

bashtage added a commit to bashtage/numpy that referenced this issue Apr 18, 2023
Add binding so that inspect will work correctly

closes numpy#21908
bashtage added a commit to bashtage/numpy that referenced this issue Jun 23, 2023
Add binding so that inspect will work correctly

closes numpy#21908
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants