From ebfea94c9f1dc081f8f3f0de9e78a5c83ff8af40 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Sat, 8 Jul 2023 23:25:43 +0200 Subject: [PATCH] stubdoc: Fix crash on non-str docstring (#15623) --- mypy/stubdoc.py | 2 +- mypy/test/teststubgen.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/mypy/stubdoc.py b/mypy/stubdoc.py index 232d9e9762e9..145f57fd7751 100644 --- a/mypy/stubdoc.py +++ b/mypy/stubdoc.py @@ -254,7 +254,7 @@ def infer_sig_from_docstring(docstr: str | None, name: str) -> list[FunctionSig] * docstr: docstring * name: name of function for which signatures are to be found """ - if not docstr: + if not (isinstance(docstr, str) and docstr): return None state = DocStringParser(name) diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 884cf87ac5d8..79d380785a39 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -1213,6 +1213,27 @@ def test(arg0: str) -> None: assert_equal(output, ["def test(arg0: foo.bar.Action) -> other.Thing: ..."]) assert_equal(set(imports), {"import foo", "import other"}) + def test_generate_c_function_no_crash_for_non_str_docstring(self) -> None: + def test(arg0: str) -> None: + ... + + test.__doc__ = property(lambda self: "test(arg0: str) -> None") # type: ignore[assignment] + + output: list[str] = [] + imports: list[str] = [] + mod = ModuleType(self.__module__, "") + generate_c_function_stub( + mod, + "test", + test, + output=output, + imports=imports, + known_modules=[mod.__name__], + sig_generators=get_sig_generators(parse_options([])), + ) + assert_equal(output, ["def test(*args, **kwargs) -> Any: ..."]) + assert_equal(imports, []) + def test_generate_c_property_with_pybind11(self) -> None: """Signatures included by PyBind11 inside property.fget are read."""