From 7a840d335e8c64b846307ebe114c26aa5e0d7db4 Mon Sep 17 00:00:00 2001 From: shBLOCK <50770712+shBLOCK@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:26:22 +0800 Subject: [PATCH] Stub generator: use property decorators to represent mutable attributes and properties --- codegen/stub_generator.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/codegen/stub_generator.py b/codegen/stub_generator.py index 10abd6b..6f53287 100644 --- a/codegen/stub_generator.py +++ b/codegen/stub_generator.py @@ -3,6 +3,8 @@ import regex +#TODO: docstring + def convert_type(org: str) -> str: types = [] for raw in map(str.strip, org.split("|")): @@ -26,13 +28,22 @@ def __init__(self, name: str, ptype: str, mutable: bool = True): self.mutable = mutable def stub(self) -> list[str]: + # if self.mutable: + # return [f"{self.name}: {self.type}"] + # else: + # return [ + # "@property", + # f"def {self.name}(self) -> {self.type}: ..." + # ] + stub = [ + "@property", + f"def {self.name}(self) -> {self.type}: ..." + ] if self.mutable: - return [f"{self.name}: {self.type}"] - else: - return [ - "@property", - f"def {self.name}(self) -> {self.type}: ..." - ] + stub.append(f"@{self.name}.setter") + stub.append(f"def {self.name}(self, value: {self.type}) -> None: ...") + return stub + class StubMethod: class Param: