Skip to content

Commit c3cd286

Browse files
committed
increase test coverage
1 parent 692e0e5 commit c3cd286

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

oqpy/control_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def ForIn(
8686
iterator: Iterable[AstConvertible] | range | AstConvertible,
8787
identifier_name: Optional[str],
8888
) -> contextlib._GeneratorContextManager[IntVar]:
89-
...
89+
... # pragma: no cover
9090

9191

9292
@overload
@@ -96,7 +96,7 @@ def ForIn(
9696
identifier_name: Optional[str],
9797
identifier_type: type[ClassicalVarT],
9898
) -> contextlib._GeneratorContextManager[ClassicalVarT]:
99-
...
99+
... # pragma: no cover
100100

101101

102102
@contextlib.contextmanager

oqpy/subroutines.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,17 @@ def increment_variable(int[32] i) {
100100
for argname in argnames[1:]: # arg 0 should be program
101101
if argname not in type_hints:
102102
raise ValueError(f"No type hint provided for {argname} on subroutine {name}.")
103+
elif not issubclass(type_hints[argname], (_ClassicalVar, Qubit)):
104+
raise ValueError(
105+
f"Type hint for {argname} on subroutine {name} is not an oqpy variable type."
106+
)
107+
103108
input_ = inputs[argname] = type_hints[argname](name=argname)
104109

105110
if isinstance(input_, _ClassicalVar):
106111
arguments.append(ast.ClassicalArgument(input_.type, ast.Identifier(argname)))
107112
elif isinstance(input_, Qubit):
108113
arguments.append(ast.QuantumArgument(ast.Identifier(input_.name), None))
109-
else:
110-
raise ValueError(
111-
f"Type hint for {argname} on subroutine {name} is not an oqpy variable type."
112-
)
113114

114115
inner_prog = oqpy.Program()
115116
for input_val in inputs.values():

tests/test_directives.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def test_variable_declaration():
141141
BitVar[-1](name="d")
142142
with pytest.raises(IndexError):
143143
prog.set(arr[1.3], 0)
144+
with pytest.raises(IndexError):
145+
prog.set(arr[index * 2.0], 0)
144146
with pytest.raises(TypeError):
145147
prog.set(c[0], 1)
146148

@@ -811,10 +813,18 @@ def return2(prog: Program) -> float:
811813
with pytest.raises(ValueError):
812814

813815
@subroutine
814-
def add(prog: Program, x: IntVar, y) -> IntVar:
816+
def add1(prog: Program, x: IntVar, y) -> IntVar:
815817
return x + y
816818

817-
prog.set(y, add(prog, y, 3))
819+
prog.set(y, add1(prog, y, 3))
820+
821+
with pytest.raises(ValueError):
822+
823+
@subroutine
824+
def add2(prog: Program, x: IntVar, y: int) -> IntVar:
825+
return x + y
826+
827+
prog.set(y, add2(prog, y, 3))
818828

819829
expected = textwrap.dedent(
820830
"""
@@ -1972,9 +1982,16 @@ def to_ast(self):
19721982
obj = MyToAst()
19731983
assert convert_float_to_duration(obj) is obj
19741984

1985+
with pytest.warns(DeprecationWarning):
1986+
make_duration(obj)
1987+
19751988
with pytest.raises(TypeError):
19761989
convert_float_to_duration("asdf")
19771990

1991+
b = BoolVar(True, "b")
1992+
with pytest.raises(TypeError):
1993+
convert_float_to_duration(b)
1994+
19781995

19791996
def test_autoencal():
19801997
port = PortVar("portname")

0 commit comments

Comments
 (0)