codeant-ai-for-open-source[bot] commented on code in PR #41651: URL: https://github.com/apache/superset/pull/41651#discussion_r3518817611
########## scripts/translations/apply_do_not_translate.py: ########## @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +# 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. +"""Stamp do-not-translate msgids in a .pot with an extracted-comment marker. + +For every msgid listed in ``superset/translations/do-not-translate.txt`` that is +present in the target .pot, add a ``#. MACHINE_READ-DO_NOT_TRANSLATE`` extracted +comment. gettext extracted comments (``#.``) propagate from the .pot into every +language .po on ``pybabel update``, so the do-not-translate status stays +consistent across all catalogs from a single registry. + +Run from ``babel_update.sh`` after the .pot is extracted and normalized (and +before ``pybabel update``). Idempotent: re-running makes no further changes. + +Usage: + python scripts/translations/apply_do_not_translate.py [POT_PATH] + # POT_PATH defaults to superset/translations/messages.pot +""" + +from __future__ import annotations + +import sys +from pathlib import Path + +# The standardized extracted-comment marker. Kept in sync with backfill_po.py. +MARKER: str = "MACHINE_READ-DO_NOT_TRANSLATE" +_MARKER_LINE: str = f"#. {MARKER}" + +TRANSLATIONS_DIR: Path = ( + Path(__file__).parent.parent.parent / "superset" / "translations" +) +DEFAULT_POT: Path = TRANSLATIONS_DIR / "messages.pot" +REGISTRY: Path = TRANSLATIONS_DIR / "do-not-translate.txt" + + +def load_registry(path: Path = REGISTRY) -> set[str]: + """Return the set of do-not-translate msgids (skips comments/blank lines). + + Each line is stripped before the blank/comment check, so trailing + whitespace or an indented comment never yields a msgid that fails to match + the .pot. + """ + if not path.exists(): + return set() + entries: set[str] = set() + for raw_line in path.read_text(encoding="utf-8").splitlines(): + line = raw_line.strip() + if line and not line.startswith("#"): + entries.add(line) + return entries + + +def _escape(msgid: str) -> str: + """Escape a msgid the way gettext writes it on a `msgid "..."` line.""" + return msgid.replace("\\", "\\\\").replace('"', '\\"') + + +def apply_markers(pot_path: Path, registry: set[str]) -> int: + """Insert the marker comment above each registry msgid via text edit. + + Text manipulation (rather than a polib round-trip) preserves the .pot's + exact wrapping/layout, so the only change is the added marker lines. + Idempotent. Returns the number of entries newly marked. + """ + lines = pot_path.read_text(encoding="utf-8").split("\n") Review Comment: **Suggestion:** Add an explicit type hint for this local collection variable to satisfy the requirement that relevant variables are annotated. [custom_rule] **Severity Level:** Minor β οΈ <details> <summary><b>Why it matters? π€ </b></summary> The rule requires type hints on relevant variables that can be annotated. This local list variable is unannotated, so the suggestion correctly identifies a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=ce2e83b7fae74483baa26ba1b3194cc3&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=ce2e83b7fae74483baa26ba1b3194cc3&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:** scripts/translations/apply_do_not_translate.py **Line:** 79:79 **Comment:** *Custom Rule: Add an explicit type hint for this local collection variable to satisfy the requirement that relevant variables are annotated. 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%2F41651&comment_hash=bdf91641b32288d3dd9836a7df7951ef11b44cb6bbf5881f6fc1ce8bc0428ba8&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41651&comment_hash=bdf91641b32288d3dd9836a7df7951ef11b44cb6bbf5881f6fc1ce8bc0428ba8&reaction=dislike'>π</a> ########## scripts/translations/apply_do_not_translate.py: ########## @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +# 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. +"""Stamp do-not-translate msgids in a .pot with an extracted-comment marker. + +For every msgid listed in ``superset/translations/do-not-translate.txt`` that is +present in the target .pot, add a ``#. MACHINE_READ-DO_NOT_TRANSLATE`` extracted +comment. gettext extracted comments (``#.``) propagate from the .pot into every +language .po on ``pybabel update``, so the do-not-translate status stays +consistent across all catalogs from a single registry. + +Run from ``babel_update.sh`` after the .pot is extracted and normalized (and +before ``pybabel update``). Idempotent: re-running makes no further changes. + +Usage: + python scripts/translations/apply_do_not_translate.py [POT_PATH] + # POT_PATH defaults to superset/translations/messages.pot +""" + +from __future__ import annotations + +import sys +from pathlib import Path + +# The standardized extracted-comment marker. Kept in sync with backfill_po.py. +MARKER: str = "MACHINE_READ-DO_NOT_TRANSLATE" +_MARKER_LINE: str = f"#. {MARKER}" + +TRANSLATIONS_DIR: Path = ( + Path(__file__).parent.parent.parent / "superset" / "translations" +) +DEFAULT_POT: Path = TRANSLATIONS_DIR / "messages.pot" +REGISTRY: Path = TRANSLATIONS_DIR / "do-not-translate.txt" + + +def load_registry(path: Path = REGISTRY) -> set[str]: + """Return the set of do-not-translate msgids (skips comments/blank lines). + + Each line is stripped before the blank/comment check, so trailing + whitespace or an indented comment never yields a msgid that fails to match + the .pot. + """ + if not path.exists(): + return set() + entries: set[str] = set() + for raw_line in path.read_text(encoding="utf-8").splitlines(): + line = raw_line.strip() + if line and not line.startswith("#"): + entries.add(line) + return entries + + +def _escape(msgid: str) -> str: + """Escape a msgid the way gettext writes it on a `msgid "..."` line.""" + return msgid.replace("\\", "\\\\").replace('"', '\\"') + + +def apply_markers(pot_path: Path, registry: set[str]) -> int: + """Insert the marker comment above each registry msgid via text edit. + + Text manipulation (rather than a polib round-trip) preserves the .pot's + exact wrapping/layout, so the only change is the added marker lines. + Idempotent. Returns the number of entries newly marked. + """ + lines = pot_path.read_text(encoding="utf-8").split("\n") + targets = {f'msgid "{_escape(m)}"' for m in registry} Review Comment: **Suggestion:** Add a type annotation for this derived set variable so it complies with the projectβs type-hint requirement for relevant variables. [custom_rule] **Severity Level:** Minor β οΈ <details> <summary><b>Why it matters? π€ </b></summary> This set comprehension result is a relevant variable that could be annotated, but it is not. That matches the stated type-hint requirement violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=1c289dc97aea4216892c457119f9b641&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=1c289dc97aea4216892c457119f9b641&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:** scripts/translations/apply_do_not_translate.py **Line:** 80:80 **Comment:** *Custom Rule: Add a type annotation for this derived set variable so it complies with the projectβs type-hint requirement for relevant 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%2F41651&comment_hash=273bd8147d822094dd6d1ace235e1f9377665560b3bbd5aee72467c8e205c91f&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41651&comment_hash=273bd8147d822094dd6d1ace235e1f9377665560b3bbd5aee72467c8e205c91f&reaction=dislike'>π</a> ########## scripts/translations/apply_do_not_translate.py: ########## @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +# 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. +"""Stamp do-not-translate msgids in a .pot with an extracted-comment marker. + +For every msgid listed in ``superset/translations/do-not-translate.txt`` that is +present in the target .pot, add a ``#. MACHINE_READ-DO_NOT_TRANSLATE`` extracted +comment. gettext extracted comments (``#.``) propagate from the .pot into every +language .po on ``pybabel update``, so the do-not-translate status stays +consistent across all catalogs from a single registry. + +Run from ``babel_update.sh`` after the .pot is extracted and normalized (and +before ``pybabel update``). Idempotent: re-running makes no further changes. + +Usage: + python scripts/translations/apply_do_not_translate.py [POT_PATH] + # POT_PATH defaults to superset/translations/messages.pot +""" + +from __future__ import annotations + +import sys +from pathlib import Path + +# The standardized extracted-comment marker. Kept in sync with backfill_po.py. +MARKER: str = "MACHINE_READ-DO_NOT_TRANSLATE" +_MARKER_LINE: str = f"#. {MARKER}" + +TRANSLATIONS_DIR: Path = ( + Path(__file__).parent.parent.parent / "superset" / "translations" +) +DEFAULT_POT: Path = TRANSLATIONS_DIR / "messages.pot" +REGISTRY: Path = TRANSLATIONS_DIR / "do-not-translate.txt" + + +def load_registry(path: Path = REGISTRY) -> set[str]: + """Return the set of do-not-translate msgids (skips comments/blank lines). + + Each line is stripped before the blank/comment check, so trailing + whitespace or an indented comment never yields a msgid that fails to match + the .pot. + """ + if not path.exists(): + return set() + entries: set[str] = set() + for raw_line in path.read_text(encoding="utf-8").splitlines(): + line = raw_line.strip() + if line and not line.startswith("#"): + entries.add(line) + return entries + + +def _escape(msgid: str) -> str: + """Escape a msgid the way gettext writes it on a `msgid "..."` line.""" + return msgid.replace("\\", "\\\\").replace('"', '\\"') + + +def apply_markers(pot_path: Path, registry: set[str]) -> int: + """Insert the marker comment above each registry msgid via text edit. + + Text manipulation (rather than a polib round-trip) preserves the .pot's + exact wrapping/layout, so the only change is the added marker lines. + Idempotent. Returns the number of entries newly marked. + """ + lines = pot_path.read_text(encoding="utf-8").split("\n") + targets = {f'msgid "{_escape(m)}"' for m in registry} + out: list[str] = [] + changed: int = 0 + for line in lines: + if line in targets and (not out or out[-1] != _MARKER_LINE): + # `#.` extracted comments precede `msgid`; these registry entries are + # bare single-line msgids, so inserting directly above is correct. + out.append(_MARKER_LINE) + changed += 1 + out.append(line) + if changed: + pot_path.write_text("\n".join(out), encoding="utf-8") + return changed + + +def main() -> None: + """Stamp the marker onto the target .pot from the registry.""" + pot_path: Path = Path(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_POT + if not pot_path.exists(): + print(f"POT file not found: {pot_path}", file=sys.stderr) + sys.exit(1) + # Fail fast if the registry file is absent: babel_update.sh depends on this + # step to stamp the .pot, and continuing would silently publish catalogs + # without any do-not-translate markers. An existing-but-empty registry is a + # valid state (nothing to mark), so only a missing file is an error. + if not REGISTRY.exists(): + print( + f"do-not-translate registry not found at {REGISTRY}; refusing to " + "produce unmarked translation artifacts.", + file=sys.stderr, + ) + sys.exit(1) + registry: set[str] = load_registry() + if not registry: + print( + f"do-not-translate registry {REGISTRY} is empty; nothing to mark.", + file=sys.stderr, + ) + return + changed = apply_markers(pot_path, registry) Review Comment: **Suggestion:** Add an explicit type annotation for this local result variable to keep variable typing consistent with the enforced rule. [custom_rule] **Severity Level:** Minor β οΈ <details> <summary><b>Why it matters? π€ </b></summary> The local result variable is unannotated even though its type is evident and could be declared. This is a real omission under the Python type-hint rule. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=043be62829484b529a4453d7e66c6311&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=043be62829484b529a4453d7e66c6311&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:** scripts/translations/apply_do_not_translate.py **Line:** 119:119 **Comment:** *Custom Rule: Add an explicit type annotation for this local result variable to keep variable typing consistent with the enforced rule. 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%2F41651&comment_hash=6b33b765d77b54fc8887430162627972051947903802a8ce48d1883b2875bbb2&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41651&comment_hash=6b33b765d77b54fc8887430162627972051947903802a8ce48d1883b2875bbb2&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]
