Skip to content

Commit 98b0426

Browse files
seanzhougooglecopybara-github
authored andcommitted
chore: Make UT of a2a consistent about how tests should be skipped when python verison < 3.10
PiperOrigin-RevId: 801040421
1 parent 2eddc5e commit 98b0426

File tree

11 files changed

+202
-328
lines changed

11 files changed

+202
-328
lines changed

tests/unittests/a2a/converters/test_event_converter.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,10 @@
4949
from google.adk.events.event_actions import EventActions
5050
except ImportError as e:
5151
if sys.version_info < (3, 10):
52-
# Create dummy classes to prevent NameError during test collection
53-
# Tests will be skipped anyway due to pytestmark
54-
class DummyTypes:
55-
pass
56-
57-
DataPart = DummyTypes()
58-
Message = DummyTypes()
59-
Role = DummyTypes()
60-
Task = DummyTypes()
61-
TaskState = DummyTypes()
62-
TaskStatusUpdateEvent = DummyTypes()
63-
_create_artifact_id = lambda *args: None
64-
_create_error_status_event = lambda *args: None
65-
_create_status_update_event = lambda *args: None
66-
_get_adk_metadata_key = lambda *args: None
67-
_get_context_metadata = lambda *args: None
68-
_process_long_running_tool = lambda *args: None
69-
_serialize_metadata_value = lambda *args: None
70-
ADK_METADATA_KEY_PREFIX = "adk_"
71-
ARTIFACT_ID_SEPARATOR = "_"
72-
convert_event_to_a2a_events = lambda *args: None
73-
convert_event_to_a2a_message = lambda *args: None
74-
convert_a2a_task_to_event = lambda *args: None
75-
DEFAULT_ERROR_MESSAGE = "error"
76-
InvocationContext = DummyTypes()
77-
Event = DummyTypes()
78-
EventActions = DummyTypes()
79-
types = DummyTypes()
52+
# Imports are not needed since tests will be skipped due to pytestmark.
53+
# The imported names are only used within test methods, not at module level,
54+
# so no NameError occurs during module compilation.
55+
pass
8056
else:
8157
raise e
8258

tests/unittests/a2a/converters/test_part_converter.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,10 @@
3838
from google.genai import types as genai_types
3939
except ImportError as e:
4040
if sys.version_info < (3, 10):
41-
# Create dummy classes to prevent NameError during test collection
42-
# Tests will be skipped anyway due to pytestmark
43-
class DummyTypes:
44-
pass
45-
46-
a2a_types = DummyTypes()
47-
genai_types = DummyTypes()
48-
A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL = "function_call"
49-
A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE = "function_response"
50-
A2A_DATA_PART_METADATA_TYPE_CODE_EXECUTION_RESULT = "code_execution_result"
51-
A2A_DATA_PART_METADATA_TYPE_EXECUTABLE_CODE = "executable_code"
52-
A2A_DATA_PART_METADATA_TYPE_KEY = "type"
53-
convert_a2a_part_to_genai_part = lambda x: None
54-
convert_genai_part_to_a2a_part = lambda x: None
55-
_get_adk_metadata_key = lambda x: f"adk_{x}"
41+
# Imports are not needed since tests will be skipped due to pytestmark.
42+
# The imported names are only used within test methods, not at module level,
43+
# so no NameError occurs during module compilation.
44+
pass
5645
else:
5746
raise e
5847

tests/unittests/a2a/converters/test_request_converter.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# Skip all tests in this module if Python version is less than 3.10
2222
pytestmark = pytest.mark.skipif(
23-
sys.version_info < (3, 10), reason="A2A tool requires Python 3.10+"
23+
sys.version_info < (3, 10), reason="A2A requires Python 3.10+"
2424
)
2525

2626
# Import dependencies with version checking
@@ -32,17 +32,10 @@
3232
from google.genai import types as genai_types
3333
except ImportError as e:
3434
if sys.version_info < (3, 10):
35-
# Create dummy classes to prevent NameError during test collection
36-
# Tests will be skipped anyway due to pytestmark
37-
class DummyTypes:
38-
pass
39-
40-
a2a_types = DummyTypes()
41-
genai_types = DummyTypes()
42-
RequestContext = DummyTypes()
43-
RunConfig = DummyTypes()
44-
_get_user_id = lambda x: None
45-
convert_a2a_request_to_adk_run_args = lambda x: None
35+
# Imports are not needed since tests will be skipped due to pytestmark.
36+
# The imported names are only used within test methods, not at module level,
37+
# so no NameError occurs during module compilation.
38+
pass
4639
else:
4740
raise e
4841

tests/unittests/a2a/converters/test_utils.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@
2121
sys.version_info < (3, 10), reason="A2A requires Python 3.10+"
2222
)
2323

