Skip to content

Commit 40c9e1b

Browse files
hyanwongbenjeffery
authored andcommitted
Allow the same scheme name, if description matches
Fixes #929
1 parent dc2c0bf commit 40c9e1b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

tests/test_inference.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4350,11 +4350,39 @@ def test_is_copy(self):
43504350
assert schema is not other
43514351

43524352
def test_name_collision(self):
4353+
schema = MetadataSchema.permissive_json().schema
4354+
schema = tsinfer.add_to_schema(
4355+
schema, "name", definition={"type": "number", "description": "something"}
4356+
)
4357+
with pytest.raises(ValueError):
4358+
tsinfer.add_to_schema(
4359+
schema, "name", definition={"type": "number", "description": "alt"}
4360+
)
4361+
4362+
def test_name_collision_no_definition(self):
43534363
schema = MetadataSchema.permissive_json().schema
43544364
schema = tsinfer.add_to_schema(schema, "name")
43554365
with pytest.raises(ValueError):
43564366
tsinfer.add_to_schema(schema, "name")
43574367

4368+
def test_name_collision_same_description(self, caplog):
4369+
schema = MetadataSchema.permissive_json().schema
4370+
with caplog.at_level(logging.WARNING):
4371+
schema1 = tsinfer.add_to_schema(
4372+
schema,
4373+
"name",
4374+
definition={"description": "a unique description", "type": "number"},
4375+
)
4376+
assert caplog.text == ""
4377+
with caplog.at_level(logging.WARNING):
4378+
schema2 = tsinfer.add_to_schema(
4379+
schema1,
4380+
"name",
4381+
definition={"type": "number", "description": "a unique description"},
4382+
)
4383+
assert "already in schema" in caplog.text
4384+
assert schema1 == schema2
4385+
43584386
def test_defaults(self):
43594387
schema = MetadataSchema.permissive_json().schema
43604388
schema = tsinfer.add_to_schema(schema, "name")

tsinfer/inference.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,29 @@
7272

7373

7474
def add_to_schema(schema, name, definition=None, required=False):
75+
"""
76+
Adds the specified metadata name to the schema, with the specified definition.
77+
If the metadata name is already in the schema then either will warn about
78+
potential overwriting (if the definition is the same and there is a description),
79+
or will raise an error otherwise (to avoid conflicting metadata definitions).
80+
"""
7581
schema = copy.deepcopy(schema)
7682
if definition is None:
7783
definition = {}
7884
try:
7985
if name in schema["properties"]:
86+
try:
87+
if (
88+
schema["properties"][name] == definition
89+
and definition["description"] != ""
90+
):
91+
logger.warning(
92+
f"Metadata {name} with identical description already in schema."
93+
" Schema left unchanged: existing metadata may be overwritten."
94+
)
95+
return schema
96+
except KeyError:
97+
pass
8098
raise ValueError(f"The metadata {name} is reserved for use by tsinfer")
8199
except KeyError:
82100
schema["properties"] = {}

0 commit comments

Comments
 (0)