Skip to content

TypeError exception when calling service operation with a default attribute for parameter of type dateTime  #395

@joahim

Description

@joahim

When calling a service operation that has a parameter of type dateTime with the default value set in the WSDL, the following exception is raised in:

https://github.com/mvantellingen/python-zeep/blob/master/src/zeep/xsd/types/builtins.py#L129

TypeError: combine() argument 1 must be datetime.date, not str

The WSDL definition (excerpt):

<s:element name="ItemList">
  <s:complexType>
    <s:sequence>
      <s:element minOccurs="0" maxOccurs="1" default="1899-12-30T00:00:00" name="DateFrom" type="s:dateTime"/>
      <s:element minOccurs="0" maxOccurs="1" default="1899-12-30T00:00:00" name="DateTo" type="s:dateTime"/>
    </s:sequence>
  </s:complexType>
</s:element>

Since the accepted_types = (datetime.datetime,) + six.string_types for DateTime a possible solution would be to simply return the value unchanged if it's of type str in:

https://github.com/mvantellingen/python-zeep/blob/master/src/zeep/xsd/types/builtins.py#L120

class DateTime(BuiltinType, AnySimpleType):
    _default_qname = xsd_ns('dateTime')
    accepted_types = (datetime.datetime,) + six.string_types

    @check_no_collection
    def xmlvalue(self, value):

        if isinstance(value, str):
            return value

        # Bit of a hack, since datetime is a subclass of date we can't just
        # test it with an isinstance(). And actually, we should not really
        # care about the type, as long as it has the required attributes
        if not all(hasattr(value, attr) for attr in ('hour', 'minute', 'second')):
            value = datetime.datetime.combine(value, datetime.time(
                getattr(value, 'hour', 0),
                getattr(value, 'minute', 0),
                getattr(value, 'second', 0)))

        if getattr(value, 'microsecond', 0):
            return isodate.isostrf.strftime(value, '%Y-%m-%dT%H:%M:%S.%f%Z')
        return isodate.isostrf.strftime(value, '%Y-%m-%dT%H:%M:%S%Z')

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions