pabigot / pyxb

Python XML Schema Bindings
Apache License 2.0
130 stars 75 forks source link

nil-ed values should return None. #98

Closed conqp closed 6 years ago

conqp commented 6 years ago

Currently e.g. a nilable decimal field with value xsd:nil will return Decimal('0'). It'd be more convenient (for me) if it returned None instead.

>>> type(decimal_field)
<class 'laglerlib.dom.expose.posDecimal'>
>>> decimal_field
Decimal('0')    # Should imho be None.
>>> decimal_field._isNil()
1
>>> decimal_field.toxml()
'<?xml version="1.0" ?><ns1:heizkosten xmlns:ns1="http://www.immopool.de/expose" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></ns1:heizkosten>'
pabigot commented 6 years ago

I don't believe it's possible to have an instance of Decimal that has a value None:

>>> import decimal
>>> x = decimal.Decimal()
>>> x
Decimal('0')
>>> 

If the value of the reference in an expression were None then it would not allow methods such as toxml().

Generally speaking the concept of a typed nullable value doesn't really fit with Python's data structures. I think PyXB's doing the only thing it can do here.

apexo commented 6 years ago

The API could be changed to, e.g.:

>>> type(struct.decimal_field)
None
>>> struct.isNil.decimal_field()
True
>>> struct.toxml.decimal_field()
<element xsi:nil=true ...

Guess this is is a bit out of scope for the current state of pyxb.