Skip to content
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

feat(types) Support for inline generics #5268

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from

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

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