codeant-ai-for-open-source[bot] commented on code in PR #41133: URL: https://github.com/apache/superset/pull/41133#discussion_r3429808897
########## tests/unit_tests/tasks/test_export_dashboard_excel.py: ########## @@ -0,0 +1,212 @@ +# 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. +from __future__ import annotations + +import glob +import os +import tempfile +from collections.abc import Iterator +from contextlib import ExitStack +from typing import Any +from unittest import mock + Review Comment: **Suggestion:** Add a short docstring to this helper function describing that it builds a mocked chart object for test scenarios. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python function in the new file, and it does not include a docstring. The custom rule requires newly added Python functions and classes to be documented inline, so the suggestion correctly identifies a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=77b579a4042c4a76bcb0e9ff1227f40b&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=77b579a4042c4a76bcb0e9ff1227f40b&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/tasks/test_export_dashboard_excel.py **Line:** 26:26 **Comment:** *Custom Rule: Add a short docstring to this helper function describing that it builds a mocked chart object for test scenarios. 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%2F41133&comment_hash=ae26164ff9521c9e61028540020aa71a8b6cc0fc4d91006d5fb80cfbdff8c45c&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=ae26164ff9521c9e61028540020aa71a8b6cc0fc4d91006d5fb80cfbdff8c45c&reaction=dislike'>👎</a> ########## tests/unit_tests/utils/excel_streaming_tests.py: ########## @@ -0,0 +1,170 @@ +# 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. +from __future__ import annotations + +from datetime import date, datetime +from decimal import Decimal +from pathlib import Path + +import pytest + +from superset.utils import excel_streaming +from superset.utils.excel_streaming import ( + _sanitize_cell, + sanitize_sheet_name, + StreamingXlsxWriter, +) + +# --- sanitize_sheet_name --- + + +def test_sheet_name_replaces_forbidden_chars() -> None: + assert sanitize_sheet_name("a/b:c*d?e[f]g\\h", set()) == "a_b_c_d_e_f_g_h" + + +def test_sheet_name_truncated_to_31() -> None: + assert sanitize_sheet_name("x" * 40, set()) == "x" * 31 + + +def test_sheet_name_dedupes_case_insensitively() -> None: Review Comment: **Suggestion:** Add a docstring to this new test function so its intent is explicitly documented. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This newly added test function has no docstring in the final file state. The rule explicitly says new Python functions should be documented inline. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=3277060fbc234bcf8313a2791fd0f88f&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=3277060fbc234bcf8313a2791fd0f88f&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/excel_streaming_tests.py **Line:** 43:43 **Comment:** *Custom Rule: Add a docstring to this new test function so its intent is explicitly documented. 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%2F41133&comment_hash=65ea1f026e3fb2f04a53db1f84088f72b50bc7265cb9acabd56997c6a96eacbb&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=65ea1f026e3fb2f04a53db1f84088f72b50bc7265cb9acabd56997c6a96eacbb&reaction=dislike'>👎</a> ########## tests/unit_tests/utils/s3_tests.py: ########## @@ -0,0 +1,71 @@ +# 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. +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +from superset.utils import s3 + + +@patch("superset.utils.s3.boto3.client") +@patch("superset.utils.s3.current_app") +def test_upload_file_to_s3(mock_app: MagicMock, mock_client_fn: MagicMock) -> None: Review Comment: **Suggestion:** Add a short docstring under this new test function describing the behavior being validated and the expected S3 upload interaction. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python function in the new file, and it does not include a docstring. The custom rule requires newly added Python functions and classes to be documented inline, so the suggestion correctly identifies a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=882ce9122ee54b74b461cdfb398f3a91&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=882ce9122ee54b74b461cdfb398f3a91&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/s3_tests.py **Line:** 26:26 **Comment:** *Custom Rule: Add a short docstring under this new test function describing the behavior being validated and the expected S3 upload interaction. 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%2F41133&comment_hash=b8b91aa5d7e65c9af113dbc81f669c29e282335a09e8d7b3213912c473f6ba7d&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=b8b91aa5d7e65c9af113dbc81f669c29e282335a09e8d7b3213912c473f6ba7d&reaction=dislike'>👎</a> ########## tests/unit_tests/tasks/test_export_dashboard_excel.py: ########## @@ -0,0 +1,212 @@ +# 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. +from __future__ import annotations + +import glob +import os +import tempfile +from collections.abc import Iterator +from contextlib import ExitStack +from typing import Any +from unittest import mock + +import pytest +from celery.exceptions import SoftTimeLimitExceeded + +from superset.utils import json + +MODULE = "superset.tasks.export_dashboard_excel" + + +def _chart(chart_id: int, name: str, has_context: bool = True) -> mock.MagicMock: + chart = mock.MagicMock() + chart.id = chart_id + chart.slice_name = name + chart.query_context = json.dumps({"queries": [{}]}) if has_context else None + return chart + + Review Comment: **Suggestion:** Add a brief test docstring summarizing the expected behavior validated by this test case. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This newly introduced test function has no docstring in the final file state. Since the rule says new Python functions should be documented inline, this is a valid and verifiable violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6980146687f7422ab7dcf03f08b0231a&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=6980146687f7422ab7dcf03f08b0231a&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/tasks/test_export_dashboard_excel.py **Line:** 40:42 **Comment:** *Custom Rule: Add a brief test docstring summarizing the expected behavior validated by this test case. 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%2F41133&comment_hash=b4f097485f09571e1bbe0df987d4b63245d8bf2ebd88c312d4e2f894ade76387&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=b4f097485f09571e1bbe0df987d4b63245d8bf2ebd88c312d4e2f894ade76387&reaction=dislike'>👎</a> ########## superset/dashboards/schemas.py: ########## @@ -623,3 +623,21 @@ class CacheScreenshotSchema(Schema): fields.List(fields.Str(), validate=lambda x: len(x) == 2), required=False ) permalinkKey = fields.Str(required=False) # noqa: N815 + + +class DashboardExportXlsxPostSchema(Schema): + active_data_mask = fields.Dict( + keys=fields.Str(), + values=fields.Dict(), + load_default=dict, + metadata={ + "description": "Live dashboard filter state keyed by native filter id, " + "each carrying an extraFormData object." + }, + ) + + +class DashboardExportXlsxResponseSchema(Schema): + job_id = fields.String( + metadata={"description": "Correlation id for the async export task"} + ) Review Comment: **Suggestion:** Add a class docstring explaining that this schema represents the async XLSX export response contract. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python class and it does not include a docstring. That matches the stated custom rule for newly added functions and classes, so the violation is real. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d6f9c13209324905bd83ba8be9154446&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=d6f9c13209324905bd83ba8be9154446&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/dashboards/schemas.py **Line:** 640:643 **Comment:** *Custom Rule: Add a class docstring explaining that this schema represents the async XLSX export response contract. 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%2F41133&comment_hash=d35e366b979a8f3401b8eca232dd5ca7d231a37ba9e0883c8fcf5ee9cde54314&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=d35e366b979a8f3401b8eca232dd5ca7d231a37ba9e0883c8fcf5ee9cde54314&reaction=dislike'>👎</a> ########## tests/unit_tests/dashboards/test_excel_export_email.py: ########## @@ -0,0 +1,98 @@ +# 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. +from __future__ import annotations + +from datetime import datetime +from unittest.mock import MagicMock, patch + +from superset.dashboards.excel_export import email + +REQUESTED = datetime(2026, 1, 1, 12, 0, 0) +EXPIRES = datetime(2026, 1, 2, 12, 0, 0) + + +def test_success_email_contains_link_and_expiry() -> None: + html = email.build_success_email( + dashboard_title="Sales", + download_url="https://signed.example/file.xlsx?sig=abc", + requested_at=REQUESTED, + expires_at=EXPIRES, + ttl_seconds=86400, + skipped_charts=[], + ) + assert "https://signed.example/file.xlsx?sig=abc" in html + assert "expires in 24 hours" in html + assert "2026-01-02 12:00:00 UTC" in html + assert "2026-01-01 12:00:00 UTC" in html + assert "<li>" not in html # no skipped section + + +def test_success_email_lists_skipped_charts() -> None: + html = email.build_success_email( + dashboard_title="Sales", + download_url="https://x", + requested_at=REQUESTED, + expires_at=EXPIRES, + ttl_seconds=86400, + skipped_charts=["10 - Broken chart"], + ) + assert "no saved query context" in html + assert "<li>10 - Broken chart</li>" in html + + +def test_success_email_escapes_title() -> None: + html = email.build_success_email( + dashboard_title="<script>alert(1)</script>", + download_url="https://x", + requested_at=REQUESTED, + expires_at=EXPIRES, + ttl_seconds=86400, + skipped_charts=[], + ) + assert "<script>" not in html + assert "<script>" in html + + +def test_failure_email_body() -> None: + html = email.build_failure_email("Sales", REQUESTED) + assert "could not be completed" in html + assert "2026-01-01 12:00:00 UTC" in html + + +@patch("superset.dashboards.excel_export.email.current_app") Review Comment: **Suggestion:** Add a docstring to this new test function to describe subject-prefix handling for both success and failure email subjects. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The function is newly added and lacks a docstring in the current file. That is a direct match for the custom rule about documenting new Python functions and classes. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=c6b42e1fedb14dc29c43c068aa73bd76&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=c6b42e1fedb14dc29c43c068aa73bd76&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/dashboards/test_excel_export_email.py **Line:** 76:76 **Comment:** *Custom Rule: Add a docstring to this new test function to describe subject-prefix handling for both success and failure email subjects. 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%2F41133&comment_hash=78a15c449e782c1f9275dd72d96eb10798f3b0cb5936a90d169b3170373913ab&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=78a15c449e782c1f9275dd72d96eb10798f3b0cb5936a90d169b3170373913ab&reaction=dislike'>👎</a> ########## superset/utils/excel_streaming.py: ########## @@ -0,0 +1,190 @@ +# 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. +""" +Streaming, constant-memory XLSX writer for multi-sheet dashboard exports. + +Unlike :mod:`superset.utils.excel`, which materializes a whole DataFrame in +memory, this writer streams rows one at a time into an ``xlsxwriter`` workbook +opened in ``constant_memory`` mode, so a dashboard with many large charts never +holds more than one row per sheet in memory at once. +""" + +from __future__ import annotations + +import math +import numbers +import re +from collections.abc import Iterable, Sequence +from datetime import date, datetime +from decimal import Decimal +from typing import Any + +import xlsxwriter + +from superset.utils.excel import NEUTRAL_DOCUMENT_PROPERTIES + +# Excel limits a sheet name to 31 characters and forbids these characters. +MAX_SHEET_NAME_LEN = 31 +_INVALID_SHEET_CHARS_RE = re.compile(r"[\[\]:*?/\\]") +# Excel reserves the sheet name "History" (case-insensitive). +_RESERVED_SHEET_NAME = "history" + +# A worksheet holds at most 1,048,576 rows; one is reserved for the header. +MAX_DATA_ROWS_PER_SHEET = 1_048_576 - 1 + +# Leading characters that turn a cell into a formula in spreadsheet apps. Mirrors +# superset.utils.excel.quote_formulas so streamed exports get the same guard. +_FORMULA_PREFIXES = {"=", "+", "-", "@"} + +# Excel cannot represent integers beyond 10**15 without precision loss. +_MAX_EXCEL_INT = 10**15 + + +def sanitize_sheet_name(raw: str, used: set[str]) -> str: + """ + Produce a valid, unique Excel sheet name from ``raw``. + + Replaces forbidden characters, strips surrounding apostrophes/whitespace, + avoids the reserved name "History", truncates to 31 characters, and + disambiguates case-insensitive collisions with ``~2``/``~3`` suffixes. + The chosen name (lower-cased) is added to ``used``. + + :param raw: The desired sheet name (e.g. ``"42 - Sales by Region"``) + :param used: Lower-cased names already taken; mutated with the result + :returns: A sanitized, unique sheet name no longer than 31 characters + """ + name = _INVALID_SHEET_CHARS_RE.sub("_", raw or "") + name = name.strip().strip("'").strip() + if not name: + name = "Sheet" + if name.lower() == _RESERVED_SHEET_NAME: + name = f"{name}_" + name = name[:MAX_SHEET_NAME_LEN] + + if name.lower() not in used: + used.add(name.lower()) + return name + + suffix = 2 + while True: + marker = f"~{suffix}" + candidate = name[: MAX_SHEET_NAME_LEN - len(marker)] + marker + if candidate.lower() not in used: + used.add(candidate.lower()) + return candidate + suffix += 1 + + +def _sanitize_cell(value: Any) -> Any: + """ + Coerce a single cell value into something safe for ``xlsxwriter``. + + Quotes formula-like strings (defense against formula injection), stringifies + integers/floats Excel cannot represent precisely, renders temporal values as + ISO strings (timezones are not natively supported), and blanks out ``None`` + and non-finite floats. + """ + if value is None: + return "" + # bool is a subclass of int; preserve it before the numeric branches. + if isinstance(value, bool): + return value + if isinstance(value, str): + return f"'{value}" if value and value[0] in _FORMULA_PREFIXES else value + if isinstance(value, (datetime, date)): + return value.isoformat() + if isinstance(value, Decimal): + number = float(value) + return str(number) if abs(number) > _MAX_EXCEL_INT else number + if isinstance(value, numbers.Integral): + number = int(value) + return str(number) if abs(number) > _MAX_EXCEL_INT else number + if isinstance(value, numbers.Real): + number = float(value) + if not math.isfinite(number): + return "" + return str(number) if abs(number) > _MAX_EXCEL_INT else number + # Anything else (lists, dicts, custom objects) is stringified, still guarding + # against formula injection on the resulting text. + text = str(value) + return f"'{text}" if text and text[0] in _FORMULA_PREFIXES else text + + +class StreamingXlsxWriter: + """ + A thin wrapper over ``xlsxwriter`` in constant-memory mode that writes one + sheet per chart, row by row. + + Sheet names are sanitized and de-duplicated, cell values are sanitized for + safety/compatibility, and per-sheet row counts are capped at Excel's limit. + Always call :meth:`close` (e.g. in a ``finally`` block) to finalize the file. + """ + + def __init__(self, path: str) -> None: + self._workbook = xlsxwriter.Workbook(path, {"constant_memory": True}) + # Reset document properties so the file carries no identifying details. + self._workbook.set_properties(NEUTRAL_DOCUMENT_PROPERTIES) + self._used_sheet_names: set[str] = set() Review Comment: **Suggestion:** Add an inline docstring to this newly added initializer method to document its purpose and setup behavior. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added method in a new Python file, and it does not have a docstring immediately under the definition. The custom rule requires newly added Python functions and classes to include docstrings, so this is a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=9bca29e1c506476c8c5651eb3695adc6&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=9bca29e1c506476c8c5651eb3695adc6&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/excel_streaming.py **Line:** 137:141 **Comment:** *Custom Rule: Add an inline docstring to this newly added initializer method to document its purpose and setup behavior. 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%2F41133&comment_hash=8de78bbeb58fab449059e49e01794a9f84e0e9913d543b137ff1e2897c7cde4d&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41133&comment_hash=8de78bbeb58fab449059e49e01794a9f84e0e9913d543b137ff1e2897c7cde4d&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]
