devansh173 opened a new issue, #44614:
URL: https://github.com/apache/superset/issues/44614
### Bug description
`superset.utils.core.cast_to_num` is documented to return `None` when a
value can't be converted to a number, but it raises an uncaught `ValueError`
for strings made of characters that `str.isdigit()` accepts but `int()` does
not (for example superscript digits such as `"²"`).
```python
>>> from superset.utils.core import cast_to_num
>>> "²".isdigit()
True
>>> cast_to_num("²")
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: '²'
```
The function checks `value.isdigit()` and then calls `int(value)`.
`isdigit()` is also true for characters in the Unicode "Digit" category that
are not decimal digits (`²`, `³`, `①`, ...), and `int()` rejects those. The
`float()` fallback that would have returned `None` is never reached, because
the `ValueError` is raised outside the `try` block.
`cast_to_num` is used when building filter values for numeric columns
(`superset/models/helpers.py`, `SqlaTable.filter_values_handler` and the
array-length filter), so a stray character like this in a filter value ends up
as an unhandled exception instead of the value being treated as non-numeric.
### Expected behavior
`cast_to_num("²")` returns `None`, like any other string that is not a
number (e.g. `cast_to_num("this is not a string") is None`).
### Actual behavior
`ValueError` is raised.
### Suggested fix
Use `str.isdecimal()` instead of `str.isdigit()` for the integer fast path,
so anything that isn't a plain decimal number falls through to the existing
`try: float(value) / except ValueError: return None`.
### Environment
- Superset version: master
- Python version: 3.11
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]