codeant-ai-for-open-source[bot] commented on code in PR #41970: URL: https://github.com/apache/superset/pull/41970#discussion_r3565447313
########## superset/utils/legacy_thumbnails.py: ########## @@ -0,0 +1,41 @@ +# 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. +"""Thumbnail helpers for report screenshots.""" +from io import BytesIO + +from PIL import Image, ImageDraw, ImageFont + +# Module-level: on Pillow >=10 this raises AttributeError at import time. +RESAMPLE_FILTER = Image.ANTIALIAS + + +def generate_thumbnail(image_bytes: bytes, max_size: tuple[int, int] = (400, 300)) -> bytes: + img = Image.open(BytesIO(image_bytes)) + img = img.convert("RGB") + img.thumbnail(max_size, RESAMPLE_FILTER) + + # Stamp a small label in the corner. ImageFont.getsize() was also removed in + # Pillow 10 -- the second API a migration must fix (to getbbox()/getlength()). + draw = ImageDraw.Draw(img) + font = ImageFont.load_default() + label = "thumb" + text_w, text_h = font.getsize(label) Review Comment: **Suggestion:** `ImageFont.getsize()` is removed in Pillow 10+, so this call fails at runtime when generating a thumbnail. Use a supported text measurement API (such as `font.getbbox()`/`draw.textbbox()`) before computing the text placement. [api mismatch] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Thumbnail helper generate_thumbnail crashes when stamping label text. - ⚠️ Unit tests calling generate_thumbnail fail after import fix. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. The unit tests in `tests/unit_tests/utils/legacy_thumbnails_test.py:33-40` (found via Grep) call `generate_thumbnail`, so once the module imports successfully they exercise the function body. 2. Inside `generate_thumbnail` at `superset/utils/legacy_thumbnails.py:26-37` (verified with Read), the function opens the image bytes, converts to RGB, and prepares to draw a label using `ImageDraw.Draw` and `ImageFont.load_default`. 3. At line 36 in `superset/utils/legacy_thumbnails.py`, the code calls `font.getsize(label)` to compute `text_w` and `text_h` for label placement. 4. In Pillow version 10 or higher, `ImageFont.getsize` has been removed in favor of newer APIs; calling `font.getsize(label)` raises an `AttributeError` at line 36, causing `generate_thumbnail` to fail at runtime and preventing the tests (or any caller) from successfully generating a thumbnail once the import-time constant issue is addressed. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=bbd42ea5aba144c9939012ba6dfc4016&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=bbd42ea5aba144c9939012ba6dfc4016&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/legacy_thumbnails.py **Line:** 36:36 **Comment:** *Api Mismatch: `ImageFont.getsize()` is removed in Pillow 10+, so this call fails at runtime when generating a thumbnail. Use a supported text measurement API (such as `font.getbbox()`/`draw.textbbox()`) before computing the text placement. 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%2F41970&comment_hash=7f6ddfc2dd54986bfbdf8f200a70f1b3c2a55be637836775785ea4772de95bd8&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41970&comment_hash=7f6ddfc2dd54986bfbdf8f200a70f1b3c2a55be637836775785ea4772de95bd8&reaction=dislike'>👎</a> ########## superset/utils/legacy_thumbnails.py: ########## @@ -0,0 +1,41 @@ +# 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. +"""Thumbnail helpers for report screenshots.""" +from io import BytesIO + +from PIL import Image, ImageDraw, ImageFont + +# Module-level: on Pillow >=10 this raises AttributeError at import time. +RESAMPLE_FILTER = Image.ANTIALIAS Review Comment: **Suggestion:** `Image.ANTIALIAS` was removed in Pillow 10+, and this project requires Pillow 11+, so this module raises `AttributeError` at import time before any function can run. Replace it with a supported enum (for example `Image.Resampling.LANCZOS`) and avoid binding deprecated constants at module import. [api mismatch] <details> <summary><b>Severity Level:</b> Critical 🚨</summary> ```mdx - ❌ Unit test generate_thumbnail import crashes with AttributeError. - ⚠️ Any consumer importing legacy_thumbnails fails before execution. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. In `tests/unit_tests/utils/legacy_thumbnails_test.py:21` (found via Grep), the tests import `generate_thumbnail` from `superset.utils.legacy_thumbnails`. 2. Python resolves this import by loading `superset/utils/legacy_thumbnails.py` and executing its module-level statements. 3. During import, at `superset/utils/legacy_thumbnails.py:20-23` (verified with Read), the code executes `RESAMPLE_FILTER = Image.ANTIALIAS`. 4. On Pillow version 10 or higher, `Image.ANTIALIAS` has been removed; this attribute access raises `AttributeError` at line 23, causing the module import to fail and any caller (including the unit test import) to error before `generate_thumbnail` can run. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=956123abd2d94f89a69c2e4bdc021a2e&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=956123abd2d94f89a69c2e4bdc021a2e&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/legacy_thumbnails.py **Line:** 23:23 **Comment:** *Api Mismatch: `Image.ANTIALIAS` was removed in Pillow 10+, and this project requires Pillow 11+, so this module raises `AttributeError` at import time before any function can run. Replace it with a supported enum (for example `Image.Resampling.LANCZOS`) and avoid binding deprecated constants at module import. 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%2F41970&comment_hash=830ba9f7585dbc0da0ae0969d1cf4d83f776978cb10aae5e97ac7ef4fb789624&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41970&comment_hash=830ba9f7585dbc0da0ae0969d1cf4d83f776978cb10aae5e97ac7ef4fb789624&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]
