Skip to content

Commit 9fa773a

Browse files
author
Martin Lambertsen
authored
fix: avoid using default mutable parameters
Using mutable default arguments is a common Python problem, see e.g. https://docs.python-guide.org/writing/gotchas/@mutable-default-arguments In this specific case the default argument even tries to setup some infrastructure settings at import time, which can potentially fail.
1 parent a19e3b1 commit 9fa773a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/msgraph_core/base_graph_request_adapter.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
SerializationWriterFactoryRegistry,
88
)
99
from kiota_http.httpx_request_adapter import HttpxRequestAdapter
10+
from typing import Optional
1011

1112
from .graph_client_factory import GraphClientFactory
1213

@@ -16,11 +17,16 @@ class BaseGraphRequestAdapter(HttpxRequestAdapter):
1617
def __init__(
1718
self,
1819
authentication_provider: AuthenticationProvider,
19-
parse_node_factory: ParseNodeFactory = ParseNodeFactoryRegistry(),
20-
serialization_writer_factory:
21-
SerializationWriterFactory = SerializationWriterFactoryRegistry(),
22-
http_client: httpx.AsyncClient = GraphClientFactory.create_with_default_middleware()
20+
parse_node_factory: Optional[ParseNodeFactory] = None,
21+
serialization_writer_factory: Optional[SerializationWriterFactory] = None,
22+
http_client: Optional[httpx.AsyncClient] = None
2323
) -> None:
24+
if parse_node_factory is None:
25+
parse_node_factory = ParseNodeFactoryRegistry()
26+
if serialization_writer_factory is None:
27+
serialization_writer_factory = SerializationWriterFactoryRegistry()
28+
if http_client is None:
29+
http_client = GraphClientFactory.create_with_default_middleware()
2430
super().__init__(
2531
authentication_provider=authentication_provider,
2632
parse_node_factory=parse_node_factory,

src/msgraph_core/graph_client_factory.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GraphClientFactory(KiotaClientFactory):
2424
@staticmethod
2525
def create_with_default_middleware(
2626
api_version: APIVersion = APIVersion.v1,
27-
client: httpx.AsyncClient = KiotaClientFactory.get_default_client(),
27+
client: Optional[httpx.AsyncClient] = None,
2828
host: NationalClouds = NationalClouds.Global,
2929
options: Optional[Dict[str, RequestOption]] = None
3030
) -> httpx.AsyncClient:
@@ -44,6 +44,8 @@ def create_with_default_middleware(
4444
Returns:
4545
httpx.AsyncClient: An instance of the AsyncClient object
4646
"""
47+
if client is None:
48+
client = KiotaClientFactory.get_default_client()
4749
client.base_url = GraphClientFactory._get_base_url(host, api_version) # type: ignore
4850
middleware = KiotaClientFactory.get_default_middleware(options)
4951
telemetry_handler = GraphClientFactory._get_telemetry_handler(options)
@@ -54,7 +56,7 @@ def create_with_default_middleware(
5456
def create_with_custom_middleware(
5557
middleware: Optional[List[BaseMiddleware]],
5658
api_version: APIVersion = APIVersion.v1,
57-
client: httpx.AsyncClient = KiotaClientFactory.get_default_client(),
59+
client: Optional[httpx.AsyncClient] = None,
5860
host: NationalClouds = NationalClouds.Global,
5961
) -> httpx.AsyncClient:
6062
"""Applies a custom middleware chain to the HTTP Client
@@ -70,6 +72,8 @@ def create_with_custom_middleware(
7072
host (NationalClouds): The national clound endpoint to be used.
7173
Defaults to NationalClouds.Global.
7274
"""
75+
if client is None:
76+
client = KiotaClientFactory.get_default_client()
7377
client.base_url = GraphClientFactory._get_base_url(host, api_version) # type: ignore
7478
return GraphClientFactory._load_middleware_to_client(client, middleware)
7579

0 commit comments

Comments
 (0)