Skip to content

Commit

Permalink
modified code to check for assignment targets to be of type name
Browse files Browse the repository at this point in the history
  • Loading branch information
nucccc committed Mar 24, 2024
1 parent 98250dc commit 15cfc77
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
12 changes: 8 additions & 4 deletions markarth/convert/collect/ast_to_typ/ast_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def assigned_typs(
# in such case it seems it is illegal for the target to be of
# tuple type, it will be of type name
target = ast_expr.target

target_var_name = target.id


left_typ = ast_val_to_typ(target, var_tracker)
right_typ = ast_val_to_typ(ast_expr.value, var_tracker)

Expand All @@ -96,7 +94,11 @@ def assigned_typs(
right_typ = right_typ
)

assigned_typs.add_typ(target_var_name, val_typ)
_add_target_to_typ_store(
target = target,
target_store = assigned_typs,
val_typ = val_typ
)

return AssignTypRes(
assigned_typs = assigned_typs,
Expand All @@ -118,6 +120,8 @@ def _add_target_to_typ_store(
target_store.add_typ(var_name, val_typ)
elif type(target) == ast.Tuple:
for elt in target.elts:
if type(elt) is not ast.Name:
continue
var_name = elt.id
# TODO: in case of a tuple at the moment i just put an any as the
# typ, in the future a check on eventual container typs maybe shall
Expand Down
36 changes: 35 additions & 1 deletion tests/test_ast_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,38 @@ def test_typ_store_from_target():

c_typ = typ_store.get_typ('c')
assert c_typ is not None
assert c_typ.is_any()
assert c_typ.is_any()


# additionally there should be some tests regarding subscripts
mod = ast.parse('a[0], b, c = 7')

assignment = mod.body[0]

typ_store = typ_store_from_target(
target = assignment.targets[0],
val_typ = typs.TypPrimitive(typs.PrimitiveCod.INT)
)

assert len(typ_store) == 2

b_typ = typ_store.get_typ('b')
assert b_typ is not None
assert b_typ.is_any()

c_typ = typ_store.get_typ('c')
assert c_typ is not None
assert c_typ.is_any()


# additionally there should be some tests regarding subscripts
mod = ast.parse('a[0] += 7')

assignment = mod.body[0]

typ_store = typ_store_from_target(
target = assignment.target,
val_typ = typs.TypPrimitive(typs.PrimitiveCod.INT)
)

assert len(typ_store) == 0

0 comments on commit 15cfc77

Please sign in to comment.