SEPURI-SAI-KRISHNA commented on code in PR #45439:
URL: https://github.com/apache/superset/pull/45439#discussion_r4325700908
##########
tests/unit_tests/charts/test_client_processing.py:
##########
@@ -2501,6 +2501,293 @@ def test_table_applies_si_number_format() -> None:
assert formatted["amount"].tolist() == ["1.23k"]
+def test_table_applies_small_number_format() -> None:
+ """
+ A cell below one in absolute value takes the column's small-number format.
+
+ ``formatColumnValue`` in the Table plugin picks the formatter per value, so
+ the report has to switch per cell rather than per column.
+ """
+ df = pd.DataFrame.from_dict({"ratio": {0: 0.0523, 1: 12.5, 2: -0.5, 3:
0.0}})
+ form_data = {
+ "viz_type": "table",
+ "metrics": ["ratio"],
+ "column_config": {
+ "ratio": {"d3NumberFormat": ",.2f", "d3SmallNumberFormat": ".1%"}
+ },
+ }
+ formatted = table(df, form_data)
+ assert formatted["ratio"].tolist() == ["5.2%", "12.50", "-50.0%", "0.0%"]
+
Review Comment:
Confirmed, and both slips survive the suite as it stands. I ran each of them
against every data set the small-number tests use:
```
0.0523 / 12.5 / -0.5 / 0.0 original, <= 1, no abs() -> all three
identical
0.0523 / 1200.0 original, <= 1, no abs() -> all three
identical
0.5 / 12.5 original, <= 1, no abs() -> all three
identical
```
Your row separates all three:
```
1, -1, -12.5
original -> ['1.00', '-1.00', '-12.50']
<= 1 -> ['100.0%', '-100.0%', '-12.50']
no abs() -> ['1.00', '-100.0%', '-1250.0%']
```
Added all three to `test_table_applies_small_number_format` with the values
you
gave, and both mutations now fail on it.
While I was in there I checked the rest of the predicate the same way, and
the
other three conditions turn out not to be observable:
```python
if isinstance(value, bool) or not isinstance(value, (int, float,
Decimal)):
return False
number = float(value)
return not math.isnan(number) and not math.isinf(number) and abs(number)
< 1
```
`abs(nan) < 1` and `abs(inf) < 1` are already `False`, so the two `math`
calls
decide nothing. And `format_number_with_config` passes a boolean through
unchanged on both paths, so the `bool` branch only picks which formatter
hands
it back untouched. Keeping the `isinstance` type check and dropping the other
three, so the body reads
```python
if not isinstance(value, (int, float, Decimal)):
return False
return abs(float(value)) < 1
```
leaves a bool column as `[True, False]` and `nan`/`inf` as `''`, and the
file's
122 tests pass. The type check itself does carry weight: a nullable `Int64`
column is `is_numeric_dtype`, so `pd.NA` reaches here and `float(pd.NA)`
would
raise `TypeError` without it. That column formats as `['1.00', '', '0.0%']`
today.
I left all three in, since the `bool` check is what mirrors the browser's
`typeof value === 'number'` and would start mattering if that pass-through
ever
changed, but say the word if you would rather see the two `math` calls go.
--
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]