serge-sans-paille / pythran

Ahead of Time compiler for numeric kernels
https://pythran.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.01k stars 193 forks source link

default parameters vs export specifications #2056

Open nbecker opened 1 year ago

nbecker commented 1 year ago

I think there is an incompatibility between functions with default parameter values and pythran specifications. Here is an example:

#pythran export F(int, float, str)
def F(a=1, b=2.0, c='hello'):
    print ('a:', a, 'b:', b, 'c:', c)
from _default import F

In [23]: F()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[23], line 1
----> 1 F()

TypeError: Invalid call to pythranized function `F()'
Candidates are:

    - F(int, float, str)

In python, any combination of values could be specified or omitted: F(a=1) F(b=2.5) F(b=3.0, c='bye') There would appear to be no way to capture this in pythran_export specifications.

For now, my workaround is write python wrapper functions that interpret the passed parameters and defaults and then call the pythran function with all parameters passed.

def wrap_F (a=1, b=2.0, c='hello'):
    return F(a, b, c)

But this is not ideal

At least it would be better if somehow the wrapper function could be included in the same source file instead of needing a 2nd source file for the wrapper.

pdumon commented 1 year ago

optional args can be specified with a ? but that would not solve your case. Don't know about kwargs.

#pythran export F(int?, float?, str?)
def F(a=1, b=2.0, c='hello'):
    print ('a:', a, 'b:', b, 'c:', c)

can be called with e.g. F(5), F(3, 4.0) etc, but not with F(b=3.0).