Skip to content

Commit ebfea94

Browse files
authored
stubdoc: Fix crash on non-str docstring (#15623)
1 parent 1592945 commit ebfea94

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

mypy/stubdoc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def infer_sig_from_docstring(docstr: str | None, name: str) -> list[FunctionSig]
254254
* docstr: docstring
255255
* name: name of function for which signatures are to be found
256256
"""
257-
if not docstr:
257+
if not (isinstance(docstr, str) and docstr):
258258
return None
259259

260260
state = DocStringParser(name)

mypy/test/teststubgen.py

+21
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,27 @@ def test(arg0: str) -> None:
12131213
assert_equal(output, ["def test(arg0: foo.bar.Action) -> other.Thing: ..."])
12141214
assert_equal(set(imports), {"import foo", "import other"})
12151215

1216+
def test_generate_c_function_no_crash_for_non_str_docstring(self) -> None:
1217+
def test(arg0: str) -> None:
1218+
...
1219+
1220+
test.__doc__ = property(lambda self: "test(arg0: str) -> None") # type: ignore[assignment]
1221+
1222+
output: list[str] = []
1223+
imports: list[str] = []
1224+
mod = ModuleType(self.__module__, "")
1225+
generate_c_function_stub(
1226+
mod,
1227+
"test",
1228+
test,
1229+
output=output,
1230+
imports=imports,
1231+
known_modules=[mod.__name__],
1232+
sig_generators=get_sig_generators(parse_options([])),
1233+
)
1234+
assert_equal(output, ["def test(*args, **kwargs) -> Any: ..."])
1235+
assert_equal(imports, [])
1236+
12161237
def test_generate_c_property_with_pybind11(self) -> None:
12171238
"""Signatures included by PyBind11 inside property.fget are read."""
12181239

0 commit comments

Comments
 (0)