Skip to content

Commit

Permalink
Added type hinting for compound fields
Browse files Browse the repository at this point in the history
  • Loading branch information
zemanj committed Oct 24, 2023
1 parent 39f7254 commit 2772c6c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tests/codegen/handlers/test_create_compound_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_group_fields(self):
name="choice",
tag="Choice",
index=0,
types=[AttrTypeFactory.native(DataType.ANY_TYPE)],
types=list({t for attr in target.attrs for t in attr.types}),
choices=[
AttrFactory.create(
tag=target.attrs[0].tag,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/compound/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass, field
from typing import List
from typing import List, Union


@dataclass
Expand Down Expand Up @@ -35,7 +35,7 @@ class Root:
class Meta:
name = "root"

alpha_or_bravo: List[object] = field(
alpha_or_bravo: List[Union[Bravo, Alpha]] = field(
default_factory=list,
metadata={
"type": "Elements",
Expand Down
6 changes: 2 additions & 4 deletions xsdata/codegen/handlers/create_compound_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
from xsdata.codegen.mixins import ContainerInterface
from xsdata.codegen.mixins import RelativeHandlerInterface
from xsdata.codegen.models import Attr
from xsdata.codegen.models import AttrType
from xsdata.codegen.models import Class
from xsdata.codegen.models import get_restriction_choice
from xsdata.codegen.models import Restrictions
from xsdata.codegen.utils import ClassUtils
from xsdata.formats.dataclass.models.elements import XmlType
from xsdata.models.enums import DataType
from xsdata.models.enums import Tag
from xsdata.utils.collections import group_by

Expand Down Expand Up @@ -86,18 +84,18 @@ def group_fields(self, target: Class, attrs: List[Attr]):
ClassUtils.remove_attribute(target, attr)
names.append(attr.local_name)
choices.append(self.build_attr_choice(attr))

self.update_counters(attr, counters)

min_occurs, max_occurs = self.sum_counters(counters)
name = self.choose_name(target, names)
types = list({t for attr in attrs for t in attr.types})

target.attrs.insert(
pos,
Attr(
name=name,
index=0,
types=[AttrType(qname=str(DataType.ANY_TYPE), native=True)],
types=types,
tag=Tag.CHOICE,
restrictions=Restrictions(
min_occurs=sum(min_occurs),
Expand Down
6 changes: 3 additions & 3 deletions xsdata/formats/dataclass/models/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ def __init__(
self.is_attribute = False
self.is_attributes = False

if xml_type == XmlType.ELEMENT or self.clazz:
self.is_element = True
elif xml_type == XmlType.ELEMENTS:
if xml_type == XmlType.ELEMENTS:
self.is_elements = True
elif xml_type == XmlType.ELEMENT or self.clazz:
self.is_element = True
elif xml_type == XmlType.ATTRIBUTE:
self.is_attribute = True
elif xml_type == XmlType.ATTRIBUTES:
Expand Down

0 comments on commit 2772c6c

Please sign in to comment.