Skip to content

Commit

Permalink
Fix attribute groups annotation parsing (issue #366)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunato committed Sep 15, 2023
1 parent 43657b2 commit d0f07d7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
45 changes: 45 additions & 0 deletions tests/validators/test_xsdbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,51 @@ def test_annotations(self):
self.assertEqual(len(annotation.errors), 0) # see issue 287
self.assertIsNone(annotation.annotation)

def test_attribute_group_annotation__issue_366(self):
schema = XMLSchema10(dedent("""\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attributeGroup name="attrGroup">
<xs:annotation>
<xs:documentation>
A global attribute group
</xs:documentation>
</xs:annotation>
<xs:attribute name="attr1"/>
<xs:attribute name="attr2"/>
</xs:attributeGroup>
<xs:complexType name="rootType" mixed="true">
<xs:annotation>
<xs:documentation>
A global complex type
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>
<xs:attributeGroup ref="attrGroup"/>
</xs:complexType>
<xs:element name="root" type="rootType">
<xs:annotation>
<xs:documentation>
The root element
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:schema>"""))

attribute_group = schema.attribute_groups['attrGroup']
self.assertIn('A global attribute group', str(attribute_group.annotation))

xsd_type = schema.types['rootType']
self.assertIn('A global complex type', str(xsd_type.annotation))
self.assertIsNone(xsd_type.attributes.annotation)

xsd_element = schema.elements['root']
self.assertIn('The root element', str(xsd_element.annotation))
self.assertIsNone(xsd_element.attributes.annotation)


class TestXsdType(unittest.TestCase):

Expand Down
8 changes: 7 additions & 1 deletion xmlschema/validators/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ..helpers import get_namespace, get_qname

from .exceptions import XMLSchemaValidationError
from .xsdbase import XsdComponent, ValidationMixin
from .xsdbase import XsdComponent, XsdAnnotation, ValidationMixin
from .simple_types import XsdSimpleType
from .wildcards import XsdAnyAttribute

Expand Down Expand Up @@ -614,6 +614,12 @@ def _parse(self) -> None:
def built(self) -> bool:
return True

@property
def annotation(self) -> Optional['XsdAnnotation']:
if self.parent is not None and '_annotation' not in self.__dict__:
self._annotation = None
return super().annotation

def parse_error(self, error: Union[str, Exception],
elem: Optional[ElementType] = None,
validation: Optional[str] = None) -> None:
Expand Down

0 comments on commit d0f07d7

Please sign in to comment.