24-
from google.adk.a2a.converters.utils import _from_a2a_context_id
25-
from google.adk.a2a.converters.utils import _get_adk_metadata_key
26-
from google.adk.a2a.converters.utils import _to_a2a_context_id
27-
from google.adk.a2a.converters.utils import ADK_CONTEXT_ID_PREFIX
28-
from google.adk.a2a.converters.utils import ADK_METADATA_KEY_PREFIX
29-
import pytest
24+
# Import dependencies with version checking
25+
try:
26+
from google.adk.a2a.converters.utils import _from_a2a_context_id
27+
from google.adk.a2a.converters.utils import _get_adk_metadata_key
28+
from google.adk.a2a.converters.utils import _to_a2a_context_id
29+
from google.adk.a2a.converters.utils import ADK_CONTEXT_ID_PREFIX
30+
from google.adk.a2a.converters.utils import ADK_METADATA_KEY_PREFIX
31+
except ImportError as e:
32+
if sys.version_info < (3, 10):
33+
# Imports are not needed since tests will be skipped due to pytestmark.
34+
# The imported names are only used within test methods, not at module level,
35+
# so no NameError occurs during module compilation.
36+
pass
37+
else:
38+
raise e
3039

3140

3241
class TestUtilsFunctions:

tests/unittests/a2a/executor/test_a2a_agent_executor.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
# Skip all tests in this module if Python version is less than 3.10
2323
pytestmark = pytest.mark.skipif(
24-
sys.version_info < (3, 10), reason="A2A tool requires Python 3.10+"
24+
sys.version_info < (3, 10), reason="A2A requires Python 3.10+"
2525
)
2626

2727
# Import dependencies with version checking
@@ -37,23 +37,10 @@
3737
from google.adk.runners import Runner
3838
except ImportError as e:
3939
if sys.version_info < (3, 10):
40-
# Create dummy classes to prevent NameError during test collection
41-
# Tests will be skipped anyway due to pytestmark
42-
class DummyTypes:
43-
pass
44-
45-
RequestContext = DummyTypes()
46-
EventQueue = DummyTypes()
47-
Message = DummyTypes()
48-
Role = DummyTypes()
49-
TaskState = DummyTypes()
50-
TaskStatus = DummyTypes()
51-
TaskStatusUpdateEvent = DummyTypes()
52-
TextPart = DummyTypes()
53-
A2aAgentExecutor = DummyTypes()
54-
A2aAgentExecutorConfig = DummyTypes()
55-
Event = DummyTypes()
56-
Runner = DummyTypes()
40+
# Imports are not needed since tests will be skipped due to pytestmark.
41+
# The imported names are only used within test methods, not at module level,
42+
# so no NameError occurs during module compilation.
43+
pass
5744
else:
5845
raise e
5946

tests/unittests/a2a/executor/test_task_result_aggregator.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,32 @@
1717

1818
import pytest
1919

20-
# Skip entire module if Python < 3.10
21-
if sys.version_info < (3, 10):
22-
pytest.skip("A2A requires Python 3.10+", allow_module_level=True)
23-
24-
# Normal imports after the skip
25-
from a2a.types import Message
26-
from a2a.types import Part
27-
from a2a.types import Role
28-
from a2a.types import TaskState
29-
from a2a.types import TaskStatus
30-
from a2a.types import TaskStatusUpdateEvent
31-
from a2a.types import TextPart
32-
from google.adk.a2a.executor.task_result_aggregator import TaskResultAggregator
33-
34-
35-
def create_test_message(text: str) -> Message:
20+
# Skip all tests in this module if Python version is less than 3.10
21+
pytestmark = pytest.mark.skipif(
22+
sys.version_info < (3, 10), reason="A2A requires Python 3.10+"
23+
)
24+
25+
# Import dependencies with version checking
26+
try:
27+
from a2a.types import Message
28+
from a2a.types import Part
29+
from a2a.types import Role
30+
from a2a.types import TaskState
31+
from a2a.types import TaskStatus
32+
from a2a.types import TaskStatusUpdateEvent
33+
from a2a.types import TextPart
34+
from google.adk.a2a.executor.task_result_aggregator import TaskResultAggregator
35+
except ImportError as e:
36+
if sys.version_info < (3, 10):
37+
# Imports are not needed since tests will be skipped due to pytestmark.
38+
# The imported names are only used within test methods, not at module level,
39+
# so no NameError occurs during module compilation.
40+
pass
41+
else:
42+
raise e
43+
44+
45+
def create_test_message(text: str):
3646
"""Helper function to create a test Message object."""
3747
return Message(
3848
message_id="test-msg",

0 commit comments

Comments
 (0)