Skip to content

ListField(FloatField) does not allow NoneType values #2290

Description

@Adam-Edry

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions