Cython: C++: classes in classes, cimport not consistent

Created on 9 Aug 2019  路  1Comment  路  Source: cython/cython

Consider this minimal example:

foo.h

#pragma once

#include <iostream>

struct Foobar {
        struct LongName {
                void foo() {
                        std::cout << "foo" << std::endl;
                }
        };

        static LongName get() {
                return Foobar::LongName();
        }

};

foo.pxd

#cython: language_level=3

cdef extern from "foo.h":
    cdef cppclass Foobar:
        cppclass LongName:
            void foo()

        @staticmethod
        LongName get()

foo.pyx

from foo cimport Foobar

def my_func():
    cdef Foobar.LongName bla
    bla = Foobar.get()
    bla.foo()

This works so far. However, this does not work:

foo1.pyx

cimport foo

def my_func():
    cdef foo.Foobar.LongName bla
    bla = foo.Foobar.get()
    bla.foo()

foo2.pyx

from foo cimport Foobar
from foo cimport Foobar.LongName as LongName
# alternative, also not working:
# from foo.Foobar cimport LongName as LongName

def my_func():
    cdef LongName bla
    bla = Foobar.get()
    bla.foo()

While this works:

foo3.pyx

from foo cimport Foobar

ctypedef Foobar.LongName LongName

def my_func():
    cdef LongName bla
    bla = Foobar.get()
    bla.foo()

For me, this feels like C++ class in C++ class is not consistent. Is it possible to support also the long nested name (foo.Foobar.LongName)?

C++ Type Analysis defect good first issue

Most helpful comment

A cimport from a class cannot work (only from modules, as in Python), but the other examples look reasonable to me. PR welcome to make them work.

>All comments

A cimport from a class cannot work (only from modules, as in Python), but the other examples look reasonable to me. PR welcome to make them work.

Was this page helpful?
0 / 5 - 0 ratings