I recently had an issue when converting a field from IntField to FloatField. The default value for the field is None and when we tried to use FloatField we were getting a TypeError: float() argument must be a string or a number, not 'NoneType'.
I've tracked it down to this line of code only catching ValueError exceptions while in IntField it also catches the TypeError.
As a temporary workaround I've created my own class that also catches the TypeError and allows me to use None as a value for floats:
class CustomFloatField(FloatField):
"""Floating point number field. Added TypeError check to allow NoneType values"""
def to_python(self, value):
try:
value = super().to_python(value)
except TypeError:
pass
return value
I'm posting this issue to ask if there is a specific reason behind FloatFields not accepting None values because then my custom field might cause me issues down the road or was it just an oversight?
If there isn't a specific reason I'd be happy to contribute a PR that adds it.
Mongoengine version: 0.19.1
Python version: 3.7.5
I recently had an issue when converting a field from IntField to FloatField. The default value for the field is None and when we tried to use FloatField we were getting a
TypeError: float() argument must be a string or a number, not 'NoneType'.I've tracked it down to this line of code only catching ValueError exceptions while in IntField it also catches the TypeError.
As a temporary workaround I've created my own class that also catches the TypeError and allows me to use None as a value for floats:
I'm posting this issue to ask if there is a specific reason behind FloatFields not accepting None values because then my custom field might cause me issues down the road or was it just an oversight?
If there isn't a specific reason I'd be happy to contribute a PR that adds it.
Mongoengine version: 0.19.1
Python version: 3.7.5