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

keyword args appear as positional in method signature for cdef class #1864

Open
johnyf opened this Issue Sep 9, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@johnyf
Contributor

johnyf commented Sep 9, 2017

Using the compiler directive binding and a cdef class, inspect.signature and inspect.getfullargspec both return an incorrect signature for methods: keyword arguments are presented as if they are positional.

@scoder

This comment has been minimized.

Show comment
Hide comment
@scoder

scoder Sep 9, 2017

Contributor

Could you show an example?

Contributor

scoder commented Sep 9, 2017

Could you show an example?

@johnyf

This comment has been minimized.

Show comment
Hide comment
@johnyf

johnyf Sep 9, 2017

Contributor

Contents of the file foo.pyx:

# cython: binding=True


cdef class Foo(object):

    def wrong_kw_1(self, a, b=False):
        pass

    cpdef wrong_kw_2(self, a, b=False):
        pass


def correct_kw(self, u, v=True):
    pass


cpdef wrong_kw_3(self, u, v=True):
    pass

Run as follows:

import inspect
import pyximport
pyximport.install()

import foo


a = foo.Foo()

# wrong signature for `def` method
>>> inspect.signature(a.wrong_kw_1)
<Signature (a, b)>

# wrong signature for `cpdef` method too
>>> inspect.signature(a.wrong_kw_2)
<Signature (a, b)>

# correct signature for `def` function in module's namespace
>>> inspect.signature(foo.correct_kw)
<Signature (self, u, v=True)>

# wrong signature for `cpdef` function in module's namespace
>>> inspect.signature(hehe.correct_kw_too)
<Signature (self, u, v)>

The situation is a little worse than the OP reports: introspection yields wrong signatures for cpdef functions too, besides for methods.

python == 3.5.2rc1+
cython == 0.26.1
ipython == 6.1.0
Contributor

johnyf commented Sep 9, 2017

Contents of the file foo.pyx:

# cython: binding=True


cdef class Foo(object):

    def wrong_kw_1(self, a, b=False):
        pass

    cpdef wrong_kw_2(self, a, b=False):
        pass


def correct_kw(self, u, v=True):
    pass


cpdef wrong_kw_3(self, u, v=True):
    pass

Run as follows:

import inspect
import pyximport
pyximport.install()

import foo


a = foo.Foo()

# wrong signature for `def` method
>>> inspect.signature(a.wrong_kw_1)
<Signature (a, b)>

# wrong signature for `cpdef` method too
>>> inspect.signature(a.wrong_kw_2)
<Signature (a, b)>

# correct signature for `def` function in module's namespace
>>> inspect.signature(foo.correct_kw)
<Signature (self, u, v=True)>

# wrong signature for `cpdef` function in module's namespace
>>> inspect.signature(hehe.correct_kw_too)
<Signature (self, u, v)>

The situation is a little worse than the OP reports: introspection yields wrong signatures for cpdef functions too, besides for methods.

python == 3.5.2rc1+
cython == 0.26.1
ipython == 6.1.0
@cython-notifications

This comment has been minimized.

Show comment
Hide comment
@cython-notifications

cython-notifications Sep 10, 2017

cython-notifications commented Sep 10, 2017

@scoder

This comment has been minimized.

Show comment
Hide comment
@scoder

scoder Sep 10, 2017

Contributor

Yes, they must be missing from the DefNodeWrapper. Defaults of methods and cpdef functions are currently not tested, neither in py34_signature.pyx nor in cyfunction_defaults.pyx. Probably easy to fix. Pull request welcome.

Contributor

scoder commented Sep 10, 2017

Yes, they must be missing from the DefNodeWrapper. Defaults of methods and cpdef functions are currently not tested, neither in py34_signature.pyx nor in cyfunction_defaults.pyx. Probably easy to fix. Pull request welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment