codeant-ai-for-open-source[bot] commented on code in PR #40994:
URL: https://github.com/apache/superset/pull/40994#discussion_r3600399076


##########
superset/cli/importexport.py:
##########
@@ -48,16 +48,38 @@
     is_flag=True,
     help="Force load data even if table already exists",
 )
-def import_directory(directory: str, overwrite: bool, force: bool) -> None:
[email protected](
+    "--username",
+    "-u",
+    required=False,
+    default="admin",
+    help="Specify the user name to assign imported assets to",
+)
+def import_directory(
+    directory: str, overwrite: bool, force: bool, username: Optional[str]
+) -> None:
     """Imports configs from a given directory"""
     # pylint: disable=import-outside-toplevel
     from superset.examples.utils import load_configs_from_directory
 
-    load_configs_from_directory(
-        root=Path(directory),
-        overwrite=overwrite,
-        force_data=force,
-    )
+    user = security_manager.find_user(username=username)

Review Comment:
   **Suggestion:** Add an explicit type annotation for the `user` local 
variable to satisfy the type-hint requirement for relevant newly introduced 
variables. [custom_rule]
   
   **Severity Level:** Minor ๐Ÿงน
   <details>
   <summary><b>Why it matters? โญ </b></summary>
   
   The new local variable `user` is introduced without a type annotation, and 
it is a relevant variable that can be annotated. This matches the Python 
type-hint rule for modified code.
   </details>
   <details>
   <summary><b>Rule source ๐Ÿ“– </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 28)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=f211d3b755c94a048511800200b56f31&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=f211d3b755c94a048511800200b56f31&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/importexport.py
   **Line:** 65:65
   **Comment:**
        *Custom Rule: Add an explicit type annotation for the `user` local 
variable to satisfy the type-hint requirement for relevant newly introduced 
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%2F40994&comment_hash=f63e665b0c0f7f27cedece0ddefb93cbbb2008f1bcf7a12a17dfce9ff04ee088&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40994&comment_hash=f63e665b0c0f7f27cedece0ddefb93cbbb2008f1bcf7a12a17dfce9ff04ee088&reaction=dislike'>๐Ÿ‘Ž</a>



##########
tests/integration_tests/cli_tests.py:
##########
@@ -466,3 +467,118 @@ def 
test_re_encrypt_secrets_engine_option_invalid_raises_usage(
     assert response.exit_code != 0
     assert "Invalid value" in response.output or "Usage:" in response.output
     assert "aes" in response.output or "aes-gcm" in response.output
+
+
[email protected]("superset.examples.utils.load_configs_from_directory")
+def test_import_directory(
+    load_configs_mock: mock.MagicMock,
+    app_context: Any,
+    fs: Any,
+) -> None:
+    """
+    Test that import-directory calls load_configs_from_directory with the
+    correct arguments and assigns assets to the specified user.
+    """
+    # pylint: disable=reimported, redefined-outer-name
+    import superset.cli.importexport  # noqa: F811
+
+    importlib.reload(superset.cli.importexport)
+
+    fake_user = mock.MagicMock()
+    fake_user.username = "admin"
+
+    captured_user: list[Any] = []
+
+    def capture_g_user(**kwargs: Any) -> None:
+        """Capture g.user at call time to verify override_user is active."""
+        captured_user.append(g.user)
+
+    load_configs_mock.side_effect = capture_g_user
+
+    fs.create_dir("/assets")
+    fs.create_file("/assets/metadata.yaml", contents="version: 1.0.0\n")
+
+    runner = current_app.test_cli_runner()
+    with mock.patch.object(
+        superset.cli.importexport, "security_manager"
+    ) as security_manager_mock:
+        security_manager_mock.find_user.return_value = fake_user
+        response = runner.invoke(
+            superset.cli.importexport.import_directory,
+            ("/assets", "-u", "admin"),
+        )
+
+    assert response.exit_code == 0
+    security_manager_mock.find_user.assert_called_once_with(username="admin")
+    load_configs_mock.assert_called_once()
+    call_kwargs = load_configs_mock.call_args

Review Comment:
   **Suggestion:** Add a concrete type annotation for `call_kwargs` so this 
newly added variable is explicitly typed. [custom_rule]
   
   **Severity Level:** Minor ๐Ÿงน
   <details>
   <summary><b>Why it matters? โญ </b></summary>
   
   The newly added `call_kwargs` variable is unannotated even though it can be 
typed. This matches the stated type-hint requirement for new Python code.
   </details>
   <details>
   <summary><b>Rule source ๐Ÿ“– </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 28)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=3f115af0985a4f29b0eae7ea6bf125c1&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=3f115af0985a4f29b0eae7ea6bf125c1&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/integration_tests/cli_tests.py
   **Line:** 514:514
   **Comment:**
        *Custom Rule: Add a concrete type annotation for `call_kwargs` so this 
newly added variable is explicitly typed.
   
   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%2F40994&comment_hash=eb370e1f139237bb99b5ff608a75ee0d257e0c7011ab4d8bf3e037b19db0adeb&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40994&comment_hash=eb370e1f139237bb99b5ff608a75ee0d257e0c7011ab4d8bf3e037b19db0adeb&reaction=dislike'>๐Ÿ‘Ž</a>



##########
tests/integration_tests/cli_tests.py:
##########
@@ -466,3 +467,118 @@ def 
test_re_encrypt_secrets_engine_option_invalid_raises_usage(
     assert response.exit_code != 0
     assert "Invalid value" in response.output or "Usage:" in response.output
     assert "aes" in response.output or "aes-gcm" in response.output
+
+
[email protected]("superset.examples.utils.load_configs_from_directory")
+def test_import_directory(
+    load_configs_mock: mock.MagicMock,
+    app_context: Any,
+    fs: Any,
+) -> None:
+    """
+    Test that import-directory calls load_configs_from_directory with the
+    correct arguments and assigns assets to the specified user.
+    """
+    # pylint: disable=reimported, redefined-outer-name
+    import superset.cli.importexport  # noqa: F811
+
+    importlib.reload(superset.cli.importexport)
+
+    fake_user = mock.MagicMock()

Review Comment:
   **Suggestion:** Add an explicit type annotation for `fake_user` to satisfy 
the rule requiring type-hinted relevant variables in new code. [custom_rule]
   
   **Severity Level:** Minor ๐Ÿงน
   <details>
   <summary><b>Why it matters? โญ </b></summary>
   
   The new variable `fake_user` is introduced without a type hint, and the rule 
explicitly requires type hints for new or modified Python variables that can be 
annotated. This is a real violation.
   </details>
   <details>
   <summary><b>Rule source ๐Ÿ“– </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 28)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=07a387fa80a2465b8cf4ea07362208d3&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=07a387fa80a2465b8cf4ea07362208d3&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/integration_tests/cli_tests.py
   **Line:** 487:487
   **Comment:**
        *Custom Rule: Add an explicit type annotation for `fake_user` to 
satisfy the rule requiring type-hinted relevant variables in new code.
   
   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%2F40994&comment_hash=1882de083986cf13e7aebadb4ae43779d0158d0480c9c769c66b35fcbebf130c&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40994&comment_hash=1882de083986cf13e7aebadb4ae43779d0158d0480c9c769c66b35fcbebf130c&reaction=dislike'>๐Ÿ‘Ž</a>



##########
tests/integration_tests/cli_tests.py:
##########
@@ -466,3 +467,118 @@ def 
test_re_encrypt_secrets_engine_option_invalid_raises_usage(
     assert response.exit_code != 0
     assert "Invalid value" in response.output or "Usage:" in response.output
     assert "aes" in response.output or "aes-gcm" in response.output
+
+
[email protected]("superset.examples.utils.load_configs_from_directory")
+def test_import_directory(
+    load_configs_mock: mock.MagicMock,
+    app_context: Any,
+    fs: Any,
+) -> None:
+    """
+    Test that import-directory calls load_configs_from_directory with the
+    correct arguments and assigns assets to the specified user.
+    """
+    # pylint: disable=reimported, redefined-outer-name
+    import superset.cli.importexport  # noqa: F811
+
+    importlib.reload(superset.cli.importexport)
+
+    fake_user = mock.MagicMock()
+    fake_user.username = "admin"
+
+    captured_user: list[Any] = []
+
+    def capture_g_user(**kwargs: Any) -> None:
+        """Capture g.user at call time to verify override_user is active."""
+        captured_user.append(g.user)
+
+    load_configs_mock.side_effect = capture_g_user
+
+    fs.create_dir("/assets")
+    fs.create_file("/assets/metadata.yaml", contents="version: 1.0.0\n")
+
+    runner = current_app.test_cli_runner()
+    with mock.patch.object(
+        superset.cli.importexport, "security_manager"
+    ) as security_manager_mock:
+        security_manager_mock.find_user.return_value = fake_user
+        response = runner.invoke(
+            superset.cli.importexport.import_directory,
+            ("/assets", "-u", "admin"),
+        )
+
+    assert response.exit_code == 0
+    security_manager_mock.find_user.assert_called_once_with(username="admin")
+    load_configs_mock.assert_called_once()
+    call_kwargs = load_configs_mock.call_args
+    assert str(call_kwargs.kwargs["root"]) == "/assets"
+    assert call_kwargs.kwargs["overwrite"] is False
+    assert call_kwargs.kwargs["force_data"] is False
+    assert captured_user[0] is fake_user
+
+
+def test_import_directory_unknown_user(
+    app_context: Any,
+    fs: Any,
+) -> None:
+    """
+    Test that import-directory fails fast with a clear error when the
+    specified user does not exist.
+    """
+    # pylint: disable=reimported, redefined-outer-name
+    import superset.cli.importexport  # noqa: F811
+
+    importlib.reload(superset.cli.importexport)
+
+    fs.create_dir("/assets")
+    fs.create_file("/assets/metadata.yaml", contents="version: 1.0.0\n")
+
+    runner = current_app.test_cli_runner()
+    with mock.patch.object(
+        superset.cli.importexport, "security_manager"
+    ) as security_manager_mock:
+        security_manager_mock.find_user.return_value = None
+        response = runner.invoke(
+            superset.cli.importexport.import_directory,
+            ("/assets", "-u", "nonexistent"),
+        )
+
+    assert response.exit_code != 0
+    assert "nonexistent" in response.output
+
+
[email protected](
+    "superset.examples.utils.load_configs_from_directory",
+    side_effect=Exception("import failed"),
+)
+def test_failing_import_directory(
+    load_configs_mock: mock.MagicMock,
+    app_context: Any,
+    fs: Any,
+    caplog: Any,
+) -> None:
+    """
+    Test that a failure in import-directory is handled gracefully.
+    """
+    # pylint: disable=reimported, redefined-outer-name
+    import superset.cli.importexport  # noqa: F811
+
+    importlib.reload(superset.cli.importexport)
+
+    fake_user = mock.MagicMock()

Review Comment:
   **Suggestion:** Annotate `fake_user` with an explicit type in this test as 
well, matching the mandatory type-hint policy for new variables. [custom_rule]
   
   **Severity Level:** Minor ๐Ÿงน
   <details>
   <summary><b>Why it matters? โญ </b></summary>
   
   This is the same issue as the earlier `fake_user` assignment: a newly 
introduced variable lacks a type hint, which violates the Python type-hint rule.
   </details>
   <details>
   <summary><b>Rule source ๐Ÿ“– </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 28)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=58275f9f4fec48f2850112a8d174a83e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=58275f9f4fec48f2850112a8d174a83e&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/integration_tests/cli_tests.py
   **Line:** 569:569
   **Comment:**
        *Custom Rule: Annotate `fake_user` with an explicit type in this test 
as well, matching the mandatory type-hint policy for new 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%2F40994&comment_hash=b066816aa34b432650fe4869bf2c493ca186cfb96a3717ff246aa8f0349ab249&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40994&comment_hash=b066816aa34b432650fe4869bf2c493ca186cfb96a3717ff246aa8f0349ab249&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]

Reply via email to