Skip to content

Commit 5221b2c

Browse files
Joseph-Edwardsjames-d-mitchell
authored andcommitted
CxxWrapper: Fix type hints
1 parent daf1332 commit 5221b2c

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

src/libsemigroups_pybind11/detail/cxx_wrapper.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919
from typing import Any, Callable
2020
from typing_extensions import Self
2121

22-
from _libsemigroups_pybind11 import ( # pylint: disable=no-name-in-module
23-
UNDEFINED as _UNDEFINED,
24-
)
25-
26-
pybind11_type = type(_UNDEFINED)
22+
# We would really like Pybind11Type to point to pybind11_builtins.pybind11_type.
23+
# However, this is not easily importable into python; see the discussions in
24+
# https://github.com/pybind/pybind11/issues/3945 and
25+
# https://github.com/pybind/cmake_example/pull/164.
26+
# A proposed workaround would be to do the following:
27+
# Pybind11Type = TypeVar('Pybind11Type', bound=type(Undefined))
28+
# but that isn't allowed, since function calls aren't permitted in TypeVar
29+
# expressions. Hence, the next best thing is to use the parent class of
30+
# pybind11_builtins.pybind11_type, which is simply type. Further runtime checks
31+
# can be added latter if required.
32+
Pybind11Type = type
2733

2834

2935
def to_cxx(x: Any) -> Any:
@@ -49,7 +55,7 @@ def to_py(x: Any, *args) -> Any:
4955
_CXX_WRAPPED_TYPE_TO_PY_TYPE = {}
5056

5157

52-
def register_cxx_wrapped_type(cxx_type: pybind11_type, py_type: type) -> None:
58+
def register_cxx_wrapped_type(cxx_type: Pybind11Type, py_type: type) -> None:
5359
"""
5460
Function for adding to the _CXX_WRAPPED_TYPE_TO_PY_TYPE dictionary.
5561
"""
@@ -181,7 +187,7 @@ def init_cxx_obj(self: Self, *args) -> None:
181187

182188

183189
# TODO proper annotations
184-
def wrap_cxx_mem_fn(cxx_mem_fn: pybind11_type) -> Callable:
190+
def wrap_cxx_mem_fn(cxx_mem_fn: Pybind11Type) -> Callable:
185191
"""
186192
This function creates a wrapper around the pybind11 c++ member function
187193
<cxx_mem_fn> that automatically wraps and unwraps CxxWrapper types, and
@@ -216,7 +222,7 @@ def cxx_mem_fn_wrapper(self, *args):
216222

217223

218224
# TODO proper annotations
219-
def wrap_cxx_free_fn(cxx_free_fn: pybind11_type) -> Callable:
225+
def wrap_cxx_free_fn(cxx_free_fn: Pybind11Type) -> Callable:
220226
"""
221227
This function creates a wrapper around the pybind11 c++ free function
222228
<cxx_free_fn> that automatically wraps and unwraps CxxWrapper types. The
@@ -231,7 +237,7 @@ def cxx_free_fn_wrapper(*args):
231237
return cxx_free_fn_wrapper
232238

233239

234-
def copy_cxx_mem_fns(cxx_class: pybind11_type, py_class: CxxWrapper) -> None:
240+
def copy_cxx_mem_fns(cxx_class: Pybind11Type, py_class: CxxWrapper) -> None:
235241
"""
236242
Copy all the non-special methods of *cxx_class* into methods of *py_class*
237243
that call the cxx member function on the _cxx_obj.

0 commit comments

Comments
 (0)