From 9c0f1bb32b46d5cd52c16554c814db9f045f52e2 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 26 Jan 2022 21:19:00 +0200 Subject: [PATCH 1/2] Remove the internal prefix for value fields Notes: In the past enumeration classes were allowed to be inner|nested. To resolve that issues I had used the prefix @ for value fields in order to quickly identify these cases. I know it was an ugly hack but worked well till now that someone noticed the conflict with the originalCase name convention. Since that part of the code was hackish and not really used anymore we can get rid for the code and the silly prefix. --- tests/codegen/handlers/test_class_extension.py | 2 +- tests/codegen/test_utils.py | 17 ----------------- tests/models/xsd/test_alternative.py | 2 +- tests/models/xsd/test_list.py | 2 +- tests/models/xsd/test_restriction.py | 2 +- tests/models/xsd/test_simple_type.py | 2 +- tests/models/xsd/test_union.py | 2 +- xsdata/codegen/handlers/class_extension.py | 3 ++- xsdata/codegen/utils.py | 7 ------- xsdata/models/xsd.py | 3 +-- xsdata/utils/constants.py | 1 + 11 files changed, 10 insertions(+), 33 deletions(-) diff --git a/tests/codegen/handlers/test_class_extension.py b/tests/codegen/handlers/test_class_extension.py index 28b06f7d3..63f22d68c 100644 --- a/tests/codegen/handlers/test_class_extension.py +++ b/tests/codegen/handlers/test_class_extension.py @@ -444,7 +444,7 @@ def test_add_default_attribute(self): ClassExtensionHandler.add_default_attribute(item, extension) expected = AttrFactory.create( - name="@value", default=None, types=[xs_string], tag=Tag.EXTENSION + name="value", default=None, types=[xs_string], tag=Tag.EXTENSION ) self.assertEqual(2, len(item.attrs)) diff --git a/tests/codegen/test_utils.py b/tests/codegen/test_utils.py index 41bbf24ba..05853a5ba 100644 --- a/tests/codegen/test_utils.py +++ b/tests/codegen/test_utils.py @@ -179,23 +179,6 @@ def test_copy_inner_class(self): self.assertEqual(target.module, target.inner[0].module) self.assertEqual(inner.qname, target.inner[0].qname) - def test_copy_inner_class_rename_simple_inner_type(self): - source = ClassFactory.create() - inner = ClassFactory.create(qname="{a}@value", module="b", package="c") - target = ClassFactory.create() - attr = AttrFactory.create(name="simple") - attr_type = AttrTypeFactory.create(forward=True, qname=inner.qname) - - source.inner.append(inner) - ClassUtils.copy_inner_class(source, target, attr, attr_type) - - self.assertEqual(1, len(target.inner)) - self.assertIsNot(inner, target.inner[0]) - self.assertEqual(target.package, target.inner[0].package) - self.assertEqual(target.module, target.inner[0].module) - self.assertEqual("{a}simple", target.inner[0].qname) - self.assertEqual("{a}simple", attr_type.qname) - def test_copy_inner_class_skip_non_forward_reference(self): source = ClassFactory.create() target = ClassFactory.create() diff --git a/tests/models/xsd/test_alternative.py b/tests/models/xsd/test_alternative.py index 27bdb13ef..60eb3d2f3 100644 --- a/tests/models/xsd/test_alternative.py +++ b/tests/models/xsd/test_alternative.py @@ -6,7 +6,7 @@ class AlternativeTests(TestCase): def test_property_real_name(self): obj = Alternative() - self.assertEqual("@value", obj.real_name) + self.assertEqual("value", obj.real_name) obj.id = "foo" self.assertEqual("foo", obj.real_name) diff --git a/tests/models/xsd/test_list.py b/tests/models/xsd/test_list.py index 4ba9cc0ca..23391f1d7 100644 --- a/tests/models/xsd/test_list.py +++ b/tests/models/xsd/test_list.py @@ -10,7 +10,7 @@ def test_is_attribute(self): def test_real_name(self): obj = List() - self.assertEqual("@value", obj.real_name) + self.assertEqual("value", obj.real_name) def test_real_type(self): obj = List() diff --git a/tests/models/xsd/test_restriction.py b/tests/models/xsd/test_restriction.py index c56f088d1..3616c0318 100644 --- a/tests/models/xsd/test_restriction.py +++ b/tests/models/xsd/test_restriction.py @@ -34,7 +34,7 @@ def test_property_attr_types(self): def test_property_real_name(self): obj = Restriction() - self.assertEqual("@value", obj.real_name) + self.assertEqual("value", obj.real_name) def test_property_bases(self): obj = Restriction() diff --git a/tests/models/xsd/test_simple_type.py b/tests/models/xsd/test_simple_type.py index e81007aa4..424be2d2e 100644 --- a/tests/models/xsd/test_simple_type.py +++ b/tests/models/xsd/test_simple_type.py @@ -11,7 +11,7 @@ class SimpleTypeTests(TestCase): def test_property_real_name(self): obj = SimpleType() - self.assertEqual("@value", obj.real_name) + self.assertEqual("value", obj.real_name) obj.name = "foo" self.assertEqual("foo", obj.real_name) diff --git a/tests/models/xsd/test_union.py b/tests/models/xsd/test_union.py index 168f9b0af..854058c66 100644 --- a/tests/models/xsd/test_union.py +++ b/tests/models/xsd/test_union.py @@ -25,7 +25,7 @@ def test_property_is_property(self): def test_property_real_name(self): obj = Union() - self.assertEqual("@value", obj.real_name) + self.assertEqual("value", obj.real_name) def test_property_attr_types(self): obj = Union() diff --git a/xsdata/codegen/handlers/class_extension.py b/xsdata/codegen/handlers/class_extension.py index 4b31aaf39..2aa3604d4 100644 --- a/xsdata/codegen/handlers/class_extension.py +++ b/xsdata/codegen/handlers/class_extension.py @@ -11,6 +11,7 @@ from xsdata.models.enums import DataType from xsdata.models.enums import NamespaceType from xsdata.models.enums import Tag +from xsdata.utils.constants import DEFAULT_ATTR_NAME class ClassExtensionHandler(RelativeHandlerInterface): @@ -268,7 +269,7 @@ def add_default_attribute(cls, target: Class, extension: Extension): type.""" if extension.type.datatype != DataType.ANY_TYPE: tag = Tag.EXTENSION - name = "@value" + name = DEFAULT_ATTR_NAME namespace = None else: tag = Tag.ANY diff --git a/xsdata/codegen/utils.py b/xsdata/codegen/utils.py index 40af02229..4e0331c2a 100644 --- a/xsdata/codegen/utils.py +++ b/xsdata/codegen/utils.py @@ -136,13 +136,6 @@ class from the source to the target class. clone = inner.clone() clone.package = target.package clone.module = target.module - - # Simple type, update the name - if clone.name == "@value": - namespace, _ = namespaces.split_qname(clone.qname) - qname = namespaces.build_qname(namespace, attr.name) - clone.qname = attr_type.qname = qname - target.inner.append(clone) @classmethod diff --git a/xsdata/models/xsd.py b/xsdata/models/xsd.py index 5cce18883..fd8a41728 100644 --- a/xsdata/models/xsd.py +++ b/xsdata/models/xsd.py @@ -24,14 +24,13 @@ from xsdata.models.mixins import ElementBase from xsdata.utils import text from xsdata.utils.collections import unique_sequence +from xsdata.utils.constants import DEFAULT_ATTR_NAME from xsdata.utils.namespaces import clean_uri docstring_serializer = XmlSerializer( config=SerializerConfig(pretty_print=True, xml_declaration=False) ) -DEFAULT_ATTR_NAME = "@value" - @dataclass(frozen=True) class Docstring: diff --git a/xsdata/utils/constants.py b/xsdata/utils/constants.py index 91cf91053..cc6c02b26 100644 --- a/xsdata/utils/constants.py +++ b/xsdata/utils/constants.py @@ -10,6 +10,7 @@ XML_FALSE = sys.intern("false") XML_TRUE = sys.intern("true") +DEFAULT_ATTR_NAME = "value" def return_true(*_: Any) -> bool: From 077e0771c5c5fb2b8697500c7eef62419700129d Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 26 Jan 2022 21:23:04 +0200 Subject: [PATCH 2/2] Update pre-commit hooks --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 96c1e206a..64e1f3161 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,7 @@ repos: - id: pyupgrade args: [--py37-plus] - repo: https://github.com/asottile/reorder_python_imports - rev: v2.6.0 + rev: v2.7.1 hooks: - id: reorder-python-imports - repo: https://github.com/ambv/black