Skip to content

Conversation

@InvincibleRMC
Copy link
Contributor

Description

#5167

Expands upon the initial TypeVars to generate inline generics by putting typevars, paramspecs, or typevartuples, in the atrribute __type_params__ where using the python3.12 inline generic syntax stores them. This should work for functions and classes. For more info on __type_params__ can be found here.

Normal Python3.12 syntax.

def foo[T](x: T) -> T:
      return x

Mocked by doing the following in pybind.

def foo(x: T) -> T:
      return x
T = TypeVar('T')
foo.__type_params__ = T

Suggested changelog entry:

Adds support for generating inline generics with stubgen with `TypeVar`, `ParamSpec`, and `TypeVarTuple`

@InvincibleRMC
Copy link
Contributor Author

Currently the following classes are seg faulting due to some error in their construction. Any ideas on how to properly create such classes? I tried some stuff with inheriting the constructor from object() but got nowhere. Another solution could be to replace the class with a function which just returns a python object with the attributes set but, I also got stuck with that idea. I'm sure both of these solutions are likely possible but, I'm not sure what I'm missing.

class ParamSpec : public object {
    PYBIND11_OBJECT_DEFAULT(ParamSpec, object, PyObject_Type)
    using object::object;
    ParamSpec(const char *name) {
        attr("__name__") = name;
        attr("__bound__") = pybind11::none();
    }
};

class TypeVarTuple : public object {
    PYBIND11_OBJECT_DEFAULT(TypeVarTuple, object, PyObject_Type)
    using object::object;
    TypeVarTuple(const char *name) { attr("__name__") = name; }
};

Error attached

[ 37%] Linking CXX shared module pybind11_tests.cpython-312-x86_64-linux-gnu.so
lto-wrapper: warning: using serial compilation of 6 LTRANS jobs
lto-wrapper: note: see the ‘-flto’ option documentation for more information
------ pybind11_tests.cpython-312-x86_64-linux-gnu.so file size: 393264 (no change)
[ 37%] Built target pybind11_tests
Segmentation fault (core dumped)
gmake[3]: *** [tests/CMakeFiles/pytest.dir/build.make:70: tests/CMakeFiles/pytest] Error 139
gmake[2]: *** [CMakeFiles/Makefile2:269: tests/CMakeFiles/pytest.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:330: tests/CMakeFiles/check.dir/rule] Error 2
gmake: *** [Makefile:222: check] Error 2

@InvincibleRMC InvincibleRMC deleted the inline-generics branch February 5, 2025 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant