From 28d118f4eecaeaa850f41f45d89f84dbf690938e Mon Sep 17 00:00:00 2001 From: florian <f.spreckelsen@inidscale.com> Date: Wed, 24 Aug 2022 15:38:05 +0200 Subject: [PATCH] MAINT: Deprecate `parent` keyword in yaml datamodel Resolves #36 --- CHANGELOG.md | 5 ++++ src/caosadvancedtools/models/parser.py | 11 ++++++++ src/doc/yaml_interface.rst | 2 +- unittests/test_yaml_model_parser.py | 39 +++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea860b1d..58cd56df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ## ### Added ### + - You can now use `python -m caosadvancedtools.models.parser model_file` to parse and potentially synchronize data models. @@ -14,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated ### +- [#36](https://gitlab.com/caosdb/caosdb-advanced-user-tools/-/issues/36) + `parent` keyword in yaml datamodel definition (replaced by + `inherit_from_{obligatory|recommended|suggested}` keywords). + ### Removed ### ### Fixed ### diff --git a/src/caosadvancedtools/models/parser.py b/src/caosadvancedtools/models/parser.py index 25fe83d9..c9b890de 100644 --- a/src/caosadvancedtools/models/parser.py +++ b/src/caosadvancedtools/models/parser.py @@ -42,6 +42,7 @@ import sys import yaml from typing import List +from warnings import warn import jsonschema import caosdb as db @@ -519,6 +520,16 @@ class Parser(object): self._inherit(name, prop, db.RECOMMENDED) elif prop_name == "inherit_from_suggested": self._inherit(name, prop, db.SUGGESTED) + elif prop_name == "parent": + warn( + DeprecationWarning( + "The `parent` keyword is deprecated and will be " + "removed in a future version. Use " + "`inherit_from_{obligatory|recommended|suggested}` " + "instead." + ) + ) + self._inherit(name, prop, db.OBLIGATORY) else: raise ValueError("invalid keyword: {}".format(prop_name)) diff --git a/src/doc/yaml_interface.rst b/src/doc/yaml_interface.rst index f5dea11a..78ff4cdd 100644 --- a/src/doc/yaml_interface.rst +++ b/src/doc/yaml_interface.rst @@ -109,7 +109,7 @@ Keywords added as parents, and all Properties with at least the importance ``XXX`` are inherited. For example, ``inherited_from_recommended`` will inherit all Properties of importance ``recommended`` and ``obligatory``, but not ``suggested``. -- **parent**: Parent of this entity. (*Deprecated*) +- **parent**: Parent of this entity. Same as ``inherit_from_obligatory``. (*Deprecated*) Usage ===== diff --git a/unittests/test_yaml_model_parser.py b/unittests/test_yaml_model_parser.py index a9f072b7..6cdea792 100644 --- a/unittests/test_yaml_model_parser.py +++ b/unittests/test_yaml_model_parser.py @@ -1,7 +1,7 @@ import unittest from datetime import date from tempfile import NamedTemporaryFile -from pytest import raises +from pytest import deprecated_call, raises import caosdb as db from caosadvancedtools.models.parser import (TwiceDefinedException, @@ -474,3 +474,40 @@ F: """ with raises(NotImplementedError): entities = parse_model_from_string(model) + + +def test_issue_36(): + """Test whether the `parent` keyword is deprecated. + + See https://gitlab.com/caosdb/caosdb-advanced-user-tools/-/issues/36. + + """ + model_string = """ +R1: + obligatory_properties: + prop1: + datatype: TEXT +R2: + obligatory_properties: + prop2: + datatype: TEXT + recommended_properties: + prop3: + datatype: TEXT +R3: + parent: + - R2 + inherit_from_obligatory: + - R1 +""" + with deprecated_call(): + # Check whether this is actually deprecated + model = parse_model_from_string(model_string) + + assert "R3" in model + r3 = model["R3"] + assert isinstance(r3, db.RecordType) + for par in ["R1", "R2"]: + # Until removal, both do the same + assert has_parent(r3, par) + assert r3.get_parent(par)._flags["inheritance"] == db.OBLIGATORY -- GitLab