mahmoud / glom

☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
https://glom.readthedocs.io
Other
1.88k stars 61 forks source link

`skip` option for Coalesce doesn't seems to work #266

Closed antoine-gallix closed 11 months ago

antoine-gallix commented 11 months ago

Below a snippet of code where I convert variables to float. I want to skip trying to convert empty strings and have the default value instead. But I can't seem to make the skip option work.

from glom import Coalesce, glom
glom( [1, "1", ""], [Coalesce(float, default=None, skip="")])

# GlomError.wrap(ValueError): error raised while processing, details below.
#  Target-spec trace (most recent last):
#  - Target: [1, '1', '']
#  - Spec: [Coalesce(float, default=None, skip='')]
#  - Target: ''
#  - Spec: Coalesce(float, default=None, skip='')
#  - Spec: float
#            ^^^^^^^^^^^^
# ValueError: could not convert string to float: ''

My expected result would be [1, 1, None]

Am I understanding something wrong with how to use skip? The documentation is unclear.

antoine-gallix commented 11 months ago

I get the same error with skip=("",) and skip=lambda v:v==""

mahmoud commented 11 months ago

Hey @antoine-gallix! So skip is actually checked against the value after evaluating the spec passed to Coalesce (float, in this case). Depending on your use case, you might want to do skip_exc=ValueError to skip un-float-ables. Hope this helps!