Skip to content

fix: warnings is imported repeatedly inside exception handlers - #182

Open
andrewwhitecdw wants to merge 1 commit into
NVIDIA:mainfrom
andrewwhitecdw:codequality/init-warnings-is-imported-repeatedly-inside
Open

fix: warnings is imported repeatedly inside exception handlers#182
andrewwhitecdw wants to merge 1 commit into
NVIDIA:mainfrom
andrewwhitecdw:codequality/init-warnings-is-imported-repeatedly-inside

Conversation

@andrewwhitecdw

Copy link
Copy Markdown

This PR addresses the following issue in src/tilegym/ops/__init__.py: warnings is imported repeatedly inside exception handlers.

Changes

  • src/tilegym/ops/__init__.py: warnings is imported repeatedly inside exception handlers.

Details

--- a/src/tilegym/ops/__init__.py
+++ b/src/tilegym/ops/__init__.py
@@ -1,31 +1,28 @@
-"""TileGym ops module - contains all operation interfaces and backend implementations"""
-
-from tilegym.backend import is_backend_available
-
-# Backend implementations
-# Import interface modules
-from . import activation
-from . import attn_interface
-
-# Make cutile optional - only import if backend is fully available
-if is_backend_available("cutile"):
-    try:
-        from . import cutile
-    except (ImportError, RuntimeError):
-        import warnings
-
-        warnings.warn("Cutile backend import failed, cutile operations will not be available")
-        cutile = None  # type: ignore
-else:
-    cutile = None  # type: ignore
-
-from . import moe_interface
-
-# Make triton optional - only import if the library is available
-try:
-    from . import triton
-except (ImportError, RuntimeError):
-    import warnings
-
-    warnings.warn("Triton backend import failed, triton operations will not be available")
-    triton = None  # type: ignore
+"""TileGym ops module - contains all operation interfaces and backend implementations"""
+
+import warnings
+from tilegym.backend import is_backend_available
+
+# Backend implementations
+# Import interface modules
+from . import activation
+from . import attn_interface
+
+# Make cutile optional - only import if backend is fully available
+if is_backend_available("cutile"):
+    try:
+        from . import cutile
+    except (ImportError, RuntimeError):
+        warnings.warn("Cutile backend import failed, cutile operations will not be available")
+        cutile = None  # type: ignore
+else:
+    cutile = None  # type: ignore
+
+from . import moe_interface
+
+# Make triton optional - only import if the library is available
+try:
+    from . import triton
+except (ImportError, RuntimeError):
+    warnings.warn("Triton backend import failed, triton operations will not be available")
+    triton = None  # type: ignore

Tests

  • tests/test_ops_warnings_import.py
--- /dev/null
+++ b/tests/test_ops_warnings_import.py
@@ -0,0 +1,28 @@
+# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+#
+# SPDX-License-Identifier: MIT
+
+import ast
+import inspect
+from pathlib import Path
+
+import tilegym.ops
+
+
+def test_warnings_imported_once_at_module_level():
+    """Verify warnings is imported once at module level, not inside except handlers."""
+    source_path = Path(inspect.getfile(tilegym.ops))
+    tree = ast.parse(source_path.read_text())
+
+    def add_parents(node, parent=None):
+        node.parent = parent
+        for child in ast.iter_child_nodes(node):
+            add_parents(child, node)
+
+    add_parents(tree)
+
+    warnings_imports = [
+        node
+        for node in ast.walk(tree)
+        if isinstance(node, ast.Import)
+        and any(alias.name == "warnings" for alias in node.names)
+    ]
+
+    assert len(warnings_imports) == 1
+    assert isinstance(warnings_imports[0].parent, ast.Module)

Contributor guidelines

Per this repo's CONTRIBUTING.md:

  • All commits are signed off (Signed-off-by trailer, DCO).

Signed-off-by: andrewwhitecdw <andrewwhitecdw@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant