codeant-ai-for-open-source[bot] commented on code in PR #40878: URL: https://github.com/apache/superset/pull/40878#discussion_r3377481432
########## tests/unit_tests/tasks/test_get_current_user.py: ########## @@ -0,0 +1,165 @@ +# 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. +"""Testes unitários para get_current_user (superset/tasks/utils.py). + +Técnicas aplicadas: + - Caixa-preta : particionamento de equivalência (CV1, CI1-CI3) + - Caixa-branca: cobertura de branch + MC/DC (D1, D2) + - Isolamento : substituição de g via Flask app_context + patch direto +""" + +import importlib.util +import pathlib +import sys +import types +from unittest.mock import MagicMock, patch + +import pytest +from flask import Flask + + +def _stub(name, **attrs): + mod = types.ModuleType(name) + mod.__dict__.update(attrs) + sys.modules.setdefault(name, mod) + return mod + + +_stub("celery") +_stub("celery.utils") +_stub("celery.utils.log", get_task_logger=lambda n: MagicMock()) +_stub("superset_core") +_stub("superset_core.tasks") +_stub("superset_core.tasks.types", TaskProperties=dict, TaskScope=MagicMock()) +_stub( + "superset.tasks.exceptions", + ExecutorNotFoundError=Exception, + InvalidExecutorError=Exception, +) +_stub( + "superset.tasks.types", + ChosenExecutor=MagicMock(), + Executor=MagicMock(), + ExecutorType=MagicMock(), + FixedExecutor=MagicMock(), +) +_stub("superset.utils") +_stub( + "superset.utils.json", + loads=MagicMock(), + dumps=MagicMock(), + JSONDecodeError=ValueError, +) +_stub("superset.utils.hashing", hash_from_str=MagicMock(return_value="abc" * 30)) +_stub("superset.utils.urls", get_url_path=MagicMock()) + +_path = pathlib.Path(__file__).parents[3] / "superset" / "tasks" / "utils.py" +_spec = importlib.util.spec_from_file_location("superset.tasks.utils", _path) +assert _spec is not None +assert _spec.loader is not None +_mod = importlib.util.module_from_spec(_spec) +_spec.loader.exec_module(_mod) Review Comment: **Suggestion:** This test bootstraps `superset.tasks.utils` via `exec_module` after stubbing only deep submodules, but it does not stub the parent `superset` package chain. During `from superset.tasks.exceptions import ...`, Python can still import the real `superset` package (triggering heavy app/bootstrap imports from `superset/__init__.py`), which makes this unit test fail in minimal environments and introduces import-order fragility. Stub the parent packages (`superset`, `superset.tasks`) or load the target module under an isolated module name with a fully patched module graph. [import error] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - CRIT: Unit tests import full Superset package unnecessarily. - WARN: Test requires Celery and SQLAlchemy just to import. - WARN: Harder to run test in minimal environments. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Import `tests/unit_tests/tasks/test_get_current_user.py`, which executes the `_stub` and import bootstrapping logic at lines 42–75 (notably `_stub("superset.tasks.exceptions", ...)` at lines 48–52 and `_spec.loader.exec_module(_mod)` at line 75). 2. During `_spec.loader.exec_module(_mod)`, the real module `superset/tasks/utils.py` is executed under the name `"superset.tasks.utils"` (see `superset/tasks/utils.py` lines 27–40 for its top-level imports). 3. While executing `superset/tasks/utils.py`, the `from superset.tasks.exceptions import ExecutorNotFoundError, InvalidExecutorError` statement at line 31 triggers Python's import machinery to load the real `superset` package hierarchy, executing `superset/__init__.py` lines 17–31, which in turn imports `superset.app` and `superset.extensions`. 4. Importing `superset.extensions` at `superset/extensions/__init__.py` lines 17–61 and 139–160 runs significant side-effectful initialization (Celery app, SQLAlchemy `db`, cache manager, feature flags, etc.), meaning every run of this unit test implicitly bootstraps the full Superset package instead of remaining isolated to `superset.tasks.utils`. ``` </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=28c6940ada764a4fabe33ed807717990&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=28c6940ada764a4fabe33ed807717990&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_get_current_user.py **Line:** 45:75 **Comment:** *Import Error: This test bootstraps `superset.tasks.utils` via `exec_module` after stubbing only deep submodules, but it does not stub the parent `superset` package chain. During `from superset.tasks.exceptions import ...`, Python can still import the real `superset` package (triggering heavy app/bootstrap imports from `superset/__init__.py`), which makes this unit test fail in minimal environments and introduces import-order fragility. Stub the parent packages (`superset`, `superset.tasks`) or load the target module under an isolated module name with a fully patched module graph. 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%2F40878&comment_hash=1d125d95a5d978d0db255563128e14725e763abebfca9a280334e05988c9b764&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40878&comment_hash=1d125d95a5d978d0db255563128e14725e763abebfca9a280334e05988c9b764&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]
