codeant-ai-for-open-source[bot] commented on code in PR #41530:
URL: https://github.com/apache/superset/pull/41530#discussion_r3506097459
##########
superset/cli/thumbnails.py:
##########
@@ -82,18 +83,23 @@ def compute_generic_thumbnail(
query = db.session.query(model_cls)
if model_ids:
query = query.filter(model_cls.id.in_(model_ids))
- dashboards = query.all()
- count = len(dashboards)
- for i, model in enumerate(dashboards):
- if asynchronous:
- func = compute_func.delay
- action = "Triggering"
- else:
- func = compute_func
- action = "Processing"
- msg = f'{action} {friendly_type} "{model}" ({i + 1}/{count})'
+ # Materialize the id and label up front. Computing a thumbnail below
can
+ # close/expire the session, which detaches the ORM instances and makes
+ # str(model) raise DetachedInstanceError on subsequent iterations.
+ items: list[tuple[int, str]] = [(model.id, str(model)) for model in
query.all()]
+ count: int = len(items)
+ func: Callable[..., Any]
+ action: str
+ if asynchronous:
+ func = compute_func.delay
+ action = "Triggering"
+ else:
+ func = compute_func
+ action = "Processing"
+ for i, (model_pk, label) in enumerate(items):
+ msg = f'{action} {friendly_type} "{label}" ({i + 1}/{count})'
Review Comment:
**Suggestion:** Add an explicit type annotation for this newly introduced
local variable to comply with the rule requiring type hints on relevant
annotatable variables. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This is a newly introduced local variable that can clearly be annotated as
`str`, but it is assigned without a type hint. That matches the rule requiring
type hints on relevant annotatable variables.
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=da192180d4664decb1267e7ffb8ff862&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=da192180d4664decb1267e7ffb8ff862&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/cli/thumbnails.py
**Line:** 100:100
**Comment:**
*Custom Rule: Add an explicit type annotation for this newly introduced
local variable to comply with the rule requiring type hints on relevant
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%2F41530&comment_hash=d77dac5360b6a8c354c7c655c28a920ae4e745ade53e553bc2176ce6b4b16866&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41530&comment_hash=d77dac5360b6a8c354c7c655c28a920ae4e745ade53e553bc2176ce6b4b16866&reaction=dislike'>👎</a>
##########
tests/unit_tests/cli/thumbnails_test.py:
##########
@@ -0,0 +1,79 @@
+# 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 click.testing import CliRunner
+from pytest_mock import MockerFixture
+
+from superset.cli import thumbnails
+
+
+class _FakeModel:
+ """Stand-in for a Dashboard/Slice that detaches mid-run.
+
+ Once ``detached`` is set, ``str()`` raises -- mimicking the
+ DetachedInstanceError that SQLAlchemy raises when the session that
+ loaded the instance has been closed/expired.
+ """
+
+ def __init__(self, pk: int, label: str) -> None:
+ self.id: int = pk
+ self._label: str = label
+ self.detached: bool = False
+
+ def __str__(self) -> str:
+ if self.detached:
+ raise RuntimeError("Instance is not bound to a Session
(simulated)")
+ return self._label
+
+
+def test_compute_thumbnails_survives_detached_instances(
+ mocker: MockerFixture, app_context: None
+) -> None:
+ """Regression test for the DetachedInstanceError in compute-thumbnails.
+
+ Computing a thumbnail can close/expire the session, detaching the ORM
+ instances. The command must read each model's id and label *before* the
+ compute loop, so building the progress message never touches a detached
+ instance even when several models are processed.
+ """
+ models = [_FakeModel(1, "Dashboard A"), _FakeModel(2, "Dashboard B")]
Review Comment:
**Suggestion:** Add an explicit type annotation for this local collection
variable to satisfy the type-hinting requirement for relevant variables.
[custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The new code introduces a local collection variable without an explicit type
hint, and the rule requires type hints on relevant Python variables that can be
annotated. This is a real omission in the added code.
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=60aeac47d71a42c980fc90d5e3492b16&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=60aeac47d71a42c980fc90d5e3492b16&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/cli/thumbnails_test.py
**Line:** 52:52
**Comment:**
*Custom Rule: Add an explicit type annotation for this local collection
variable to satisfy the type-hinting 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%2F41530&comment_hash=43d054f9b90d68e4571547d5d49795f5144089fe355320d73a237d73ad0eb637&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41530&comment_hash=43d054f9b90d68e4571547d5d49795f5144089fe355320d73a237d73ad0eb637&reaction=dislike'>👎</a>
##########
tests/unit_tests/cli/thumbnails_test.py:
##########
@@ -0,0 +1,79 @@
+# 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 click.testing import CliRunner
+from pytest_mock import MockerFixture
+
+from superset.cli import thumbnails
+
+
+class _FakeModel:
+ """Stand-in for a Dashboard/Slice that detaches mid-run.
+
+ Once ``detached`` is set, ``str()`` raises -- mimicking the
+ DetachedInstanceError that SQLAlchemy raises when the session that
+ loaded the instance has been closed/expired.
+ """
+
+ def __init__(self, pk: int, label: str) -> None:
+ self.id: int = pk
+ self._label: str = label
+ self.detached: bool = False
+
+ def __str__(self) -> str:
+ if self.detached:
+ raise RuntimeError("Instance is not bound to a Session
(simulated)")
+ return self._label
+
+
+def test_compute_thumbnails_survives_detached_instances(
+ mocker: MockerFixture, app_context: None
+) -> None:
+ """Regression test for the DetachedInstanceError in compute-thumbnails.
+
+ Computing a thumbnail can close/expire the session, detaching the ORM
+ instances. The command must read each model's id and label *before* the
+ compute loop, so building the progress message never touches a detached
+ instance even when several models are processed.
+ """
+ models = [_FakeModel(1, "Dashboard A"), _FakeModel(2, "Dashboard B")]
+
+ query = mocker.MagicMock()
Review Comment:
**Suggestion:** Add a type annotation to this mock variable so the new code
does not omit type hints on relevant local variables. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The mock variable is newly introduced and has no explicit type hint. Since
the rule flags relevant variables that can be annotated, this is a valid
type-hint omission.
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=3a7e45ee5b4e4a978c13a7a31bb1b1b7&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=3a7e45ee5b4e4a978c13a7a31bb1b1b7&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/cli/thumbnails_test.py
**Line:** 54:54
**Comment:**
*Custom Rule: Add a type annotation to this mock variable so the new
code does not omit type hints on relevant local 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%2F41530&comment_hash=48502022b080f3be7e4552653c0021bd7aa681aa1f300b50af8747c2d1cc2faf&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41530&comment_hash=48502022b080f3be7e4552653c0021bd7aa681aa1f300b50af8747c2d1cc2faf&reaction=dislike'>👎</a>
##########
tests/unit_tests/cli/thumbnails_test.py:
##########
@@ -0,0 +1,79 @@
+# 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 click.testing import CliRunner
+from pytest_mock import MockerFixture
+
+from superset.cli import thumbnails
+
+
+class _FakeModel:
+ """Stand-in for a Dashboard/Slice that detaches mid-run.
+
+ Once ``detached`` is set, ``str()`` raises -- mimicking the
+ DetachedInstanceError that SQLAlchemy raises when the session that
+ loaded the instance has been closed/expired.
+ """
+
+ def __init__(self, pk: int, label: str) -> None:
+ self.id: int = pk
+ self._label: str = label
+ self.detached: bool = False
+
+ def __str__(self) -> str:
+ if self.detached:
+ raise RuntimeError("Instance is not bound to a Session
(simulated)")
+ return self._label
+
+
+def test_compute_thumbnails_survives_detached_instances(
+ mocker: MockerFixture, app_context: None
+) -> None:
+ """Regression test for the DetachedInstanceError in compute-thumbnails.
+
+ Computing a thumbnail can close/expire the session, detaching the ORM
+ instances. The command must read each model's id and label *before* the
+ compute loop, so building the progress message never touches a detached
+ instance even when several models are processed.
+ """
+ models = [_FakeModel(1, "Dashboard A"), _FakeModel(2, "Dashboard B")]
+
+ query = mocker.MagicMock()
+ query.filter.return_value = query
+ query.all.return_value = models
+ db_mock = mocker.patch("superset.cli.thumbnails.db")
+ db_mock.session.query.return_value = query
+
+ def _detach_session(_url: object, _model_id: int, force: bool) -> None:
+ # Computing a thumbnail expires the session -> every instance detaches.
+ for model in models:
+ model.detached = True
+
+ cache_dashboard = mocker.patch(
+ "superset.tasks.thumbnails.cache_dashboard_thumbnail",
+ side_effect=_detach_session,
+ )
+
+ result = CliRunner().invoke(thumbnails.compute_thumbnails, ["-d"])
Review Comment:
**Suggestion:** Add a type annotation for this command invocation result
variable to comply with the type-hint rule for relevant variables. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The invocation result is stored in a new local variable without a type hint.
Under the stated rule, this is a valid omission because the variable can be
annotated.
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2d098142c77e4b8084369e2ff721e4bd&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=2d098142c77e4b8084369e2ff721e4bd&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/cli/thumbnails_test.py
**Line:** 70:70
**Comment:**
*Custom Rule: Add a type annotation for this command invocation result
variable to comply with the type-hint rule 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%2F41530&comment_hash=4dcefe10600c8b992874b21df7fa5bff44968a99e89c7857d6ab71df2172e6cb&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41530&comment_hash=4dcefe10600c8b992874b21df7fa5bff44968a99e89c7857d6ab71df2172e6cb&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]