From 6e6806880b1fa0a43d63a97b937461d688e62ea0 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 7 Nov 2024 15:03:38 +0300 Subject: [PATCH 1/3] fix: fixes typing issues discovered from github api generation --- .../kiota_abstractions/request_adapter.py | 2 +- .../kiota_abstractions/request_information.py | 17 ++++++----------- .../serialization/parse_node.py | 4 ++-- .../httpx/kiota_http/httpx_request_adapter.py | 2 +- .../kiota_serialization_form/form_parse_node.py | 4 ++-- .../kiota_serialization_json/json_parse_node.py | 4 ++-- .../kiota_serialization_text/text_parse_node.py | 4 ++-- 7 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/abstractions/kiota_abstractions/request_adapter.py b/packages/abstractions/kiota_abstractions/request_adapter.py index 7ef551b6..959ba299 100644 --- a/packages/abstractions/kiota_abstractions/request_adapter.py +++ b/packages/abstractions/kiota_abstractions/request_adapter.py @@ -75,7 +75,7 @@ async def send_collection_async( async def send_collection_of_primitive_async( self, request_info: RequestInformation, - response_type: ResponseType, + response_type: type[ResponseType], error_map: Optional[Dict[str, type[ParsableFactory]]], ) -> Optional[List[ResponseType]]: """Excutes the HTTP request specified by the given RequestInformation and returns the diff --git a/packages/abstractions/kiota_abstractions/request_information.py b/packages/abstractions/kiota_abstractions/request_information.py index 590e788c..e32e6dea 100644 --- a/packages/abstractions/kiota_abstractions/request_information.py +++ b/packages/abstractions/kiota_abstractions/request_information.py @@ -28,7 +28,8 @@ from .request_adapter import RequestAdapter Url = str -T = TypeVar("T", bound=Parsable) +T = TypeVar("T", bool, str, int, float, UUID, datetime, timedelta, date, time, bytes) +U = TypeVar("U", bound=Parsable) QueryParameters = TypeVar('QueryParameters') OBSERVABILITY_TRACER_NAME = "microsoft-python-kiota-abstractions" tracer = trace.get_tracer(OBSERVABILITY_TRACER_NAME, VERSION) @@ -155,7 +156,7 @@ def set_content_from_parsable( self, request_adapter: RequestAdapter, content_type: str, - values: Union[T, List[T]], + values: Union[U, List[U]], ) -> None: """Sets the request body from a model with the specified content type. @@ -163,12 +164,12 @@ def set_content_from_parsable( request_adapter (Optional[RequestAdapter]): The adapter service to get the serialization writer from. content_type (Optional[str]): the content type. - values (Union[T, List[T]]): the models. + values (Union[U, List[U]]): the models. """ with tracer.start_as_current_span( self._create_parent_span_name("set_content_from_parsable") ) as span: - writer = self._get_serialization_writer(request_adapter, content_type, values, span) + writer = self._get_serialization_writer(request_adapter, content_type, span) if isinstance(values, MultipartBody): content_type += f"; boundary={values.boundary}" values.request_adapter = request_adapter @@ -198,7 +199,7 @@ def set_content_from_scalar( with tracer.start_as_current_span( self._create_parent_span_name("set_content_from_scalar") ) as span: - writer = self._get_serialization_writer(request_adapter, content_type, values, span) + writer = self._get_serialization_writer(request_adapter, content_type, span) if isinstance(values, list): writer.writer = writer.write_collection_of_primitive_values(None, values) @@ -255,7 +256,6 @@ def _get_serialization_writer( self, request_adapter: Optional["RequestAdapter"], content_type: Optional[str], - values: Union[T, List[T]], parent_span: trace.Span, ): """_summary_ @@ -263,7 +263,6 @@ def _get_serialization_writer( Args: request_adapter (RequestAdapter): _description_ content_type (str): _description_ - values (Union[T, List[T]]): _description_ """ _span = self._start_local_tracing_span("_get_serialization_writer", parent_span) try: @@ -275,10 +274,6 @@ def _get_serialization_writer( exc = ValueError("Content Type cannot be null") _span.record_exception(exc) raise exc - if not values: - exc = ValueError("Values cannot be null") - _span.record_exception(exc) - raise exc return request_adapter.get_serialization_writer_factory( ).get_serialization_writer(content_type) finally: diff --git a/packages/abstractions/kiota_abstractions/serialization/parse_node.py b/packages/abstractions/kiota_abstractions/serialization/parse_node.py index e8c7e841..339a03e8 100644 --- a/packages/abstractions/kiota_abstractions/serialization/parse_node.py +++ b/packages/abstractions/kiota_abstractions/serialization/parse_node.py @@ -118,7 +118,7 @@ def get_time_value(self) -> Optional[time]: pass @abstractmethod - def get_collection_of_primitive_values(self, primitive_type) -> Optional[List[T]]: + def get_collection_of_primitive_values(self, primitive_type: type[T]) -> Optional[List[T]]: """Gets the collection of primitive values of the node Args: primitive_type: The type of primitive to return. @@ -128,7 +128,7 @@ def get_collection_of_primitive_values(self, primitive_type) -> Optional[List[T] pass @abstractmethod - def get_collection_of_object_values(self, factory: ParsableFactory) -> Optional[List[U]]: + def get_collection_of_object_values(self, factory: ParsableFactory[U]) -> Optional[List[U]]: """Gets the collection of model object values of the node Args: factory (ParsableFactory): The factory to use to create the model object. diff --git a/packages/http/httpx/kiota_http/httpx_request_adapter.py b/packages/http/httpx/kiota_http/httpx_request_adapter.py index 533d085d..2d1fbcf1 100644 --- a/packages/http/httpx/kiota_http/httpx_request_adapter.py +++ b/packages/http/httpx/kiota_http/httpx_request_adapter.py @@ -250,7 +250,7 @@ async def send_collection_async( async def send_collection_of_primitive_async( self, request_info: RequestInformation, - response_type: ResponseType, + response_type: type[ResponseType], error_map: Optional[Dict[str, type[ParsableFactory]]], ) -> Optional[List[ResponseType]]: """Excutes the HTTP request specified by the given RequestInformation and returns the diff --git a/packages/serialization/form/kiota_serialization_form/form_parse_node.py b/packages/serialization/form/kiota_serialization_form/form_parse_node.py index 93798109..09ab1479 100644 --- a/packages/serialization/form/kiota_serialization_form/form_parse_node.py +++ b/packages/serialization/form/kiota_serialization_form/form_parse_node.py @@ -167,7 +167,7 @@ def get_child_node(self, field_name: str) -> Optional[ParseNode]: return FormParseNode(self._fields[field_name]) return None - def get_collection_of_primitive_values(self, primitive_type: type) -> Optional[List[T]]: + def get_collection_of_primitive_values(self, primitive_type: type[T]) -> Optional[List[T]]: """Gets the collection of primitive values of the node Args: primitive_type: The type of primitive to return. @@ -189,7 +189,7 @@ def get_collection_of_primitive_values(self, primitive_type: type) -> Optional[L return result raise Exception(f"Encountered an unknown type during deserialization {primitive_type}") - def get_collection_of_object_values(self, factory: ParsableFactory) -> Optional[List[U]]: + def get_collection_of_object_values(self, factory: ParsableFactory[U]) -> Optional[List[U]]: raise Exception("Collection of object values is not supported with uri form encoding.") def get_collection_of_enum_values(self, enum_class: K) -> Optional[List[K]]: diff --git a/packages/serialization/json/kiota_serialization_json/json_parse_node.py b/packages/serialization/json/kiota_serialization_json/json_parse_node.py index cb561aa1..e147e940 100644 --- a/packages/serialization/json/kiota_serialization_json/json_parse_node.py +++ b/packages/serialization/json/kiota_serialization_json/json_parse_node.py @@ -139,7 +139,7 @@ def get_time_value(self) -> Optional[time]: return datetime_obj return None - def get_collection_of_primitive_values(self, primitive_type: Any) -> Optional[List[T]]: + def get_collection_of_primitive_values(self, primitive_type: type[T]) -> Optional[List[T]]: """Gets the collection of primitive values of the node Args: primitive_type: The type of primitive to return. @@ -161,7 +161,7 @@ def func(item): return list(map(func, json.loads(self._json_node))) return list(map(func, list(self._json_node))) - def get_collection_of_object_values(self, factory: ParsableFactory) -> Optional[List[U]]: + def get_collection_of_object_values(self, factory: ParsableFactory[U]) -> Optional[List[U]]: """Gets the collection of type U values from the json node Returns: List[U]: The collection of model object values of the node diff --git a/packages/serialization/text/kiota_serialization_text/text_parse_node.py b/packages/serialization/text/kiota_serialization_text/text_parse_node.py index c2aca3f7..76f7d7a3 100644 --- a/packages/serialization/text/kiota_serialization_text/text_parse_node.py +++ b/packages/serialization/text/kiota_serialization_text/text_parse_node.py @@ -133,7 +133,7 @@ def get_time_value(self) -> Optional[time]: return datetime_obj.time() return None - def get_collection_of_primitive_values(self, primitive_type) -> List[T]: + def get_collection_of_primitive_values(self, primitive_type: type[T]) -> List[T]: """Gets the collection of primitive values of the node Args: primitive_type: The type of primitive to return. @@ -142,7 +142,7 @@ def get_collection_of_primitive_values(self, primitive_type) -> List[T]: """ raise Exception(self.NO_STRUCTURED_DATA_MESSAGE) - def get_collection_of_object_values(self, factory: ParsableFactory) -> List[U]: + def get_collection_of_object_values(self, factory: ParsableFactory[U]) -> List[U]: """Gets the collection of type U values from the text node Returns: List[U]: The collection of model object values of the node From b3afe83e8ee979a246ed15d24490f6ac8df546bb Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 7 Nov 2024 16:00:05 +0300 Subject: [PATCH 2/3] fix: type information for collection of primitives --- .../kiota_abstractions/request_adapter.py | 13 +++++++------ .../http/httpx/kiota_http/httpx_request_adapter.py | 10 +++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/abstractions/kiota_abstractions/request_adapter.py b/packages/abstractions/kiota_abstractions/request_adapter.py index 959ba299..c518ed5d 100644 --- a/packages/abstractions/kiota_abstractions/request_adapter.py +++ b/packages/abstractions/kiota_abstractions/request_adapter.py @@ -1,8 +1,9 @@ from __future__ import annotations from abc import ABC, abstractmethod -from datetime import datetime +from datetime import date, datetime, time, timedelta from io import BytesIO from typing import Dict, Generic, List, Optional, TypeVar, Union +from uuid import UUID from .request_information import RequestInformation from .serialization import Parsable, ParsableFactory, SerializationWriterFactory @@ -11,7 +12,7 @@ ResponseType = TypeVar("ResponseType") ModelType = TypeVar("ModelType", bound=Parsable) RequestType = TypeVar("RequestType") - +PrimitiveType = TypeVar("PrimitiveType", bool, str, int, float, UUID, datetime, timedelta, date, time, bytes) class RequestAdapter(ABC, Generic[RequestType]): """Service responsible for translating abstract Request Info into concrete native HTTP requests. @@ -75,21 +76,21 @@ async def send_collection_async( async def send_collection_of_primitive_async( self, request_info: RequestInformation, - response_type: type[ResponseType], + response_type: type[PrimitiveType], error_map: Optional[Dict[str, type[ParsableFactory]]], - ) -> Optional[List[ResponseType]]: + ) -> Optional[List[PrimitiveType]]: """Excutes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection. Args: request_info (RequestInformation): the request info to execute. - response_type (ResponseType): the class of the response model to deserialize the + response_type (PrimitiveType): the class of the response model to deserialize the response into. error_map (Optional[Dict[str, type[ParsableFactory]]]): the error dict to use in case of a failed request. Returns: - Optional[List[ModelType]]: The deserialized response model collection. + Optional[List[PrimitiveType]]: The deserialized primitive collection. """ pass diff --git a/packages/http/httpx/kiota_http/httpx_request_adapter.py b/packages/http/httpx/kiota_http/httpx_request_adapter.py index 2d1fbcf1..7f7fd79c 100644 --- a/packages/http/httpx/kiota_http/httpx_request_adapter.py +++ b/packages/http/httpx/kiota_http/httpx_request_adapter.py @@ -11,7 +11,7 @@ ) from kiota_abstractions.api_error import APIError from kiota_abstractions.authentication import AuthenticationProvider -from kiota_abstractions.request_adapter import RequestAdapter, ResponseType +from kiota_abstractions.request_adapter import RequestAdapter, ResponseType,PrimitiveType from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.serialization import ( Parsable, @@ -250,20 +250,20 @@ async def send_collection_async( async def send_collection_of_primitive_async( self, request_info: RequestInformation, - response_type: type[ResponseType], + response_type: type[PrimitiveType], error_map: Optional[Dict[str, type[ParsableFactory]]], - ) -> Optional[List[ResponseType]]: + ) -> Optional[List[PrimitiveType]]: """Excutes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection. Args: request_info (RequestInformation): the request info to execute. - response_type (ResponseType): the class of the response model + response_type (PrimitiveType): the class of the response model to deserialize the response into. error_map (Dict[str, type[ParsableFactory]]): the error dict to use in case of a failed request. Returns: - Optional[List[ResponseType]]: he deserialized response model collection. + Optional[List[PrimitiveType]]: The deserialized primitive type collection. """ parent_span = self.start_tracing_span(request_info, "send_collection_of_primitive_async") try: From 52598b3564a8be5e1cf813ff638ed77080800dd0 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 7 Nov 2024 16:06:04 +0300 Subject: [PATCH 3/3] chore: fix formatting --- packages/abstractions/kiota_abstractions/request_adapter.py | 5 ++++- packages/http/httpx/kiota_http/httpx_request_adapter.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/abstractions/kiota_abstractions/request_adapter.py b/packages/abstractions/kiota_abstractions/request_adapter.py index c518ed5d..8cf6d14d 100644 --- a/packages/abstractions/kiota_abstractions/request_adapter.py +++ b/packages/abstractions/kiota_abstractions/request_adapter.py @@ -12,7 +12,10 @@ ResponseType = TypeVar("ResponseType") ModelType = TypeVar("ModelType", bound=Parsable) RequestType = TypeVar("RequestType") -PrimitiveType = TypeVar("PrimitiveType", bool, str, int, float, UUID, datetime, timedelta, date, time, bytes) +PrimitiveType = TypeVar( + "PrimitiveType", bool, str, int, float, UUID, datetime, timedelta, date, time, bytes +) + class RequestAdapter(ABC, Generic[RequestType]): """Service responsible for translating abstract Request Info into concrete native HTTP requests. diff --git a/packages/http/httpx/kiota_http/httpx_request_adapter.py b/packages/http/httpx/kiota_http/httpx_request_adapter.py index 7f7fd79c..48c9e6a0 100644 --- a/packages/http/httpx/kiota_http/httpx_request_adapter.py +++ b/packages/http/httpx/kiota_http/httpx_request_adapter.py @@ -11,7 +11,7 @@ ) from kiota_abstractions.api_error import APIError from kiota_abstractions.authentication import AuthenticationProvider -from kiota_abstractions.request_adapter import RequestAdapter, ResponseType,PrimitiveType +from kiota_abstractions.request_adapter import RequestAdapter, ResponseType, PrimitiveType from kiota_abstractions.request_information import RequestInformation from kiota_abstractions.serialization import ( Parsable,