Skip to content

Commit

Permalink
Add inout declaration to self argument
Browse files Browse the repository at this point in the history
  • Loading branch information
msaelices committed Sep 10, 2023
1 parent ac770fd commit 78e5401
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions py2mojo/converters/functiondef.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def _replace_def_keyword(tokens: list, i: int, level: int) -> None:
tokens[idx] = Token(name='NAME', src='fn')


def _add_declaration(tokens: list, i: int, level: int, declaration: str) -> None:
tokens.insert(i, Token(name='NAME', src=declaration))
tokens.insert(i +1 , Token(name='UNIMPORTANT_WS', src=' '))


def convert_functiondef(node: ast.FunctionDef, level: int = 0) -> Iterable:
"""Converts the annotation of the given function definition."""
if not node.args.args:
Expand All @@ -38,6 +43,17 @@ def convert_functiondef(node: ast.FunctionDef, level: int = 0) -> Iterable:
)

for arg in node.args.args:
if arg.arg == 'self':
yield (
ast_to_offset(arg),
partial(
_add_declaration,
declaration='inout',
),
)
continue


curr_type = get_annotation_type(arg.annotation)
new_type = get_mojo_type(curr_type)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_functiondef.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def __init__(self, x: int, y: int) -> int: ...
''',
'''
class Point:
def __init__(self, x: Int, y: Int) -> Int: ...
def __init__(inout self, x: Int, y: Int) -> Int: ...
''',
)

0 comments on commit 78e5401

Please sign in to comment.