Skip to content

Commit

Permalink
stubdoc: Fix crash on non-str docstring (#15623)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdanal committed Jul 8, 2023
1 parent 1592945 commit ebfea94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down

0 comments on commit ebfea94

Please sign in to comment.