Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions src/a2a/client/client_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import dataclasses
import logging

from collections.abc import Callable
Expand Down Expand Up @@ -239,15 +240,20 @@ def create(
all_extensions = self._config.extensions.copy()
Copy link
Contributor

Choose a reason for hiding this comment

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

low

While the current implementation correctly fixes the mutation bug, the logic for creating the per-call configuration can be simplified for better readability and conciseness. For consistency, prefer using a conditional expression (ternary operator) for this assignment over an if/else block, as per repository guidelines.

config = dataclasses.replace(self._config, extensions=self._config.extensions + extensions) if extensions else self._config
References
  1. For consistency, prefer using conditional expressions (ternary operators) for simple assignments over if/else blocks.

if extensions:
all_extensions.extend(extensions)
self._config.extensions = all_extensions

config = (
dataclasses.replace(self._config, extensions=all_extensions)
if extensions
else self._config
)

transport = self._registry[transport_protocol](
card, transport_url, self._config, interceptors or []
card, transport_url, config, interceptors or []
)

return BaseClient(
card,
self._config,
config,
transport,
all_consumers,
interceptors or [],
Expand Down
17 changes: 17 additions & 0 deletions tests/client/test_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,20 @@ async def test_client_factory_connect_with_consumers_and_interceptors(
call_args = mock_base_client.call_args[0]
assert call_args[3] == [consumer1]
assert call_args[4] == [interceptor1]


def test_client_factory_create_does_not_mutate_config_extensions(
base_agent_card: AgentCard,
):
"""Verify that calling create() with extensions does not mutate the factory's config."""
config = ClientConfig(
httpx_client=httpx.AsyncClient(),
extensions=['base-ext'],
)
factory = ClientFactory(config)

factory.create(base_agent_card, extensions=['ext-a'])
factory.create(base_agent_card, extensions=['ext-b'])

# Config should be unchanged — no accumulation
assert config.extensions == ['base-ext']
Loading