codeant-ai-for-open-source[bot] commented on code in PR #41028: URL: https://github.com/apache/superset/pull/41028#discussion_r3505438562
########## superset/utils/number_format.py: ########## @@ -0,0 +1,333 @@ +# 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. +""" + +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" Review Comment: **Suggestion:** Add explicit type annotations to the newly introduced module-level string constants so they comply with the type-hint requirement. [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> These newly introduced module-level string constants are unannotated, and the custom rule requires type hints for new Python variables that can be annotated. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b1394ced7aa943978fa33f92a8a80f01&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b1394ced7aa943978fa33f92a8a80f01&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:** 40:45 **Comment:** *Custom Rule: Add explicit type annotations to the newly introduced module-level string constants so they comply with the type-hint requirement. 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=19f75c6c55d4e7c19be9976cc778d23dd71314ee5784a053b940e7c5ae70ffc0&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=19f75c6c55d4e7c19be9976cc778d23dd71314ee5784a053b940e7c5ae70ffc0&reaction=dislike'>๐</a> ########## superset/utils/number_format.py: ########## @@ -0,0 +1,333 @@ +# 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. +""" + +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", +} Review Comment: **Suggestion:** Add a concrete type annotation to this module-level mapping to satisfy the rule for annotatable variables. [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This module-level mapping is newly added without a type annotation, which violates the rule requiring type hints on annotatable Python variables. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=dd65509b70a54933b18e05b13126fd28&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=dd65509b70a54933b18e05b13126fd28&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:** 47:66 **Comment:** *Custom Rule: Add a concrete type annotation to this module-level mapping to satisfy the rule for annotatable variables. 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=e078fc8f7409cdf8abda026a28966dec92fb8a0f8795404cf01ca66848ccc9ca&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=e078fc8f7409cdf8abda026a28966dec92fb8a0f8795404cf01ca66848ccc9ca&reaction=dislike'>๐</a> ########## tests/unit_tests/charts/test_client_processing.py: ########## @@ -1846,6 +1853,125 @@ def test_table(): ) +def test_table_applies_currency_format(): + """ + Table reports honor a column's `currencyFormat`. + """ + df = pd.DataFrame.from_dict({"amount": {0: 1234.5}}) + form_data = { + "viz_type": "table", + "column_config": { + "amount": { + "d3NumberFormat": ",.2f", + "currencyFormat": {"symbol": "USD", "symbolPosition": "prefix"}, + } + }, + } + formatted = table(df, form_data) + assert formatted["amount"].tolist() == ["$ 1,234.50"] + + +def test_table_applies_si_number_format(): + """ + Table reports honor d3 formats that Python's str.format cannot express. + """ + df = pd.DataFrame.from_dict({"amount": {0: 1234.0}}) + form_data = { + "viz_type": "table", + "column_config": {"amount": {"d3NumberFormat": ".3s"}}, + } + formatted = table(df, form_data) + assert formatted["amount"].tolist() == ["1.23k"] + + +def test_pivot_table_v2_applies_value_format(): Review Comment: **Suggestion:** Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> The function is new and currently unannotated. The rule explicitly flags new Python code that omits type hints on functions, so this matches the violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=28e525da28d248a7a4b30299ae64dbdb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=28e525da28d248a7a4b30299ae64dbdb&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:** tests/unit_tests/charts/test_client_processing.py **Line:** 1887:1887 **Comment:** *Custom Rule: Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). 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=daaf37d0075541c8961f3d01c34d7035d0c038edd167ae9874b5acdc58effce6&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=daaf37d0075541c8961f3d01c34d7035d0c038edd167ae9874b5acdc58effce6&reaction=dislike'>๐</a> ########## tests/unit_tests/charts/test_client_processing.py: ########## @@ -1846,6 +1853,125 @@ def test_table(): ) +def test_table_applies_currency_format(): + """ + Table reports honor a column's `currencyFormat`. + """ + df = pd.DataFrame.from_dict({"amount": {0: 1234.5}}) + form_data = { + "viz_type": "table", + "column_config": { + "amount": { + "d3NumberFormat": ",.2f", + "currencyFormat": {"symbol": "USD", "symbolPosition": "prefix"}, + } + }, + } + formatted = table(df, form_data) + assert formatted["amount"].tolist() == ["$ 1,234.50"] + + +def test_table_applies_si_number_format(): Review Comment: **Suggestion:** Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This newly added test function has no return type annotation. Under the Python type-hint rule, it should be annotated (e.g. `-> None`). </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=3a0237c2e85149e5942af36092784590&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=3a0237c2e85149e5942af36092784590&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:** tests/unit_tests/charts/test_client_processing.py **Line:** 1874:1874 **Comment:** *Custom Rule: Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). 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=4eab475cbecd00ae9841ca8582d61eed9dac5d9e3e635e567fd329aaa6260b69&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=4eab475cbecd00ae9841ca8582d61eed9dac5d9e3e635e567fd329aaa6260b69&reaction=dislike'>๐</a> ########## tests/unit_tests/charts/test_client_processing.py: ########## @@ -1846,6 +1853,125 @@ def test_table(): ) +def test_table_applies_currency_format(): + """ + Table reports honor a column's `currencyFormat`. + """ + df = pd.DataFrame.from_dict({"amount": {0: 1234.5}}) + form_data = { + "viz_type": "table", + "column_config": { + "amount": { + "d3NumberFormat": ",.2f", + "currencyFormat": {"symbol": "USD", "symbolPosition": "prefix"}, + } + }, + } + formatted = table(df, form_data) + assert formatted["amount"].tolist() == ["$ 1,234.50"] + + +def test_table_applies_si_number_format(): + """ + Table reports honor d3 formats that Python's str.format cannot express. + """ + df = pd.DataFrame.from_dict({"amount": {0: 1234.0}}) + form_data = { + "viz_type": "table", + "column_config": {"amount": {"d3NumberFormat": ".3s"}}, + } + formatted = table(df, form_data) + assert formatted["amount"].tolist() == ["1.23k"] + + +def test_pivot_table_v2_applies_value_format(): + """ + Pivot table reports honor `valueFormat` and per-metric `columnFormats`. + """ + df = pd.DataFrame( + {"region": ["A", "B"], "sales": [1234.5, 6789.0], "qty": [10.0, 20.0]} + ) + form_data = { + "viz_type": "pivot_table_v2", + "groupbyRows": ["region"], + "groupbyColumns": [], + "metrics": ["sales", "qty"], + "aggregateFunction": "Sum", + "metricsLayout": "COLUMNS", + "valueFormat": ",.2f", + "columnFormats": {"qty": ",d"}, + } + formatted = pivot_table_v2(df, form_data) + assert formatted[("sales",)].tolist() == ["1,234.50", "6,789.00"] + assert formatted[("qty",)].tolist() == ["10", "20"] + + +def test_pivot_table_v2_applies_per_metric_format_when_metrics_combined(): Review Comment: **Suggestion:** Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This test definition lacks a type hint on the function signature. Since it is newly introduced Python code, the rule requires an explicit annotation such as `-> None`. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=67aef1f534774d8f8bdfffd91f84051c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=67aef1f534774d8f8bdfffd91f84051c&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:** tests/unit_tests/charts/test_client_processing.py **Line:** 1909:1909 **Comment:** *Custom Rule: Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). 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=7ab7c9ccac4484372815041624d339be10b8e27f4e5d6c01521062b5131b9edf&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=7ab7c9ccac4484372815041624d339be10b8e27f4e5d6c01521062b5131b9edf&reaction=dislike'>๐</a> ########## tests/unit_tests/utils/number_format_test.py: ########## @@ -0,0 +1,283 @@ +# 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. +import pytest + +from superset.utils.number_format import format_number_with_config + +# --- Helper behaviour the d3 parity matrix below cannot cover ---------------- + + [email protected]( + "d3_format,value,expected", + [ + # SMART_NUMBER is a Superset formatter (adaptive SI, half-up), not raw d3 + ("SMART_NUMBER", 4725, "4.73k"), + ("SMART_NUMBER", 80679663, "80.7M"), + ("SMART_NUMBER", 1234567890, "1.23B"), + ("SMART_NUMBER", 0, "0"), + (".2~f", 1200.0, "1200"), + (None, 42, "42"), + ("not-a-real-format!!", 42, "42"), + ], +) +def test_format_number(d3_format, value, expected): + assert format_number_with_config(d3_format, None, value) == expected + + [email protected]( + "currency,value,expected", + [ + ({"symbol": "USD", "symbolPosition": "prefix"}, 1234.5, "$ 1,234.50"), + ({"symbol": "EUR", "symbolPosition": "suffix"}, 1234.5, "1,234.50 โฌ"), + # unknown symbolPosition defaults to suffix, mirroring the frontend + ({"symbol": "BRL", "symbolPosition": None}, 1234.5, "1,234.50 R$"), + ], +) +def test_format_number_with_currency(currency, value, expected): + assert format_number_with_config(",.2f", currency, value) == expected Review Comment: **Suggestion:** Add explicit type annotations to the `currency`, `value`, and `expected` parameters and declare the return type. [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This function is newly added and lacks annotations for its parameters and return type, so it matches the custom rule requiring type hints in Python code. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=3783612b878140f8827af06a96390144&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=3783612b878140f8827af06a96390144&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:** tests/unit_tests/utils/number_format_test.py **Line:** 50:51 **Comment:** *Custom Rule: Add explicit type annotations to the `currency`, `value`, and `expected` parameters and declare the return type. 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=021d7951507a3073635e00f1e63bbcee1a8e7d00f487f881b9acab9b7c289a8c&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=021d7951507a3073635e00f1e63bbcee1a8e7d00f487f881b9acab9b7c289a8c&reaction=dislike'>๐</a> ########## tests/unit_tests/charts/test_client_processing.py: ########## @@ -1846,6 +1853,125 @@ def test_table(): ) +def test_table_applies_currency_format(): + """ + Table reports honor a column's `currencyFormat`. + """ + df = pd.DataFrame.from_dict({"amount": {0: 1234.5}}) + form_data = { + "viz_type": "table", + "column_config": { + "amount": { + "d3NumberFormat": ",.2f", + "currencyFormat": {"symbol": "USD", "symbolPosition": "prefix"}, + } + }, + } + formatted = table(df, form_data) + assert formatted["amount"].tolist() == ["$ 1,234.50"] + + +def test_table_applies_si_number_format(): + """ + Table reports honor d3 formats that Python's str.format cannot express. + """ + df = pd.DataFrame.from_dict({"amount": {0: 1234.0}}) + form_data = { + "viz_type": "table", + "column_config": {"amount": {"d3NumberFormat": ".3s"}}, + } + formatted = table(df, form_data) + assert formatted["amount"].tolist() == ["1.23k"] + + +def test_pivot_table_v2_applies_value_format(): + """ + Pivot table reports honor `valueFormat` and per-metric `columnFormats`. + """ + df = pd.DataFrame( + {"region": ["A", "B"], "sales": [1234.5, 6789.0], "qty": [10.0, 20.0]} + ) + form_data = { + "viz_type": "pivot_table_v2", + "groupbyRows": ["region"], + "groupbyColumns": [], + "metrics": ["sales", "qty"], + "aggregateFunction": "Sum", + "metricsLayout": "COLUMNS", + "valueFormat": ",.2f", + "columnFormats": {"qty": ",d"}, + } + formatted = pivot_table_v2(df, form_data) + assert formatted[("sales",)].tolist() == ["1,234.50", "6,789.00"] + assert formatted[("qty",)].tolist() == ["10", "20"] + + +def test_pivot_table_v2_applies_per_metric_format_when_metrics_combined(): + """ + Per-metric formats apply when `combineMetric` moves the metric to the last + column level. + """ + df = pd.DataFrame( + { + "dept": ["A", "B"], + "region": ["x", "x"], + "sales": [100.0, 200.0], + "qty": [1111.0, 2222.0], + } + ) + form_data = { + "viz_type": "pivot_table_v2", + "groupbyRows": ["dept"], + "groupbyColumns": ["region"], + "metrics": ["sales", "qty"], + "aggregateFunction": "Sum", + "metricsLayout": "COLUMNS", + "combineMetric": True, + "valueFormat": ",.2f", + "columnFormats": {"qty": ",d"}, + } + formatted = pivot_table_v2(df, form_data) + assert formatted[("x", "qty")].tolist() == ["1,111", "2,222"] + assert formatted[("x", "sales")].tolist() == ["100.00", "200.00"] + + +def test_format_column_applies_d3_and_currency(): Review Comment: **Suggestion:** Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This newly added test function is missing an explicit return type annotation. That is a direct violation of the Python type-hint requirement. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=4db8a27cc7ac457d823bd7bb59df9450&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=4db8a27cc7ac457d823bd7bb59df9450&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:** tests/unit_tests/charts/test_client_processing.py **Line:** 1938:1938 **Comment:** *Custom Rule: Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). 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=c821cd6c48d4d0f112fb96f23df6c64f979b4cf6c10d6f434a68641438e45ebe&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=c821cd6c48d4d0f112fb96f23df6c64f979b4cf6c10d6f434a68641438e45ebe&reaction=dislike'>๐</a> ########## tests/unit_tests/utils/number_format_test.py: ########## @@ -0,0 +1,283 @@ +# 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. +import pytest + +from superset.utils.number_format import format_number_with_config + +# --- Helper behaviour the d3 parity matrix below cannot cover ---------------- + + [email protected]( + "d3_format,value,expected", + [ + # SMART_NUMBER is a Superset formatter (adaptive SI, half-up), not raw d3 + ("SMART_NUMBER", 4725, "4.73k"), + ("SMART_NUMBER", 80679663, "80.7M"), + ("SMART_NUMBER", 1234567890, "1.23B"), + ("SMART_NUMBER", 0, "0"), + (".2~f", 1200.0, "1200"), + (None, 42, "42"), + ("not-a-real-format!!", 42, "42"), + ], +) +def test_format_number(d3_format, value, expected): + assert format_number_with_config(d3_format, None, value) == expected + + [email protected]( + "currency,value,expected", + [ + ({"symbol": "USD", "symbolPosition": "prefix"}, 1234.5, "$ 1,234.50"), + ({"symbol": "EUR", "symbolPosition": "suffix"}, 1234.5, "1,234.50 โฌ"), + # unknown symbolPosition defaults to suffix, mirroring the frontend + ({"symbol": "BRL", "symbolPosition": None}, 1234.5, "1,234.50 R$"), + ], +) +def test_format_number_with_currency(currency, value, expected): + assert format_number_with_config(",.2f", currency, value) == expected + + +def test_currency_defaults_to_smart_number_when_no_d3_format(): + assert ( + format_number_with_config( + None, {"symbol": "USD", "symbolPosition": "prefix"}, 1234567 + ) + == "$ 1.23M" + ) + + +def test_auto_currency_formats_without_symbol(): + assert ( + format_number_with_config( + ",.2f", {"symbol": "AUTO", "symbolPosition": "prefix"}, 1234.5 + ) + == "1,234.50" + ) + + +def test_non_numeric_value_is_returned_as_is(): + assert format_number_with_config(",.2f", None, "abc") == "abc" + assert format_number_with_config(",.2f", None, None) == "" + + +# --- Parity with the frontend d3-format -------------------------------------- +# +# EXPECTED is the authoritative output of the frontend's ``d3-format`` (the same +# library the Table/Pivot charts render with) for every number preset in +# ``D3_FORMAT_OPTIONS``. Regenerate from ``superset-frontend`` with:: +# +# node -e 'const {format}=require("d3-format"); +# const p=["~g",",d",".1s",".3s",",.1%",".2%",".3%",".4r", +# ",.1f",",.2f",",.3f","+,","$,.2f"]; +# const v=[12345.432,0,4725,80679663,1234567890,-1234.5,0.0123,999.9]; +# const o={}; for(const f of p){o[f]={}; +# for(const x of v) o[f][x]=format(f)(x);} +# console.log(JSON.stringify(o));' +# +# One intentional deviation: d3 emits a Unicode minus (U+2212); the Python helper +# emits an ASCII "-" for email/CSV safety, so the comparison normalizes it. + +VALUES = [12345.432, 0, 4725, 80679663, 1234567890, -1234.5, 0.0123, 999.9] + +EXPECTED = { + "~g": [ + "12345.4", + "0", + "4725", + "8.06797e+7", + "1.23457e+9", + "โ1234.5", + "0.0123", + "999.9", + ], # noqa: E501 + ",d": [ + "12,345", + "0", + "4,725", + "80,679,663", + "1,234,567,890", + "โ1,235", + "0", + "1,000", + ], # noqa: E501 + ".1s": ["10k", "0", "5k", "80M", "1G", "โ1k", "10m", "1k"], + ".3s": ["12.3k", "0.00", "4.73k", "80.7M", "1.23G", "โ1.23k", "12.3m", "1.00k"], + ",.1%": [ + "1,234,543.2%", + "0.0%", + "472,500.0%", + "8,067,966,300.0%", + "123,456,789,000.0%", + "โ123,450.0%", + "1.2%", + "99,990.0%", + ], # noqa: E501 + ".2%": [ + "1234543.20%", + "0.00%", + "472500.00%", + "8067966300.00%", + "123456789000.00%", + "โ123450.00%", + "1.23%", + "99990.00%", + ], # noqa: E501 + ".3%": [ + "1234543.200%", + "0.000%", + "472500.000%", + "8067966300.000%", + "123456789000.000%", + "โ123450.000%", + "1.230%", + "99990.000%", + ], # noqa: E501 + ".4r": [ + "12350", + "0.000", + "4725", + "80680000", + "1235000000", + "โ1235", + "0.01230", + "999.9", + ], # noqa: E501 + ",.1f": [ + "12,345.4", + "0.0", + "4,725.0", + "80,679,663.0", + "1,234,567,890.0", + "โ1,234.5", + "0.0", + "999.9", + ], # noqa: E501 + ",.2f": [ + "12,345.43", + "0.00", + "4,725.00", + "80,679,663.00", + "1,234,567,890.00", + "โ1,234.50", + "0.01", + "999.90", + ], # noqa: E501 + ",.3f": [ + "12,345.432", + "0.000", + "4,725.000", + "80,679,663.000", + "1,234,567,890.000", + "โ1,234.500", + "0.012", + "999.900", + ], # noqa: E501 + "+,": [ + "+12,345.432", + "+0", + "+4,725", + "+80,679,663", + "+1,234,567,890", + "โ1,234.5", + "+0.0123", + "+999.9", + ], # noqa: E501 + "$,.2f": [ + "$12,345.43", + "$0.00", + "$4,725.00", + "$80,679,663.00", + "$1,234,567,890.00", + "โ$1,234.50", + "$0.01", + "$999.90", + ], # noqa: E501 + "(,.2f": [ + "12,345.43", + "0.00", + "4,725.00", + "80,679,663.00", + "1,234,567,890.00", + "(1,234.50)", + "0.01", + "999.90", + ], # noqa: E501 + "($,.2f": [ + "$12,345.43", + "$0.00", + "$4,725.00", + "$80,679,663.00", + "$1,234,567,890.00", + "($1,234.50)", + "$0.01", + "$999.90", + ], # noqa: E501 + " ,.2f": [ + " 12,345.43", + " 0.00", + " 4,725.00", + " 80,679,663.00", + " 1,234,567,890.00", + "โ1,234.50", + " 0.01", + " 999.90", + ], # noqa: E501 +} Review Comment: **Suggestion:** Provide an explicit type annotation for this dictionary constant declaration. [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This constant dictionary is introduced without an explicit type annotation, which is covered by the custom rule requiring type hints for relevant variables in new Python code. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=8b8be7c74a8e4c38adebf70680c88edc&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=8b8be7c74a8e4c38adebf70680c88edc&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:** tests/unit_tests/utils/number_format_test.py **Line:** 96:239 **Comment:** *Custom Rule: Provide an explicit type annotation for this dictionary constant declaration. 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=f6af99f6b99570ba53e7efcd41f46371b01687c0f1a559c03cce6a607b888b12&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=f6af99f6b99570ba53e7efcd41f46371b01687c0f1a559c03cce6a607b888b12&reaction=dislike'>๐</a> ########## tests/unit_tests/charts/test_client_processing.py: ########## @@ -1846,6 +1853,125 @@ def test_table(): ) +def test_table_applies_currency_format(): Review Comment: **Suggestion:** Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This new Python test function omits an explicit return type annotation. The custom rule requires type hints on new or modified Python functions when they can be annotated, so this is a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=95bd2914205140d8a15c9373f3c37ddb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=95bd2914205140d8a15c9373f3c37ddb&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:** tests/unit_tests/charts/test_client_processing.py **Line:** 1856:1856 **Comment:** *Custom Rule: Add an explicit return type annotation to this new test function (for example, annotate it as returning `None`). 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=16175a1459b54b62d1876fe8bf7fc2d2ff7da613056791e79935ba272b6f5218&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=16175a1459b54b62d1876fe8bf7fc2d2ff7da613056791e79935ba272b6f5218&reaction=dislike'>๐</a> ########## tests/unit_tests/utils/number_format_test.py: ########## @@ -0,0 +1,283 @@ +# 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. +import pytest + +from superset.utils.number_format import format_number_with_config + +# --- Helper behaviour the d3 parity matrix below cannot cover ---------------- + + [email protected]( + "d3_format,value,expected", + [ + # SMART_NUMBER is a Superset formatter (adaptive SI, half-up), not raw d3 + ("SMART_NUMBER", 4725, "4.73k"), + ("SMART_NUMBER", 80679663, "80.7M"), + ("SMART_NUMBER", 1234567890, "1.23B"), + ("SMART_NUMBER", 0, "0"), + (".2~f", 1200.0, "1200"), + (None, 42, "42"), + ("not-a-real-format!!", 42, "42"), + ], +) +def test_format_number(d3_format, value, expected): + assert format_number_with_config(d3_format, None, value) == expected + + [email protected]( + "currency,value,expected", + [ + ({"symbol": "USD", "symbolPosition": "prefix"}, 1234.5, "$ 1,234.50"), + ({"symbol": "EUR", "symbolPosition": "suffix"}, 1234.5, "1,234.50 โฌ"), + # unknown symbolPosition defaults to suffix, mirroring the frontend + ({"symbol": "BRL", "symbolPosition": None}, 1234.5, "1,234.50 R$"), + ], +) +def test_format_number_with_currency(currency, value, expected): + assert format_number_with_config(",.2f", currency, value) == expected + + +def test_currency_defaults_to_smart_number_when_no_d3_format(): + assert ( + format_number_with_config( + None, {"symbol": "USD", "symbolPosition": "prefix"}, 1234567 + ) + == "$ 1.23M" + ) + + +def test_auto_currency_formats_without_symbol(): + assert ( + format_number_with_config( + ",.2f", {"symbol": "AUTO", "symbolPosition": "prefix"}, 1234.5 + ) + == "1,234.50" + ) + + +def test_non_numeric_value_is_returned_as_is(): + assert format_number_with_config(",.2f", None, "abc") == "abc" + assert format_number_with_config(",.2f", None, None) == "" + + +# --- Parity with the frontend d3-format -------------------------------------- +# +# EXPECTED is the authoritative output of the frontend's ``d3-format`` (the same +# library the Table/Pivot charts render with) for every number preset in +# ``D3_FORMAT_OPTIONS``. Regenerate from ``superset-frontend`` with:: +# +# node -e 'const {format}=require("d3-format"); +# const p=["~g",",d",".1s",".3s",",.1%",".2%",".3%",".4r", +# ",.1f",",.2f",",.3f","+,","$,.2f"]; +# const v=[12345.432,0,4725,80679663,1234567890,-1234.5,0.0123,999.9]; +# const o={}; for(const f of p){o[f]={}; +# for(const x of v) o[f][x]=format(f)(x);} +# console.log(JSON.stringify(o));' +# +# One intentional deviation: d3 emits a Unicode minus (U+2212); the Python helper +# emits an ASCII "-" for email/CSV safety, so the comparison normalizes it. + +VALUES = [12345.432, 0, 4725, 80679663, 1234567890, -1234.5, 0.0123, 999.9] Review Comment: **Suggestion:** Annotate this module-level variable with an explicit type to comply with required variable typing. [custom_rule] **Severity Level:** Minor โ ๏ธ <details> <summary><b>Why it matters? ๐ค </b></summary> This module-level constant is a relevant variable that can be annotated, but it is left untyped in the new file, so the type-hint rule applies. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=1104b85d22c142ef9d9adb76380d1cd7&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=1104b85d22c142ef9d9adb76380d1cd7&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:** tests/unit_tests/utils/number_format_test.py **Line:** 94:94 **Comment:** *Custom Rule: Annotate this module-level variable with an explicit type to comply with required variable typing. 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=ba2905d2020ffe4a3f8766e1c3b2e86e11d1213f4f414ea540d53411c197877a&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41028&comment_hash=ba2905d2020ffe4a3f8766e1c3b2e86e11d1213f4f414ea540d53411c197877a&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]
