Skip to content

Commit

Permalink
Merge pull request #772 from Chia-Network/hint_to_json_dict_better
Browse files Browse the repository at this point in the history
hint `.to_json_dict()` better
  • Loading branch information
altendky authored Oct 25, 2024
2 parents fb263ef + 5b85f75 commit d4a1a53
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 242 deletions.
36 changes: 32 additions & 4 deletions wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ def transform_type(m: str) -> str:


def print_class(
file: TextIO, name: str, members: list[str], extra: Optional[list[str]] = None
file: TextIO,
name: str,
members: list[str],
extra: Optional[list[str]] = None,
martial_for_json_hint: Optional[str] = None,
unmartial_from_json_hint: Optional[str] = None,
):
def add_indent(x: str):
return "\n " + x

if martial_for_json_hint is None:
martial_for_json_hint = "dict[str, Any]"

if unmartial_from_json_hint is None:
unmartial_from_json_hint = martial_for_json_hint

init_args = "".join([(",\n " + transform_type(x)) for x in members])

all_replace_parameters = []
Expand Down Expand Up @@ -67,9 +78,9 @@ def to_bytes(self) -> bytes: ...
def __bytes__(self) -> bytes: ...
def stream_to_bytes(self) -> bytes: ...
def get_hash(self) -> bytes32: ...
def to_json_dict(self) -> Any: ...
def to_json_dict(self) -> {martial_for_json_hint}: ...
@classmethod
def from_json_dict(cls, json_dict: Any) -> Self: ...
def from_json_dict(cls, json_dict: {unmartial_from_json_hint}) -> Self: ...
"""
)

Expand Down Expand Up @@ -407,6 +418,8 @@ def __init__(
"def __iadd__(self, other: G1Element) -> G1Element: ...",
"def derive_unhardened(self, idx: int) -> G1Element: ...",
],
martial_for_json_hint="str",
unmartial_from_json_hint="Union[str, bytes]",
)
print_class(
file,
Expand All @@ -422,6 +435,8 @@ def __init__(
"def __add__(self, other: G2Element) -> G2Element: ...",
"def __iadd__(self, other: G2Element) -> G2Element: ...",
],
martial_for_json_hint="str",
unmartial_from_json_hint="Union[str, bytes]",
)
print_class(
file,
Expand All @@ -433,6 +448,7 @@ def __init__(
"def __mul__(self, rhs: GTElement) -> GTElement: ...",
"def __imul__(self, rhs: GTElement) -> GTElement : ...",
],
martial_for_json_hint="str",
)
print_class(
file,
Expand All @@ -449,6 +465,7 @@ def __init__(
"@staticmethod",
"def from_seed(seed: bytes) -> PrivateKey: ...",
],
martial_for_json_hint="str",
)

print_class(
Expand Down Expand Up @@ -496,4 +513,15 @@ def __init__(
)

for item in classes:
print_class(file, item[0], item[1], extra_members.get(item[0]))
# TODO: adjust the system to provide this control via more paths
martial_for_json_hint = None
if item[0] == "Program":
martial_for_json_hint = "str"

print_class(
file,
item[0],
item[1],
extra_members.get(item[0]),
martial_for_json_hint=martial_for_json_hint,
)
Loading

0 comments on commit d4a1a53

Please sign in to comment.