Skip to content

Commit

Permalink
fix: prevent critical error message when missing self param in declar…
Browse files Browse the repository at this point in the history
…ation

---------

add tests to make sure we disallow type-aliases anywhere other than module body (for now)
  • Loading branch information
achidlow committed Jun 25, 2024
1 parent 8b4b63a commit 7ff2e17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/puya/awst_build/subroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,16 @@ def __init__(
mypy_arg_types = type_info.arg_types
if func_def.info is not mypy.nodes.FUNC_NO_INFO: # why god why
# function is a method
self._precondition(
bool(mypy_args) and mypy_args[0].variable.is_self,
"if function is a method, first variable should be self-like",
func_loc,
)
mypy_args = mypy_args[1:]
mypy_arg_types = mypy_arg_types[1:]
if not mypy_args:
context.error("Method declaration is missing 'self' argument", func_loc)
else:
self._precondition(
mypy_args[0].variable.is_self,
"if function is a method, first variable should be self-like",
func_loc,
)
mypy_args = mypy_args[1:]
mypy_arg_types = mypy_arg_types[1:]
elif mypy_args:
self._precondition(
not mypy_args[0].variable.is_self,
Expand Down
12 changes: 10 additions & 2 deletions tests/test_expected_output/subroutine.test
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,22 @@ def test(a: UInt64, b: UInt64) -> UInt64:

## case: no_type_alias
import typing
from algopy import UInt64, subroutine
from algopy import *

@subroutine
def test() -> UInt64:
Foo: typing.TypeAlias = UInt64 ## E: type aliases, type vars, and type constructors are not supported inside functions
one: Foo = UInt64(1)
return one

class MyContract(ARC4Contract):
Foo: typing.TypeAlias = UInt64 ## E: assignment statements are not supported in the class body

def __init__(self) -> None:
Bar: typing.TypeAlias = UInt64 ## E: type aliases, type vars, and type constructors are not supported inside functions
self.bar = Bar(1)


## case: no_assign_void
from algopy import UInt64, subroutine

Expand Down Expand Up @@ -476,4 +484,4 @@ def no_op() -> None:
@subroutine
def test() -> None:
if no_op(): ## E: None does not support boolean evaluation
pass
pass

0 comments on commit 7ff2e17

Please sign in to comment.