Skip to content

pyodata 1.8.0 - Error if Edm.DateTimeOffset is not set #195

Description

@metaodi

This issue is based on an issue I got on a library that uses pyodata (metaodi/swissparlpy#17)

Some calls fail when pyodata tries to parse dates:

---------------------------------------------------------------------------ValueErrorTraceback (mostrecentcalllast)
/opt/tljh/user/lib/python3.7/site-packages/pyodata/v2/model.pyinparse_datetime_literal(value)
388try:
-->389returndatetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
390exceptValueError:
/opt/tljh/user/lib/python3.7/_strptime.pyin_strptime_datetime(cls, data_string, format)
576formatstring."""--> 577 tt, fraction, gmtoff_fraction = _strptime(data_string, format) 578 tzname, gmtoff = tt[-2:]/opt/tljh/user/lib/python3.7/_strptime.py in _strptime(data_string, format) 358 raise ValueError("time data %r does not match format %r"%-->359 (data_string, format))
360iflen(data_string) !=found.end():
ValueError: timedata'0000-00-00T00:00:00'doesnotmatchformat'%Y-%m-%dT%H:%M:%S.%f'

And the trace is the following:

/opt/tljh/user/lib/python3.7/site-packages/swissparlpy/client.pyin__init__(self, entity_request, variables)
59classSwissParlResponse(object):
60def__init__(self, entity_request, variables):
--->61self.entities=entity_request.execute()
62self.count=self.entities.total_count63self.variables=variables/opt/tljh/user/lib/python3.7/site-packages/pyodata/v2/service.pyinexecute(self)
326self._logger.debug(' body: <cannot be decoded>')
327-->328returnself._handler(response)
329330defcustom(self, name, value):
/opt/tljh/user/lib/python3.7/site-packages/pyodata/v2/service.pyinget_entities_handler(response)
1407result=ListWithTotalCount(total_count)
1408forpropsinentities:
->1409entity=EntityProxy(self._service, self._entity_set, self._entity_set.entity_type, props)
1410result.append(entity)
1411/opt/tljh/user/lib/python3.7/site-packages/pyodata/v2/service.pyin__init__(self, service, entity_set, entity_type, proprties, entity_key, etag)
766else:
767# null value is in literal form for now, convert it to python representation-->768self._cache[type_proprty.name] =type_proprty.from_literal(type_proprty.typ.null_value)
769770# then, assign all navigation properties/opt/tljh/user/lib/python3.7/site-packages/pyodata/v2/model.pyinfrom_literal(self, value)
837returnNone838-->839returnself.typ.traits.from_literal(value)
840841defto_literal(self, value):
/opt/tljh/user/lib/python3.7/site-packages/pyodata/v2/model.pyinfrom_literal(self, value)
554tz_sign=-1ifmatch.group('sign') =='-'else1555tz_info=datetime.timezone(tz_sign*tz_offset)
-->556returnparse_datetime_literal(datetime_part).replace(tzinfo=tz_info)
557except (ValueError, AttributeError):
558raisePyODataModelError(f'Cannot decode datetimeoffset from value {value}.')
/opt/tljh/user/lib/python3.7/site-packages/pyodata/v2/model.pyinparse_datetime_literal(value)
395returndatetime.datetime.strptime(value, '%Y-%m-%dT%H:%M')
396exceptValueError:
-->397raisePyODataModelError(f'Cannot decode datetime from value {value}.')
398399PyODataModelError: Cannotdecodedatetimefromvalue0000-00-00T00:00:00.

I'm not 100% I understand everything that happens, but to me it seems, that in pyodata/v2/service.py (Line 768) the defined null_value is passed to from_literal and the null_value of Edm.DateTimeOffset is set to 'datetimeoffset\'0000-00-00T00:00:00Z\''

The new code introduced in #184 doesn't seem to check for this case.

My current workaround is this to monkey patch this:

defpatched_parse_datetime_literal(value):
print(value)
ifvalue=='0000-00-00T00:00:00':
returndatetime.datetime(1970, 1, 1)
try:
returndatetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')
exceptValueError:
try:
returndatetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
exceptValueError:
try:
returndatetime.datetime.strptime(value, '%Y-%m-%dT%H:%M')
exceptValueError:
raisePyODataModelError(f'Cannot decode datetime from value {value}.')
pyodata.v2.model.parse_datetime_literal=patched_parse_datetime_literal

But maybe this is not the best place to fix this issue.

cc @rettichschnidi

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions