Skip to content

Commit 25b2dec

Browse files
authored
Check that generated ast respects type hints (#48)
* Check that generated ast respects type hints * remove print statement * Skip type hint checking on python < 3.9 * Update openpulse which has fix for union import * Fix problem with indexed identifier * Add checks for list/dict contents in type hints * Correctly pass in list of except_fields
1 parent 046266a commit 25b2dec

File tree

6 files changed

+144
-34
lines changed

6 files changed

+144
-34
lines changed

oqpy/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def to_ast(program: Program, item: AstConvertible) -> ast.Expression:
323323
if isinstance(item, (bool, np.bool_)):
324324
return ast.BooleanLiteral(item)
325325
if isinstance(item, (int, np.integer)):
326+
item = int(item)
326327
if item < 0:
327328
return ast.UnaryExpression(ast.UnaryOperator["-"], ast.IntegerLiteral(-item))
328329
return ast.IntegerLiteral(item)

oqpy/program.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def _make_externs_statements(self, auto_encal: bool = False) -> list[ast.ExternD
239239
openpulse_externs.append(extern_statement)
240240
break
241241
else:
242-
if isinstance(extern_statement.return_type.type, openpulse_types):
242+
if isinstance(extern_statement.return_type, openpulse_types):
243243
openpulse_externs.append(extern_statement)
244244
else:
245245
openqasm_externs.append(extern_statement)
@@ -474,9 +474,13 @@ def _do_assignment(self, var: AstConvertible, op: str, value: AstConvertible) ->
474474
"""Helper function for variable assignment operations."""
475475
if isinstance(var, classical_types.DurationVar):
476476
value = make_duration(value)
477+
var_ast = to_ast(self, var)
478+
if isinstance(var_ast, ast.IndexExpression):
479+
assert isinstance(var_ast.collection, ast.Identifier)
480+
var_ast = ast.IndexedIdentifier(name=var_ast.collection, indices=[var_ast.index])
477481
self._add_statement(
478482
ast.ClassicalAssignment(
479-
to_ast(self, var),
483+
var_ast,
480484
ast.AssignmentOperator[op],
481485
to_ast(self, value),
482486
)

oqpy/subroutines.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def wrapper(
106106
if type_hints.get("return", False):
107107
return_hint = type_hints["return"]()
108108
if isinstance(return_hint, _ClassicalVar):
109-
return_type = return_hint
109+
return_type = return_hint.type
110110
elif return_hint is not None:
111111
raise ValueError(
112112
f"Type hint for return variable on subroutine {name} is not an oqpy classical type."
@@ -176,7 +176,7 @@ def declare_extern(
176176
extern_decl = ast.ExternDeclaration(
177177
ast.Identifier(name),
178178
[ast.ExternArgument(type=t) for t in arg_types],
179-
ast.ExternArgument(type=return_type),
179+
return_type,
180180
)
181181
extern_decl.annotations = make_annotations(annotations)
182182

poetry.lock

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ classifiers = [
1717
[tool.poetry.dependencies]
1818
python = ">=3.7,<4.0"
1919
# 0.4 loosens the antlr4-python3-runtime constraints
20-
openpulse = ">=0.4.1,<0.5.0"
20+
openpulse = ">=0.4.2"
2121
numpy = [
2222
{version = ">=1.14.5", python = ">=3.7,<3.8"},
2323
{version = ">=1.17.3", python = ">=3.8,<3.9"},

0 commit comments

Comments
 (0)