Skip to content

Commit

Permalink
[stubgenc] Render rw-properties as annotated class attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sizmailov committed Jan 20, 2021
1 parent 45cee68 commit 951fc37
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
24 changes: 14 additions & 10 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def is_static_property(obj: object) -> bool:

def generate_c_property_stub(name: str, obj: object,
static_properties: List[str],
properties: List[str], readonly: bool,
rw_properties: List[str],
ro_properties: List[str], readonly: bool,
module: Optional[ModuleType] = None,
imports: Optional[List[str]] = None) -> None:
"""Generate property stub using introspection of 'obj'.
Expand Down Expand Up @@ -286,11 +287,11 @@ def infer_prop_type(docstr: Optional[str]) -> Optional[str]:
'{}: ClassVar[{}] = ...{}'.format(name, inferred, trailing_comment)
)
else: # regular property
properties.append('@property')
properties.append('def {}(self) -> {}: ...'.format(name, inferred))
if not readonly:
properties.append('@{}.setter'.format(name))
properties.append('def {}(self, val: {}) -> None: ...'.format(name, inferred))
if readonly:
ro_properties.append('@property')
ro_properties.append('def {}(self) -> {}: ...'.format(name, inferred))
else:
rw_properties.append('{}: {}'.format(name, inferred))


def generate_c_type_stub(module: ModuleType,
Expand All @@ -312,7 +313,8 @@ def generate_c_type_stub(module: ModuleType,
methods = [] # type: List[str]
types = [] # type: List[str]
static_properties = [] # type: List[str]
properties = [] # type: List[str]
rw_properties = [] # type: List[str]
ro_properties = [] # type: List[str]
done = set() # type: Set[str]
for attr, value in items:
if is_c_method(value) or is_c_classmethod(value):
Expand All @@ -336,7 +338,7 @@ def generate_c_type_stub(module: ModuleType,
class_sigs=class_sigs)
elif is_c_property(value):
done.add(attr)
generate_c_property_stub(attr, value, static_properties, properties,
generate_c_property_stub(attr, value, static_properties, rw_properties, ro_properties,
is_c_property_readonly(value),
module=module, imports=imports)
elif is_c_type(value):
Expand Down Expand Up @@ -375,7 +377,7 @@ def generate_c_type_stub(module: ModuleType,
)
else:
bases_str = ''
if types or static_properties or methods or properties:
if types or static_properties or rw_properties or methods or ro_properties:
output.append('class %s%s:' % (class_name, bases_str))
for line in types:
if output and output[-1] and \
Expand All @@ -384,9 +386,11 @@ def generate_c_type_stub(module: ModuleType,
output.append(' ' + line)
for line in static_properties:
output.append(' %s' % line)
for line in rw_properties:
output.append(' %s' % line)
for line in methods:
output.append(' %s' % line)
for line in properties:
for line in ro_properties:
output.append(' %s' % line)
else:
output.append('class %s%s: ...' % (class_name, bases_str))
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ def get_attribute(self) -> None:
attribute = property(get_attribute, doc="")

output = [] # type: List[str]
generate_c_property_stub('attribute', TestClass.attribute, [], output, readonly=True)
generate_c_property_stub('attribute', TestClass.attribute, [], [], output, readonly=True)
assert_equal(output, ['@property', 'def attribute(self) -> str: ...'])

def test_generate_c_type_with_single_arg_generic(self) -> None:
Expand Down
10 changes: 2 additions & 8 deletions test-data/stubgen/pybind11_mypy_demo/basics.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Point:
x_axis: ClassVar[Point] = ... # read-only
y_axis: ClassVar[Point] = ... # read-only
origin: ClassVar[Point] = ...
x: float
y: float
@overload
def __init__(self) -> None: ...
@overload
Expand All @@ -53,14 +55,6 @@ class Point:
def distance_to(self, other: Point) -> float: ...
@property
def length(self) -> float: ...
@property
def x(self) -> float: ...
@x.setter
def x(self, val: float) -> None: ...
@property
def y(self) -> float: ...
@y.setter
def y(self, val: float) -> None: ...

def answer() -> int: ...
def midpoint(left: float, right: float) -> float: ...
Expand Down

0 comments on commit 951fc37

Please sign in to comment.