Skip to content

Generates invalid C when omitting self in lambda #2967

@instance01

Description

@instance01

I'm using Cython version 0.29.7.

Part of the error message (full message attached: output.txt):

AA/bug.c:830:57: error: expected identifier or ‘(’ before ‘.’ token
 struct __pyx_obj_2AA_3bug___pyx_scope_struct____pyx_base.func;

Here's a quick example to reproduce, including a workaround.

# cython: language_level=3
cdef class FBase():
    def __init__(self):
        pass
        
    def __cinit__(self):
        pass
        
    cdef func(self, whatever):
        pass
        

cdef class Bug(FBase):
    def __init__(self):
        pass
        
    cdef check(self, x):
        return x
        
    cdef func(self, whatever):
        a = [1, 2, 3]
        min(a, key=lambda x: self.check(x))

        # This generates correct C, but doesn't work in Python:
        # min(a, key=lambda self, x: self.check(x))

        # Workaround:
        # import functools
        # min(a, key=functools.partial(lambda self, x: self.check(x), self))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions