From c5ed9d4b193e0c1563282aebcfad57fecb0b4969 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 31 Dec 2024 03:53:30 +0000 Subject: [PATCH] Fix module type hint (#5469) * Fix module type hint "module" is not a valid python value. The correct type hint for a module object is "types.ModuleType" which has existed since at least Python 2.6 * Added module type hint test * style: pre-commit fixes * Remove doc function * Fixed type hint --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/pybind11.h | 2 +- tests/test_modules.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 7fc7dbf723..4387c2754b 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1322,7 +1322,7 @@ PYBIND11_NAMESPACE_BEGIN(detail) template <> struct handle_type_name { - static constexpr auto name = const_name("module"); + static constexpr auto name = const_name("types.ModuleType"); }; PYBIND11_NAMESPACE_END(detail) diff --git a/tests/test_modules.py b/tests/test_modules.py index 436271a701..ad898be89a 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -81,6 +81,13 @@ def test_pydoc(): assert pydoc.text.docmodule(pybind11_tests) +def test_module_handle_type_name(): + assert ( + m.def_submodule.__doc__ + == "def_submodule(arg0: types.ModuleType, arg1: str) -> types.ModuleType\n" + ) + + def test_duplicate_registration(): """Registering two things with the same name"""