Skip to content

Commit

Permalink
Allow the same scheme name, if description matches
Browse files Browse the repository at this point in the history
Fixes #929
  • Loading branch information
hyanwong authored and mergify[bot] committed Jun 11, 2024
1 parent 1d45c0c commit e455221
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4532,11 +4532,39 @@ def test_is_copy(self):
assert schema is not other

def test_name_collision(self):
schema = MetadataSchema.permissive_json().schema
schema = tsinfer.add_to_schema(
schema, "name", definition={"type": "number", "description": "something"}
)
with pytest.raises(ValueError):
tsinfer.add_to_schema(
schema, "name", definition={"type": "number", "description": "alt"}
)

def test_name_collision_no_definition(self):
schema = MetadataSchema.permissive_json().schema
schema = tsinfer.add_to_schema(schema, "name")
with pytest.raises(ValueError):
tsinfer.add_to_schema(schema, "name")

def test_name_collision_same_description(self, caplog):
schema = MetadataSchema.permissive_json().schema
with caplog.at_level(logging.WARNING):
schema1 = tsinfer.add_to_schema(
schema,
"name",
definition={"description": "a unique description", "type": "number"},
)
assert caplog.text == ""
with caplog.at_level(logging.WARNING):
schema2 = tsinfer.add_to_schema(
schema1,
"name",
definition={"type": "number", "description": "a unique description"},
)
assert "already in schema" in caplog.text
assert schema1 == schema2

def test_defaults(self):
schema = MetadataSchema.permissive_json().schema
schema = tsinfer.add_to_schema(schema, "name")
Expand Down
18 changes: 18 additions & 0 deletions tsinfer/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,29 @@ def put(self, key, value):


def add_to_schema(schema, name, definition=None, required=False):
"""
Adds the specified metadata name to the schema, with the specified definition.
If the metadata name is already in the schema then either will warn about
potential overwriting (if the definition is the same and there is a description),
or will raise an error otherwise (to avoid conflicting metadata definitions).
"""
schema = copy.deepcopy(schema)
if definition is None:
definition = {}
try:
if name in schema["properties"]:
try:
if (
schema["properties"][name] == definition
and definition["description"] != ""
):
logger.warning(
f"Metadata {name} with identical description already in schema."
" Schema left unchanged: existing metadata may be overwritten."
)
return schema
except KeyError:
pass
raise ValueError(f"The metadata {name} is reserved for use by tsinfer")
except KeyError:
schema["properties"] = {}
Expand Down

0 comments on commit e455221

Please sign in to comment.