Skip to content

Commit 1a42e68

Browse files
committed
[stubgenc] Render rw-properties as annotated class attributes
1 parent d80bf00 commit 1a42e68

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

mypy/stubgenc.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ def is_static_property(obj: object) -> bool:
250250

251251
def generate_c_property_stub(name: str, obj: object,
252252
static_properties: List[str],
253-
properties: List[str], readonly: bool,
253+
rw_properties: List[str],
254+
ro_properties: List[str], readonly: bool,
254255
module: Optional[ModuleType] = None,
255256
imports: Optional[List[str]] = None) -> None:
256257
"""Generate property stub using introspection of 'obj'.
@@ -286,11 +287,11 @@ def infer_prop_type(docstr: Optional[str]) -> Optional[str]:
286287
'{}: ClassVar[{}] = ...{}'.format(name, inferred, trailing_comment)
287288
)
288289
else: # regular property
289-
properties.append('@property')
290-
properties.append('def {}(self) -> {}: ...'.format(name, inferred))
291-
if not readonly:
292-
properties.append('@{}.setter'.format(name))
293-
properties.append('def {}(self, val: {}) -> None: ...'.format(name, inferred))
290+
if readonly:
291+
ro_properties.append('@property')
292+
ro_properties.append('def {}(self) -> {}: ...'.format(name, inferred))
293+
else:
294+
rw_properties.append('{}: {}'.format(name, inferred))
294295

295296

296297
def generate_c_type_stub(module: ModuleType,
@@ -312,7 +313,8 @@ def generate_c_type_stub(module: ModuleType,
312313
methods = [] # type: List[str]
313314
types = [] # type: List[str]
314315
static_properties = [] # type: List[str]
315-
properties = [] # type: List[str]
316+
rw_properties = [] # type: List[str]
317+
ro_properties = [] # type: List[str]
316318
done = set() # type: Set[str]
317319
for attr, value in items:
318320
if is_c_method(value) or is_c_classmethod(value):
@@ -336,7 +338,7 @@ def generate_c_type_stub(module: ModuleType,
336338
class_sigs=class_sigs)
337339
elif is_c_property(value):
338340
done.add(attr)
339-
generate_c_property_stub(attr, value, static_properties, properties,
341+
generate_c_property_stub(attr, value, static_properties, rw_properties, ro_properties,
340342
is_c_property_readonly(value),
341343
module=module, imports=imports)
342344
elif is_c_type(value):
@@ -375,7 +377,7 @@ def generate_c_type_stub(module: ModuleType,
375377
)
376378
else:
377379
bases_str = ''
378-
if types or static_properties or methods or properties:
380+
if types or static_properties or rw_properties or methods or ro_properties:
379381
output.append('class %s%s:' % (class_name, bases_str))
380382
for line in types:
381383
if output and output[-1] and \
@@ -384,9 +386,11 @@ def generate_c_type_stub(module: ModuleType,
384386
output.append(' ' + line)
385387
for line in static_properties:
386388
output.append(' %s' % line)
389+
for line in rw_properties:
390+
output.append(' %s' % line)
387391
for line in methods:
388392
output.append(' %s' % line)
389-
for line in properties:
393+
for line in ro_properties:
390394
output.append(' %s' % line)
391395
else:
392396
output.append('class %s%s: ...' % (class_name, bases_str))

mypy/test/teststubgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ def get_attribute(self) -> None:
815815
attribute = property(get_attribute, doc="")
816816

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

821821
def test_generate_c_type_with_single_arg_generic(self) -> None:

test-data/stubgen/pybind11_mypy_demo/basics.pyi

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Point:
4343
x_axis: ClassVar[Point] = ... # read-only
4444
y_axis: ClassVar[Point] = ... # read-only
4545
origin: ClassVar[Point] = ...
46+
x: float
47+
y: float
4648
@overload
4749
def __init__(self) -> None: ...
4850
@overload
@@ -53,14 +55,6 @@ class Point:
5355
def distance_to(self, other: Point) -> float: ...
5456
@property
5557
def length(self) -> float: ...
56-
@property
57-
def x(self) -> float: ...
58-
@x.setter
59-
def x(self, val: float) -> None: ...
60-
@property
61-
def y(self) -> float: ...
62-
@y.setter
63-
def y(self, val: float) -> None: ...
6458

6559
def answer() -> int: ...
6660
def midpoint(left: float, right: float) -> float: ...

0 commit comments

Comments
 (0)