Skip to content

Check that generated ast respects type hints #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions oqpy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def to_ast(program: Program, item: AstConvertible) -> ast.Expression:
if isinstance(item, (bool, np.bool_)):
return ast.BooleanLiteral(item)
if isinstance(item, (int, np.integer)):
item = int(item)
if item < 0:
return ast.UnaryExpression(ast.UnaryOperator["-"], ast.IntegerLiteral(-item))
return ast.IntegerLiteral(item)
Expand Down
8 changes: 6 additions & 2 deletions oqpy/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def _make_externs_statements(self, auto_encal: bool = False) -> list[ast.ExternD
openpulse_externs.append(extern_statement)
break
else:
if isinstance(extern_statement.return_type.type, openpulse_types):
if isinstance(extern_statement.return_type, openpulse_types):
openpulse_externs.append(extern_statement)
else:
openqasm_externs.append(extern_statement)
Expand Down Expand Up @@ -474,9 +474,13 @@ def _do_assignment(self, var: AstConvertible, op: str, value: AstConvertible) ->
"""Helper function for variable assignment operations."""
if isinstance(var, classical_types.DurationVar):
value = make_duration(value)
var_ast = to_ast(self, var)
if isinstance(var_ast, ast.IndexExpression):
assert isinstance(var_ast.collection, ast.Identifier)
var_ast = ast.IndexedIdentifier(name=var_ast.collection, indices=[var_ast.index])
self._add_statement(
ast.ClassicalAssignment(
to_ast(self, var),
var_ast,
ast.AssignmentOperator[op],
to_ast(self, value),
)
Expand Down
4 changes: 2 additions & 2 deletions oqpy/subroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def wrapper(
if type_hints.get("return", False):
return_hint = type_hints["return"]()
if isinstance(return_hint, _ClassicalVar):
return_type = return_hint
return_type = return_hint.type
elif return_hint is not None:
raise ValueError(
f"Type hint for return variable on subroutine {name} is not an oqpy classical type."
Expand Down Expand Up @@ -176,7 +176,7 @@ def declare_extern(
extern_decl = ast.ExternDeclaration(
ast.Identifier(name),
[ast.ExternArgument(type=t) for t in arg_types],
ast.ExternArgument(type=return_type),
return_type,
)
extern_decl.annotations = make_annotations(annotations)

Expand Down
27 changes: 14 additions & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [
[tool.poetry.dependencies]
python = ">=3.7,<4.0"
# 0.4 loosens the antlr4-python3-runtime constraints
openpulse = ">=0.4.1,<0.5.0"
openpulse = ">=0.4.2"
numpy = [
{version = ">=1.14.5", python = ">=3.7,<3.8"},
{version = ">=1.17.3", python = ">=3.8,<3.9"},
Expand Down
Loading