Skip to content

Commit

Permalink
feat: fix generated class names that are acronyms (#91)
Browse files Browse the repository at this point in the history
Allow OnCompleteAction use in ARC4 abimethod, baremethod decorators

BREAKING CHANGE: enum class names have changed
  • Loading branch information
daniel-makerx authored Feb 9, 2024
1 parent 7adea2c commit bd3f222
Show file tree
Hide file tree
Showing 30 changed files with 1,762 additions and 2,071 deletions.
8 changes: 5 additions & 3 deletions scripts/generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,9 @@ def map_typed_names(


def get_python_enum_class(arg_enum: str) -> str:
# don't change acronyms
if arg_enum.isupper():
return arg_enum
return snake_case(arg_enum).replace("_", " ").title().replace(" ", "")


Expand Down Expand Up @@ -647,10 +650,10 @@ def get_overriding_immediate(op: Op) -> Immediate | None:
def build_enum(spec: LanguageSpec, arg_enum: str) -> Iterable[str]:
values = spec.arg_enums[arg_enum]
enum_name = get_python_enum_class(arg_enum)
yield f"class {enum_name}(enum.StrEnum):"
yield f"class {enum_name}(str):"
yield f'{INDENT}"""Available values for the `{arg_enum}` enum"""'
for value in values:
yield f"{INDENT}{value.name} = ..."
yield f"{INDENT}{value.name}: {enum_name} = ..."
yield ""


Expand Down Expand Up @@ -903,7 +906,6 @@ def output_stub(
class_ops: list[ClassDef],
) -> None:
stub: list[str] = [
"import enum",
"import typing",
"",
f"from puyapy import {CLS_ACCOUNT}, {CLS_BIGINT}, {CLS_BYTES}, {CLS_UINT64}",
Expand Down
15 changes: 14 additions & 1 deletion scripts/vendor_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

MYPY_REPO = "https://github.com/python/mypy.git"
VCS_ROOT = Path(__file__).parent.parent
TYPESHED_README = """
This is PuyaPy's custom typeshed, which is a curated subset of the official MyPy typeshed.
It only includes the required stubs used by PuyaPy as this speeds up MyPy's parsing speed
significantly.
However this means certain python modules such as `enum` or `dataclasses` cannot be used in
PuyaPy stubs unless this typeshed is updated.
The contents of the typeshed are populated by the `scripts/vendor_mypy.py` script, which is used
to vendor new versions of MyPy or to update the stubs included in this typeshed. So to add new
stubs, update that script and rerun.
""".strip()


def clone_branch(version: str) -> str:
Expand Down Expand Up @@ -63,7 +75,7 @@ def update_puya_typeshed(mypy_typeshed: Path, puya_typeshed: Path) -> None:
stdlib / "sys.pyi",
stdlib / "abc.pyi",
# needed for puyapy
stdlib / "enum.pyi",
# stdlib / "enum.pyi"
]

(puya_typeshed / stdlib).mkdir(exist_ok=True, parents=True)
Expand All @@ -77,6 +89,7 @@ def update_puya_typeshed(mypy_typeshed: Path, puya_typeshed: Path) -> None:
copy_dst.parent.mkdir(exist_ok=True, parents=True)
shutil.copy(copy_src, copy_dst)
(puya_typeshed / stdlib / "collections" / "__init__.pyi").touch()
(puya_typeshed / "README.md").write_text(TYPESHED_README)


if __name__ == "__main__":
Expand Down
10 changes: 10 additions & 0 deletions src/puya/_typeshed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is PuyaPy's custom typeshed, which is a curated subset of the official MyPy typeshed.
It only includes the required stubs used by PuyaPy as this speeds up MyPy's parsing speed
significantly.

However this means certain python modules such as `enum` or `dataclasses` cannot be used in
PuyaPy stubs unless this typeshed is updated.

The contents of the typeshed are populated by the `scripts/vendor_mypy.py` script, which is used
to vendor new versions of MyPy or to update the stubs included in this typeshed. So to add new
stubs, update that script and rerun.
317 changes: 0 additions & 317 deletions src/puya/_typeshed/stdlib/enum.pyi

This file was deleted.

Loading

0 comments on commit bd3f222

Please sign in to comment.