Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type hints for compound fields #858

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions xsdata/formats/dataclass/models/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ def is_valid(
# xs:NMTOKENS need origin list
return False

if object in types:
# Any type, secondary types are not allowed
if object in types and xml_type != XmlType.ELEMENTS:
# Any type, secondary types are not allowed except for 'Elements' XML type
return len(types) == 1

return self.is_typing_supported(types)
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