codeant-ai-for-open-source[bot] commented on code in PR #41028:
URL: https://github.com/apache/superset/pull/41028#discussion_r3607276745


##########
superset/utils/number_format.py:
##########
@@ -0,0 +1,354 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Server-side port of the d3-format based number and currency formatters used by
+the Table and Pivot Table chart plugins.
+
+Report notifications that embed a chart as text build the table in Python and
+have no access to the frontend formatters, so chart number/currency format
+configuration has to be reproduced here to render the same values an end user
+sees in the browser.
+
+Only d3-format specifiers (and the ``SMART_NUMBER`` pseudo-formats) are ported.
+Non-d3 presets such as ``DURATION``, ``DURATION_SUB`` and the ``MEMORY_*``
+formatters are not supported: they do not parse as a d3 specifier and fall back
+to the raw value, so a column using one of those renders unformatted in 
reports.
+The fill/align/zero/width specifier flags (absent from every preset, reachable
+only by hand-typed formats) are parsed but not rendered: the numeric content
+(precision, grouping, sign) is still formatted correctly, only the padding is
+omitted.
+"""
+
+from __future__ import annotations
+
+import math
+import re
+from decimal import Decimal, ROUND_HALF_UP
+from typing import Any
+
+from babel.numbers import get_currency_symbol
+
+SMART_NUMBER = "SMART_NUMBER"
+SMART_NUMBER_SIGNED = "SMART_NUMBER_SIGNED"
+AUTO_CURRENCY = "AUTO"
+
+LOCALE = "en_US"
+
+# SI prefixes keyed by their power-of-1000 exponent, mirroring d3-format.
+SI_PREFIXES = {
+    -8: "y",
+    -7: "z",
+    -6: "a",
+    -5: "f",
+    -4: "p",
+    -3: "n",
+    -2: "µ",
+    -1: "m",
+    0: "",
+    1: "k",
+    2: "M",
+    3: "G",
+    4: "T",
+    5: "P",
+    6: "E",
+    7: "Z",
+    8: "Y",
+}
+
+# d3-format specifier grammar:
+# [[fill]align][sign][symbol][0][width][,][.precision][~][type]
+D3_FORMAT_RE = re.compile(
+    r"^(?:(.)?([<>=^]))?([+\-( 
])?([$#])?(0)?(\d+)?(,)?(?:\.(\d+))?(~)?([a-z%])?$",
+    re.IGNORECASE,
+)
+
+
+def resolve_auto_currency(
+    currency: dict[str, Any], detected_currency: str | None
+) -> dict[str, Any]:
+    """
+    Resolve an ``AUTO`` currency to the code detected from the data.
+
+    Mirrors the frontend's ``resolveAutoCurrency``: without a detected currency
+    (mixed-currency data) the config stays ``AUTO``, which renders the bare
+    number.
+    """
+    if currency.get("symbol") == AUTO_CURRENCY and detected_currency:
+        return {**currency, "symbol": detected_currency}
+    return currency
+
+
+def format_number_with_config(
+    d3_format: str | None,
+    currency: dict[str, Any] | None,
+    value: Any,
+) -> Any:
+    """
+    Format ``value`` using a d3-format string and optional currency config.
+
+    :param d3_format: a d3-format specifier (e.g. ``",.2f"``) or 
``SMART_NUMBER``
+    :param currency: ``{"symbol": <ISO 4217>, "symbolPosition": 
"prefix"|"suffix"}``
+    :param value: the raw value to format
+    :return: the formatted string, or the value unchanged when it is not a
+        number that can be formatted
+    """
+    if value is None:
+        return ""
+    if isinstance(value, bool) or not isinstance(value, (int, float, Decimal)):
+        return value
+    if isinstance(value, Decimal):
+        value = float(value)
+    if math.isnan(value) or math.isinf(value):
+        return ""
+
+    try:
+        if currency and currency.get("symbol"):
+            # the frontend strips the currency symbol from the d3 format and
+            # falls back to SMART_NUMBER when no explicit format is set
+            number_format = (d3_format or SMART_NUMBER).replace("$", "")
+            formatted = format_numeric(number_format, value)
+            if currency["symbol"] == AUTO_CURRENCY:
+                return formatted
+            return apply_currency(formatted, currency)
+        if not d3_format:
+            return raw_string(value)
+        return format_numeric(d3_format, value)
+    except Exception:  # pylint: disable=broad-except  # noqa: BLE001
+        # never let an unexpected value break a whole report table
+        return raw_string(value)
+
+
+def format_numeric(d3_format: str, value: float) -> str:
+    """
+    Format ``value`` according to a d3 number format.
+
+    Delegates to the smart-number formatter for the ``SMART_NUMBER`` and
+    ``SMART_NUMBER_SIGNED`` pseudo-formats, and to the d3 specifier parser for
+    every other format string.
+    """
+    if d3_format in (SMART_NUMBER, SMART_NUMBER_SIGNED):
+        return format_smart_number(value, signed=d3_format == 
SMART_NUMBER_SIGNED)
+    return format_d3(d3_format, value)
+
+
+def format_d3(d3_format: str, value: float) -> str:
+    """
+    Format ``value`` with a d3-format specifier.
+
+    Supports the subset of the specifier grammar the Table/Pivot plugins emit:
+    the ``+ - ( space`` sign modes, the ``$`` currency prefix, the ``,`` group
+    separator, ``.precision``, the ``~`` trim flag, and the ``s`` (SI), ``r``
+    (significant), ``d`` (integer), ``f``/``e``/``g``/``%`` numeric types.
+    Returns the formatted string and raises ``ValueError`` for an unparseable
+    specifier.
+    """
+    match = D3_FORMAT_RE.match(d3_format)
+    if not match:
+        raise ValueError(d3_format)
+
+    sign_mode = match.group(3) or "-"
+    currency_symbol = match.group(4) == "$"
+    comma = "," if match.group(7) else ""
+    precision = int(match.group(8)) if match.group(8) is not None else None
+    trim = bool(match.group(9))
+    type_ = (match.group(10) or "").lower()
+
+    magnitude = abs(value)
+    if type_ == "s":
+        formatted = format_si(
+            magnitude, precision if precision is not None else 6, trim
+        )
+    elif type_ == "r":
+        formatted = format_significant(
+            magnitude, precision if precision is not None else 6, trim, comma
+        )
+    elif type_ == "" and precision is None:
+        formatted = format_default(magnitude, comma)
+    else:
+        if type_ == "d":
+            formatted = format(int(quantize_half_up(magnitude, 0)), 
f"{comma}d")
+        elif type_ in ("f", "%") and precision is not None:
+            scaled = magnitude * 100 if type_ == "%" else magnitude
+            suffix = "%" if type_ == "%" else ""
+            rounded = quantize_half_up(scaled, precision)
+            formatted = format(rounded, f"{comma}.{precision}f") + suffix
+        else:
+            decimals = f".{precision}" if precision is not None else ""
+            formatted = format(magnitude, f"{comma}{decimals}{type_}")
+            formatted = normalize_exponent(formatted)
+        formatted = trim_trailing_zeros(formatted) if trim else formatted
+
+    if currency_symbol:
+        formatted = f"${formatted}"
+    return apply_sign(formatted, value, sign_mode)
+
+
+def apply_sign(formatted: str, value: float, sign_mode: str) -> str:
+    """
+    Decorate a formatted magnitude with the d3 sign mode.
+
+    Negative values get a leading ``-`` (or wrapping parentheses for the ``(``
+    accounting mode); positive values get a ``+`` or a leading space only for 
the
+    ``+`` and space modes respectively.
+    """
+    if value < 0:
+        return f"({formatted})" if sign_mode == "(" else f"-{formatted}"
+    if sign_mode == "+":
+        return f"+{formatted}"
+    if sign_mode == " ":
+        return f" {formatted}"
+    return formatted
+
+
+def format_default(value: float, comma: str) -> str:
+    """
+    Format ``value`` the way d3's default (no-type) specifier does.
+
+    Mirrors JavaScript's ``Number.toString``: the shortest decimal 
representation
+    with optional grouping and no trailing zeros, so whole-valued floats render
+    without a spurious ``.0`` (``4725.0`` -> ``4,725``) and small values stay 
in
+    fixed-point notation (``0.00005`` rather than ``5e-5``).
+    """
+    formatted = format(Decimal(repr(value)), f"{comma}f")
+    return trim_trailing_zeros(formatted)
+
+
+def format_smart_number(value: float, signed: bool = False) -> str:
+    """
+    Format ``value`` the way the frontend ``SMART_NUMBER`` formatter does.
+
+    The notation is chosen by magnitude: SI prefixes (with ``G`` shown as 
``B``)
+    for ``abs(value) >= 1000``, two decimals down to ``1``, four decimals down 
to
+    ``0.001``, a micro (``µ``) suffix down to ``1e-6``, and SI prefixes again
+    below that. When ``signed`` is set, positive values are prefixed with 
``+``.
+    """
+    if value == 0:
+        body = "0"
+    else:
+        absolute = abs(value)
+        if absolute >= 1000:
+            body = format_si(value, 3, trim=True, billions=True)
+        elif absolute >= 1:
+            body = trim_trailing_zeros(format(value, ".2f"))
+        elif absolute >= 0.001:
+            body = trim_trailing_zeros(format(value, ".4f"))
+        elif absolute > 0.000001:
+            body = format_si(value * 1000000, 3, trim=True) + "µ"
+        else:
+            body = format_si(value, 3, trim=True)
+    prefix = "+" if signed and value > 0 else ""
+    return prefix + body
+
+
+def format_si(value: float, precision: int, trim: bool, billions: bool = 
False) -> str:
+    """
+    Format ``value`` with an SI prefix to ``precision`` significant digits.
+
+    Rounds to ``precision`` significant figures first, then scales into the
+    nearest power-of-1000 bracket (clamped to the ``y``..``Y`` range) and 
appends
+    the matching SI symbol. Rounding before the divide matches d3 and keeps
+    ``4725`` at ``4.73k`` (the inexact ``4.725`` mantissa would round to
+    ``4.72k``), and lets a value that rounds up into the next bracket pick the
+    right symbol (``999.5k`` -> ``1M``). With ``billions`` set, the ``G`` 
(giga)
+    symbol is rendered as ``B``.
+    """
+    if value == 0:
+        return format_significant(0.0, precision, trim)
+
+    rounded = round_to_significant(value, precision)
+    exponent = max(-8, min(8, math.floor(math.log10(abs(rounded))) // 3))
+    mantissa = rounded / (10 ** (exponent * 3))
+
+    symbol = SI_PREFIXES[exponent]
+    if billions and symbol == "G":
+        symbol = "B"
+
+    return format_significant(mantissa, precision, trim) + symbol
+
+
+def format_significant(
+    value: float, precision: int, trim: bool, comma: str = ""
+) -> str:
+    """
+    Format to `precision` significant digits in fixed-point notation.
+
+    Serves both the d3 `r` type and SI mantissas, and avoids the scientific
+    notation Python's `g` would switch to.
+    """
+    rounded = round_to_significant(value, precision)
+    decimals = decimals_for_significant(rounded, precision)
+    formatted = format(rounded, f"{comma}.{decimals}f")
+    return trim_trailing_zeros(formatted) if trim else formatted
+
+
+def round_to_significant(value: float, precision: int) -> float:
+    """
+    Round ``value`` to ``precision`` significant digits.
+
+    The number of decimal places to keep is derived from the value's order of
+    magnitude (``precision - 1 - floor(log10(abs(value)))``) and the rounding 
is
+    half away from zero, matching d3-format.
+    """
+    if value == 0:
+        return 0.0
+    return float(
+        quantize_half_up(value, precision - 1 - 
math.floor(math.log10(abs(value))))
+    )
+
+
+def quantize_half_up(value: float, decimals: int) -> Decimal:
+    """
+    Round to `decimals` places, half away from zero, matching d3-format.
+
+    Quantizes the binary float value (not its decimal string) so the result
+    matches d3, which rounds the IEEE-754 value: ``2.675`` is ``2.67`` because 
it
+    is really ``2.67499...``, while an exact ``0.125`` rounds up to ``0.13``.
+    """
+    return Decimal(value).quantize(Decimal(1).scaleb(-decimals), 
rounding=ROUND_HALF_UP)
+
+
+def decimals_for_significant(value: float, precision: int) -> int:
+    integer_digits = 1 if value == 0 else math.floor(math.log10(abs(value))) + 
1
+    return max(0, precision - integer_digits)
+
+
+def normalize_exponent(formatted: str) -> str:
+    """Drop leading zeros in a scientific exponent (1e+07 -> 1e+7), as d3 
does."""
+    return re.sub(r"([eE][+-])0*(\d)", r"\1\2", formatted)
+
+
+def apply_currency(formatted: str, currency: dict[str, Any]) -> str:
+    normalized = formatted.replace("%", "")
+    code = currency["symbol"]
+    symbol = get_currency_symbol(code, locale=LOCALE) or code
+    if currency.get("symbolPosition") == "prefix":
+        return f"{symbol} {normalized}"
+    return f"{normalized} {symbol}"

Review Comment:
   **Suggestion:** `symbolPosition` fallback behavior does not match the 
frontend contract: when the position is unset, this implementation always 
returns a suffix, but the UI resolves it from locale/currency (and defaults to 
prefix). That will render server-side report values differently from Explore 
for charts with currency symbol but no explicit position. Resolve unset 
positions the same way as frontend before formatting. [api mismatch]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Table chart post_processed JSON misplaces currency symbols.
   - ⚠️ Pivot_table_v2 reports diverge from Explore currency layout.
   - ⚠️ Alerts and Reports show inconsistent currency positioning.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Create a Table or Pivot Table chart with a metric configured with a 
currency symbol but
   no explicit `symbolPosition` (e.g. dataset metric currency only sets 
`"symbol": "USD"`),
   so the generated `form_data["currencyFormat"]` for that metric has 
`symbol="USD"` and
   `symbolPosition` unset/`null` (this configuration shape is used by the 
frontend
   `CurrencyFormatter` in
   
`superset-frontend/packages/superset-ui-core/src/currency-format/CurrencyFormatter.ts:37-41`).
   
   2. In Explore, the frontend formats currency using 
`CurrencyFormatter.format()`
   (`CurrencyFormatter.ts:113-120`), which calls 
`resolveSymbolPosition(this.currency.symbol,
   this.currency.symbolPosition, this.locale)` and `formatWithSymbolPosition`
   (`symbolPosition.ts:47-52` and `symbolPosition.ts:87-92`). When 
`symbolPosition` is unset,
   `resolveSymbolPosition` infers the position from `Intl.NumberFormat` and 
defaults to
   `'prefix'` (`symbolPosition.ts:41-45` and `symbolPosition.ts:56-81`), so 
values are
   rendered like `"$ 1,234.50"` for `USD`.
   
   3. Configure an Alert/Report that targets the same chart and requests
   `/api/v1/chart/<id>/data?type=post_processed&format=json`, which runs
   `apply_client_processing()` 
(`superset/charts/client_processing.py:148-209`). For
   `viz_type` `"table"` or `"pivot_table_v2"`, this calls the corresponding 
post-processor
   (`post_processors` map at `client_processing.py:113-116`) which in turn calls
   `format_column()` when `apply_number_format` is true 
(`client_processing.py:49-81` and
   `client_processing.py:84-110`).
   
   4. In `format_column()`, `currency` is passed through 
`resolve_auto_currency` unchanged
   for non-AUTO symbols and the column values are formatted via
   `format_number_with_config(d3_format, currency, value)` 
(`client_processing.py:5-17` and
   `client_processing.py:13-17`). Inside `format_number_with_config`
   (`number_format.py:95-132`), after numeric formatting, `apply_currency()` is 
called
   (`number_format.py:123-127`), which unconditionally treats any 
`symbolPosition` other than
   the string `"prefix"` as a suffix (`number_format.py:334-340`), producing 
`"1,234.50 $"`.
   This diverges from the frontend behavior, where the same config without 
explicit
   `symbolPosition` yields a prefix, so the same chart renders with `"$ 
1,234.50"` in Explore
   but `"1,234.50 $"` in Alert/Report emails.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=439ef30e898844f9bebdb25423788596&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=439ef30e898844f9bebdb25423788596&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/utils/number_format.py
   **Line:** 338:340
   **Comment:**
        *Api Mismatch: `symbolPosition` fallback behavior does not match the 
frontend contract: when the position is unset, this implementation always 
returns a suffix, but the UI resolves it from locale/currency (and defaults to 
prefix). That will render server-side report values differently from Explore 
for charts with currency symbol but no explicit position. Resolve unset 
positions the same way as frontend before formatting.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=e2f339dbfbb92a515272c94ea2cd2370440da75a4c75dde370846d45cbec3848&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=e2f339dbfbb92a515272c94ea2cd2370440da75a4c75dde370846d45cbec3848&reaction=dislike'>👎</a>



##########
superset/utils/number_format.py:
##########
@@ -0,0 +1,354 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Server-side port of the d3-format based number and currency formatters used by
+the Table and Pivot Table chart plugins.
+
+Report notifications that embed a chart as text build the table in Python and
+have no access to the frontend formatters, so chart number/currency format
+configuration has to be reproduced here to render the same values an end user
+sees in the browser.
+
+Only d3-format specifiers (and the ``SMART_NUMBER`` pseudo-formats) are ported.
+Non-d3 presets such as ``DURATION``, ``DURATION_SUB`` and the ``MEMORY_*``
+formatters are not supported: they do not parse as a d3 specifier and fall back
+to the raw value, so a column using one of those renders unformatted in 
reports.
+The fill/align/zero/width specifier flags (absent from every preset, reachable
+only by hand-typed formats) are parsed but not rendered: the numeric content
+(precision, grouping, sign) is still formatted correctly, only the padding is
+omitted.
+"""
+
+from __future__ import annotations
+
+import math
+import re
+from decimal import Decimal, ROUND_HALF_UP
+from typing import Any
+
+from babel.numbers import get_currency_symbol
+
+SMART_NUMBER = "SMART_NUMBER"
+SMART_NUMBER_SIGNED = "SMART_NUMBER_SIGNED"
+AUTO_CURRENCY = "AUTO"
+
+LOCALE = "en_US"
+
+# SI prefixes keyed by their power-of-1000 exponent, mirroring d3-format.
+SI_PREFIXES = {
+    -8: "y",
+    -7: "z",
+    -6: "a",
+    -5: "f",
+    -4: "p",
+    -3: "n",
+    -2: "µ",
+    -1: "m",
+    0: "",
+    1: "k",
+    2: "M",
+    3: "G",
+    4: "T",
+    5: "P",
+    6: "E",
+    7: "Z",
+    8: "Y",
+}
+
+# d3-format specifier grammar:
+# [[fill]align][sign][symbol][0][width][,][.precision][~][type]
+D3_FORMAT_RE = re.compile(
+    r"^(?:(.)?([<>=^]))?([+\-( 
])?([$#])?(0)?(\d+)?(,)?(?:\.(\d+))?(~)?([a-z%])?$",
+    re.IGNORECASE,
+)
+
+
+def resolve_auto_currency(
+    currency: dict[str, Any], detected_currency: str | None
+) -> dict[str, Any]:
+    """
+    Resolve an ``AUTO`` currency to the code detected from the data.
+
+    Mirrors the frontend's ``resolveAutoCurrency``: without a detected currency
+    (mixed-currency data) the config stays ``AUTO``, which renders the bare
+    number.
+    """
+    if currency.get("symbol") == AUTO_CURRENCY and detected_currency:
+        return {**currency, "symbol": detected_currency}
+    return currency
+
+
+def format_number_with_config(
+    d3_format: str | None,
+    currency: dict[str, Any] | None,
+    value: Any,
+) -> Any:
+    """
+    Format ``value`` using a d3-format string and optional currency config.
+
+    :param d3_format: a d3-format specifier (e.g. ``",.2f"``) or 
``SMART_NUMBER``
+    :param currency: ``{"symbol": <ISO 4217>, "symbolPosition": 
"prefix"|"suffix"}``
+    :param value: the raw value to format
+    :return: the formatted string, or the value unchanged when it is not a
+        number that can be formatted
+    """
+    if value is None:
+        return ""
+    if isinstance(value, bool) or not isinstance(value, (int, float, Decimal)):
+        return value
+    if isinstance(value, Decimal):
+        value = float(value)
+    if math.isnan(value) or math.isinf(value):
+        return ""
+
+    try:
+        if currency and currency.get("symbol"):
+            # the frontend strips the currency symbol from the d3 format and
+            # falls back to SMART_NUMBER when no explicit format is set
+            number_format = (d3_format or SMART_NUMBER).replace("$", "")
+            formatted = format_numeric(number_format, value)
+            if currency["symbol"] == AUTO_CURRENCY:
+                return formatted
+            return apply_currency(formatted, currency)
+        if not d3_format:
+            return raw_string(value)
+        return format_numeric(d3_format, value)
+    except Exception:  # pylint: disable=broad-except  # noqa: BLE001
+        # never let an unexpected value break a whole report table
+        return raw_string(value)

Review Comment:
   **Suggestion:** Any exception in the currency branch (for example an invalid 
currency code while resolving the symbol) falls through to `raw_string(value)`, 
which drops the successfully computed numeric formatting. Frontend behavior 
keeps the formatted number and only omits the symbol on currency errors, so 
this should preserve `formatted` instead of reverting to raw value text. [logic 
error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Reports with invalid currencies show unformatted numeric values.
   - ⚠️ Explore retains formatting while server reports drop it.
   - ⚠️ Inconsistent behavior between frontend and backend currency handling.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Configure a metric with an invalid or custom currency code (for example 
`"symbol":
   "FOO"`) in its `currencyFormat`, so that the chart’s `form_data` contains a 
currency dict
   like `{"symbol": "FOO", "symbolPosition": "prefix"}`; this dict is passed 
unchanged into
   backend formatting via `format_column()` in 
`superset/charts/client_processing.py:5-17`
   and `client_processing.py:99-108`.
   
   2. Trigger an Alert/Report for a Table or Pivot Table chart so that
   `/api/v1/chart/<id>/data?type=post_processed&format=json` is called.
   `apply_client_processing()` (`client_processing.py:148-209`) selects the 
`table` or
   `pivot_table_v2` post-processor (`client_processing.py:113-116`), which calls
   `format_column()` to apply number and currency formats 
(`client_processing.py:5-17`,
   `client_processing.py:71-80`, and `client_processing.py:99-108`).
   
   3. In `format_column()`, the invalid currency code is passed into
   `format_number_with_config(d3_format, currency, value)` 
(`client_processing.py:13-17`).
   Inside `format_number_with_config` 
(`superset/utils/number_format.py:95-132`), a properly
   formatted numeric string is first produced via `formatted = 
format_numeric(number_format,
   value)` (`number_format.py:122-123`), but because `currency["symbol"]` is 
not `AUTO`, the
   function then calls `apply_currency(formatted, currency)` 
(`number_format.py:124-126`).
   
   4. `apply_currency()` uses Babel’s `get_currency_symbol(code, locale=LOCALE)`
   (`number_format.py:334-337`). For an invalid `code`, `get_currency_symbol` 
can raise an
   exception, which propagates back into `format_number_with_config`. Because 
the entire
   formatting logic is wrapped in a single broad `try` 
(`number_format.py:118-130`), this
   exception triggers the `except Exception` block, causing the function to 
return
   `raw_string(value)` (`number_format.py:130-132`). The result is that the 
formatted number
   (e.g. `"1,234.50"`) is discarded and replaced by an unformatted raw numeric 
string (e.g.
   `"1234.5"`), whereas the frontend `CurrencyFormatter.format`
   (`CurrencyFormatter.ts:77-91`) explicitly catches currency errors and 
returns the already
   formatted number without a symbol instead of falling back to raw values.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=127b3269bafa420185ff13a6ac4cdf9f&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=127b3269bafa420185ff13a6ac4cdf9f&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/utils/number_format.py
   **Line:** 123:132
   **Comment:**
        *Logic Error: Any exception in the currency branch (for example an 
invalid currency code while resolving the symbol) falls through to 
`raw_string(value)`, which drops the successfully computed numeric formatting. 
Frontend behavior keeps the formatted number and only omits the symbol on 
currency errors, so this should preserve `formatted` instead of reverting to 
raw value text.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=aebabcd4682802fa3595ac62e0744776e53f1abd9af8ac24eb26eb827674cff3&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=aebabcd4682802fa3595ac62e0744776e53f1abd9af8ac24eb26eb827674cff3&reaction=dislike'>👎</a>



-- 
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]

Reply via email to