Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/integration/defs/trt_test_alternative.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ def get_pytest_timeout(timeout=None):
if timeout:
return timeout

# Check pytest markers first (ensures "TIMEOUT" in l0_*.yml etc. is respected)
try:
import pytest
marks = None
try:
current_item = pytest.current_test
if hasattr(current_item, 'iter_markers'):
marks = list(current_item.iter_markers('timeout'))
except (AttributeError, NameError):
pass

if marks and len(marks) > 0:
timeout_mark = marks[0]
timeout_pytest = timeout_mark.args[0] if timeout_mark.args else None
if timeout_pytest and isinstance(timeout_pytest, (int, float)):
return timeout_pytest

except (ImportError, Exception):
pass

Comment on lines +336 to +355
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Fix incorrect pytest API usage.

The code attempts to use pytest.current_test which doesn't exist in pytest's API. This will always raise an AttributeError and fall through to the command-line parsing logic.

The correct approaches to access the current test item in pytest are:

  1. Using pytest's internal API (if available in the execution context):
-        try:
-            current_item = pytest.current_test
-            if hasattr(current_item, 'iter_markers'):
-                marks = list(current_item.iter_markers('timeout'))
-        except (AttributeError, NameError):
-            pass
+        try:
+            # Access current test item through pytest's internal state
+            import _pytest.main
+            if hasattr(_pytest.main, '_current_item'):
+                current_item = _pytest.main._current_item
+                if hasattr(current_item, 'iter_markers'):
+                    marks = list(current_item.iter_markers('timeout'))
+        except (AttributeError, NameError):
+            pass
  1. Alternative approach using pytest's config (more reliable):
+        try:
+            # Try to get timeout from pytest configuration
+            config = pytest.config if hasattr(pytest, 'config') else None
+            if config and hasattr(config, 'getoption'):
+                pytest_timeout = config.getoption('--timeout', default=None)
+                if pytest_timeout and isinstance(pytest_timeout, (int, float)):
+                    return pytest_timeout
+        except (AttributeError, NameError):
+            pass

Let me verify the correct pytest API for accessing current test context:


🏁 Script executed:

#!/bin/bash
# Check pytest source code for current test access patterns
rg -A 10 -B 5 "current.*test|test.*current" --type py | head -50

Length of output: 4706


Invalid pytest API usage in get_pytest_timeout
The code on lines 336–355 calls pytest.current_test, which doesn’t exist in pytest’s public API. Consequently, marker-based timeouts never get applied and the block is effectively dead.

To address this:

  • Remove or disable the entire pytest.current_test block.
  • If you really need to support pytest markers, you must run under pytest with a plugin or fixture and then pass the timeout into your harness. For example:
    • Use the pytest-timeout plugin and read its --timeout option via a fixture (pytestconfig.getoption("timeout")).
    • Or define a custom hook/fixture that extracts the marker value and exposes it to your integration code.
  • Otherwise, read the timeout from an environment variable or a command-line argument in your integration runner.

Suggested diff to remove the invalid block:

-    # Check pytest markers first (ensures "TIMEOUT" in l0_*.yml etc. is respected)
-    try:
-        import pytest
-        marks = None
-        try:
-            current_item = pytest.current_test
-            if hasattr(current_item, 'iter_markers'):
-                marks = list(current_item.iter_markers('timeout'))
-        except (AttributeError, NameError):
-            pass
-
-        if marks and len(marks) > 0:
-            timeout_mark = marks[0]
-            timeout_pytest = timeout_mark.args[0] if timeout_mark.args else None
-            if timeout_pytest and isinstance(timeout_pytest, (int, float)):
-                return timeout_pytest
-
-    except (ImportError, Exception):
-        pass

Replace with a mechanism that actually runs under pytest and can access markers (e.g., via pytest fixtures or plugins), or rely on environment/CLI inputs.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Check pytest markers first (ensures "TIMEOUT" in l0_*.yml etc. is respected)
try:
import pytest
marks = None
try:
current_item = pytest.current_test
if hasattr(current_item, 'iter_markers'):
marks = list(current_item.iter_markers('timeout'))
except (AttributeError, NameError):
pass
if marks and len(marks) > 0:
timeout_mark = marks[0]
timeout_pytest = timeout_mark.args[0] if timeout_mark.args else None
if timeout_pytest and isinstance(timeout_pytest, (int, float)):
return timeout_pytest
except (ImportError, Exception):
pass
🤖 Prompt for AI Agents
In tests/integration/defs/trt_test_alternative.py around lines 336 to 355, the
code incorrectly uses pytest.current_test, which is not part of pytest's public
API, causing marker-based timeouts to never apply. Remove the entire block that
tries to access pytest.current_test and instead implement timeout retrieval by
either using pytest-timeout plugin with a fixture to get the timeout option or
by reading the timeout value from an environment variable or command-line
argument in your test harness.

try:
import sys
for i, arg in enumerate(sys.argv):
Expand Down