Skip to content
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

_lib._util.getargspec_no_self is missing KEYWORD_ONLY support #11416

Closed
pvanmulbregt opened this issue Jan 25, 2020 · 0 comments · Fixed by #11455
Closed

_lib._util.getargspec_no_self is missing KEYWORD_ONLY support #11416

pvanmulbregt opened this issue Jan 25, 2020 · 0 comments · Fixed by #11455
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected
Milestone

Comments

@pvanmulbregt
Copy link
Contributor

_lib._util.getargspec_no_self() only checks for 3 of the 5 possible parameter kinds returned by Python's inspect.signature() : (VAR_POSITIONAL, POSITIONAL_OR_KEYWORD, VAR_KEYWORD), and ignores the other two (POSITIONAL_ONLY, KEYWORD_ONLY)

Reproducing code example:

import numpy as np, scipy, scipy._lib, scipy._lib._util, inspect                                                                                                                                                             
class rv_generic(object): 
     def _rvs(self, *args, size=None): 
         U = self._random_state.random_sample(size) 
         Y = self._ppf(U, *args) 
         return Y 

# Create an instance of rv_generic
rv_obj = rv_generic()  
# Now print the results of the 4 ways to get the arg specifications
# First SciPy's which is missing the 'size' argument
print(scipy._lib._util.getargspec_no_self(rv_obj._rvs))

# Now the 3 Python inspect approaches
# Python's inspect.signature returns the KEYWORD_ONLY arguments. 
sig = inspect.signature(rv_obj._rvs)                                                                                                                                                                    
print([[p.name, p.kind] for p in sig.parameters.values()])

# Python's inspect.getfullargspec returns information on all the arguments. 
print(inspect.getfullargspec(rv_obj._rvs))

# Python's inspect.getargspec raises an Error as there are KEYWORD_ONLY arguments, which are not handled by inspect.getargspec
print(inspect.getargspec(rv_obj._rvs))

The output is

ArgSpec(args=[], varargs='args', keywords=None, defaults=None)

[['args', <_ParameterKind.VAR_POSITIONAL: 2>],
 ['size', <_ParameterKind.KEYWORD_ONLY: 3>]]

FullArgSpec(args=['self'], varargs='args', varkw=None, defaults=None, kwonlyargs=['size'], kwonlydefaults={'size': None}, annotations={})

ValueError: Function has keyword-only parameters or annotations, use inspect.signature() API which can support them

Scipy/Numpy/Python version information:

1.5.0.dev0+4242f73 1.17.3 sys.version_info(major=3, minor=8, micro=0, releaselevel='final', serial=0)
@pvanmulbregt pvanmulbregt added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Jan 25, 2020
@ev-br ev-br added this to the 1.5.0 milestone Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected
Projects
None yet
2 participants