Skip to content

Commit

Permalink
infer nested schema (#405)
Browse files Browse the repository at this point in the history
* infer nested schema
* black
  • Loading branch information
ryanSoley authored Feb 2, 2024
1 parent a2958f3 commit c5c7cc1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion rubicon_ml/schema/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ def _safe_call_func(obj, func, optional, default=None):
@contextmanager
def _set_temporary_schema(project, schema_name):
original_schema = project.schema_
project.set_schema(registry.get_schema(schema_name))

if schema_name == "infer":
delattr(project, "schema_")
else:
project.set_schema(registry.get_schema(schema_name))

yield

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def parameter_schema():
def nested_schema():
"""Returns a schema for testing nested schema."""

return {"schema": [{"name": "AnotherObject", "attr": "object_"}]}
return {"schema": [{"name": "tests___AnotherObject", "attr": "object_"}]}


@pytest.fixture
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/schema/test_schema_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,17 @@ def test_log_parameters_with_schema(objects_to_log, rubicon_project, parameter_s
assert parameter_b.value == "param env value"


def test_log_nested_schema(objects_to_log, rubicon_project, another_object_schema, nested_schema):
@pytest.mark.parametrize("infer", [True, False])
def test_log_nested_schema(
objects_to_log, rubicon_project, another_object_schema, nested_schema, infer
):
"""Testing ``Project.log_with_schema`` can log nested schema."""

if infer:
nested_schema["schema"][0]["name"] = "infer"

object_to_log, another_object = objects_to_log
schema_to_patch = {"AnotherObject": lambda: another_object_schema}
schema_to_patch = {"tests___AnotherObject": lambda: another_object_schema}

with mock.patch.dict(RUBICON_SCHEMA_REGISTRY, schema_to_patch, clear=True):
rubicon_project.set_schema(nested_schema)
Expand Down

0 comments on commit c5c7cc1

Please sign in to comment.