Skip to content

Commit 5538a68

Browse files
committed
[stubgenc] Recognize pybind property return types
Pybind includes function name in the property signature in docstrings
1 parent 5e5baff commit 5538a68

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

mypy/stubdoc.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,19 @@ def infer_arg_sig_from_anon_docstring(docstr: str) -> List[ArgSig]:
248248
return []
249249

250250

251-
def infer_ret_type_sig_from_anon_docstring(docstr: str) -> Optional[str]:
252-
"""Convert signature in form of "(self: TestClass, arg0) -> int" to their return type."""
253-
ret = infer_sig_from_docstring("stub" + docstr.strip(), "stub")
251+
def infer_ret_type_sig_from_docstring(docstr: str, name: str) -> Optional[str]:
252+
"""Convert signature in form of "func(self: TestClass, arg0) -> int" to their return type."""
253+
ret = infer_sig_from_docstring(docstr, name)
254254
if ret:
255255
return ret[0].ret_type
256256
return None
257257

258258

259+
def infer_ret_type_sig_from_anon_docstring(docstr: str) -> Optional[str]:
260+
"""Convert signature in form of "(self: TestClass, arg0) -> int" to their return type."""
261+
return infer_ret_type_sig_from_docstring("stub" + docstr.strip(), "stub")
262+
263+
259264
def parse_signature(sig: str) -> Optional[Tuple[str,
260265
List[str],
261266
List[str]]]:

mypy/stubgenc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from mypy.moduleinspect import is_c_module
1515
from mypy.stubdoc import (
1616
infer_sig_from_docstring, infer_prop_type_from_docstring, ArgSig,
17-
infer_arg_sig_from_anon_docstring, infer_ret_type_sig_from_anon_docstring, FunctionSig
17+
infer_arg_sig_from_anon_docstring, infer_ret_type_sig_from_anon_docstring,
18+
infer_ret_type_sig_from_docstring, FunctionSig
1819
)
1920

2021
# Members of the typing module to consider for importing by default.
@@ -254,6 +255,8 @@ def infer_prop_type(docstr: Optional[str]) -> Optional[str]:
254255
"""Infer property type from docstring or docstring signature."""
255256
if docstr is not None:
256257
inferred = infer_ret_type_sig_from_anon_docstring(docstr)
258+
if not inferred:
259+
inferred = infer_ret_type_sig_from_docstring(docstr, name)
257260
if not inferred:
258261
inferred = infer_prop_type_from_docstring(docstr)
259262
return inferred

test-data/stubgen/pybind11_mypy_demo/basics.pyi

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Any
2-
31
from typing import overload
42
PI: float
53

@@ -17,11 +15,11 @@ class Point:
1715
def __ne__(self, other: object) -> bool: ...
1816
def __setstate__(self, state: int) -> None: ...
1917
@property
20-
def name(self) -> Any: ...
18+
def name(self) -> str: ...
2119
@property
22-
def __doc__(self) -> Any: ...
20+
def __doc__(self) -> str: ...
2321
@property
24-
def __members__(self) -> Any: ...
22+
def __members__(self) -> dict: ...
2523

2624
class LengthUnit:
2725
__entries: dict = ...
@@ -37,11 +35,11 @@ class Point:
3735
def __ne__(self, other: object) -> bool: ...
3836
def __setstate__(self, state: int) -> None: ...
3937
@property
40-
def name(self) -> Any: ...
38+
def name(self) -> str: ...
4139
@property
42-
def __doc__(self) -> Any: ...
40+
def __doc__(self) -> str: ...
4341
@property
44-
def __members__(self) -> Any: ...
42+
def __members__(self) -> dict: ...
4543
origin: Point = ...
4644
@overload
4745
def __init__(self) -> None: ...

0 commit comments

Comments
 (0)