8000 Typing issues · Issue #466 · tefra/xsdata · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Typing issues #466

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

Closed
nmrtv opened this issue Apr 16, 2021 · 4 comments
Closed

Typing issues #466

nmrtv opened this issue Apr 16, 2021 · 4 comments

Comments

@nmrtv
Copy link
Contributor
nmrtv commented Apr 16, 2021

With UBL schemas, I found 2 problems in file common\ubl_xad_esv132_2_1.py:

  1. Mypy complains about the generic Dict type in a strict mode:
@dataclass
class AnyType:
    any_element: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Wildcard",
            "namespace": "##any",
            "mixed": True,
        },
    )
    any_attributes: Dict = field(
        default_factory=dict,
        metadata={
            "type": "Attributes",
            "namespace": "##any",
        },
    )

XML attributes are always strings, can we change that type hint to Dict[str, str]?

  1. The type of inherited field OtherTimeStampType.encapsulated_time_stamp is different from the base class.
@tefra
Copy link
Owner
tefra commented Apr 16, 2021

Thanks for reporing @neriusmika

Yeah the first one is pretty straightforward and you are correct the keys and values are always str.
The second one not so much, I have encountered it before but I am not really sure how to handle it.

In the base class the element is effectively a list where in the subclass the element has effectively maxOccurs=1

<xsd:complexType name="GenericTimeStampType" abstract="true">
    <xsd:sequence>
                   ...
        <xsd:choice maxOccurs="unbounded"><!-- maxOccurs > 1 aka list -->
            <xsd:element name="EncapsulatedTimeStamp" type="EncapsulatedPKIDataType"/>
            <xsd:element name="XMLTimeStamp" type="AnyType"/>
        </xsd:choice>
    </xsd:sequence>
   ...
   ...

<xsd:complexType name="OtherTimeStampType">
    <xsd:complexContent>
        <xsd:restriction base="GenericTimeStampType">
            <xsd:sequence>
                ...
                ...
                <xsd:choice>  <!-- maxOccurs=1 -->
                    <xsd:element name="EncapsulatedTimeStamp" type="EncapsulatedPKIDataType"/>
                    <xsd:element name="XMLTimeStamp" type="AnyType"/>
                </xsd:choice>
            </xsd:sequence>

One way would be to simply keep the element as a list in the subclass, xjc (java) completely ignores the subclass fields by simple generating an empty class extending the base class. I ''ll have to think about it and run a few tests.

@nmrtv
Copy link
Contributor Author
nmrtv commented Apr 16, 2021

Yes, these rare cases with restrictions smell. My intuition says, that making a whole new python class without inheritance will cause fewer problems. Of course, you can't reuse code from the base class, but doing so would be dangerous anyway. Liskov violation etc.

@tefra tefra mentioned this issue May 3, 2021
tefra added a commit that referenced this issue May 4, 2021
tefra added a commit that referenced this issue May 4, 2021
@tefra tefra closed this as completed in 8f69f20 May 4, 2021
@tefra
Copy link
Owner
tefra commented May 4, 2021

Hi @neriusmika

Thanks for reporting, this issue triggered a series of some very important updates 🚀

I have added the UBL collection in the samples repo it now passes all mypy checks.

@nmrtv
Copy link
Contributor 5F7D Author
nmrtv commented May 4, 2021

Good news, thank you 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0