Skip to content

Commit 6628b39

Browse files
committed
[stubgenc] Shorten property return types (add necessary imports if needed)
1 parent 1416d42 commit 6628b39

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

mypy/stubgenc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,14 @@ def strip_or_import(typ: str, module: ModuleType, imports: List[str]) -> str:
234234
return stripped_type
235235

236236

237-
def generate_c_property_stub(name: str, obj: object, output: List[str], readonly: bool) -> None:
237+
def generate_c_property_stub(name: str, obj: object, output: List[str], readonly: bool,
238+
module: Optional[ModuleType] = None,
239+
imports: Optional[List[str]] = None) -> None:
238240
"""Generate property stub using introspection of 'obj'.
239241
240242
Try to infer type from docstring, append resulting lines to 'output'.
241243
"""
244+
242245
def infer_prop_type(docstr: Optional[str]) -> Optional[str]:
243246
"""Infer property type from docstring or docstring signature."""
244247
if docstr is not None:
@@ -256,6 +259,9 @@ def infer_prop_type(docstr: Optional[str]) -> Optional[str]:
256259
if not inferred:
257260
inferred = 'Any'
258261

262+
if module is not None and imports is not None:
263+
inferred = strip_or_import(inferred, module, imports)
264+
259265
output.append('@property')
260266
output.append('def {}(self) -> {}: ...'.format(name, inferred))
261267
if not readonly:
@@ -304,7 +310,8 @@ def generate_c_type_stub(module: ModuleType,
304310
class_sigs=class_sigs)
305311
elif is_c_property(value):
306312
done.add(attr)
307-
generate_c_property_stub(attr, value, properties, is_c_property_readonly(value))
313+
generate_c_property_stub(attr, value, properties, is_c_property_readonly(value),
314+
module=module, imports=imports)
308315

309316
variables = []
310317
for attr, value in items:

test-data/stubgen/pybind11_mypy_demo/basics.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@ class Point:
2020
@overload
2121
def distance_to(*args, **kwargs) -> Any: ...
2222
@property
23-
def angle_unit(self) -> pybind11_mypy_demo.basics.Point.AngleUnit: ...
23+
def angle_unit(self) -> Point.AngleUnit: ...
2424
@angle_unit.setter
25-
def angle_unit(self, val: pybind11_mypy_demo.basics.Point.AngleUnit) -> None: ...
25+
def angle_unit(self, val: Point.AngleUnit) -> None: ...
2626
@property
2727
def length(self) -> float: ...
2828
@property
29-
def length_unit(self) -> pybind11_mypy_demo.basics.Point.LengthUnit: ...
29+
def length_unit(self) -> Point.LengthUnit: ...
3030
@length_unit.setter
31-
def length_unit(self, val: pybind11_mypy_demo.basics.Point.LengthUnit) -> None: ...
31+
def length_unit(self, val: Point.LengthUnit) -> None: ...
3232
@property
3333
def x(self) -> float: ...
3434
@x.setter
3535
def x(self, val: float) -> None: ...
3636
@property
37-
def x_axis(self) -> pybind11_mypy_demo.basics.Point: ...
37+
def x_axis(self) -> Point: ...
3838
@property
3939
def y(self) -> float: ...
4040
@y.setter
4141
def y(self, val: float) -> None: ...
4242
@property
43-
def y_axis(self) -> pybind11_mypy_demo.basics.Point: ...
43+
def y_axis(self) -> Point: ...
4444

4545
def answer() -> int: ...
4646
def midpoint(left: float, right: float) -> float: ...

0 commit comments

Comments
 (0)