Skip to content

Commit

Permalink
Add test for propagation of optional var
Browse files Browse the repository at this point in the history
  • Loading branch information
neNasko1 committed Dec 9, 2024
1 parent 756f274 commit ac8807e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/spox/_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ def fully_typed(self) -> bool:

@dataclass
class BaseInputs(BaseVarInfos):
def vars(self, prop_values: Optional[PropDict] = None) -> BaseVars:
if prop_values is None:
prop_values = {}

def vars(self, prop_values: PropDict) -> BaseVars:
vars_dict: dict[str, Union[Var, Optional[Var], Sequence[Var]]] = {}

for field in dataclasses.fields(self):
Expand All @@ -169,10 +166,7 @@ def vars(self, prop_values: Optional[PropDict] = None) -> BaseVars:
prop_value = cast(PropValue, prop_values.get(field.name, None))
vars_dict[field.name] = Var(field_value, prop_value)

elif (
field_type == VarFieldKind.OPTIONAL
and prop_values.get(field.name, None) is not None
):
elif field_type == VarFieldKind.OPTIONAL:
prop_value = cast(PropValue, prop_values.get(field.name, None))
vars_dict[field.name] = Var(field_value, prop_value)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_value_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ def test_empty_optional_has_no_element():
)


@pytest.mark.parametrize("min", [None, 2])
def test_optional_clip(min):
min_var = min if min is None else op.const(min)
assert_equal_value(
op.clip(op.const([1, 2, 3]), min=min_var, max=op.const(3)),
np.clip([1, 2, 3], a_min=min, a_max=3),
)


def test_sequence_empty():
assert_equal_value(op.sequence_empty(dtype=np.float32), [])

Expand Down

0 comments on commit ac8807e

Please sign in to comment.