Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve typing errors in project after merge #331

Merged
merged 6 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
34 changes: 30 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,37 @@ jobs:
working-directory: ${{ matrix.library.path }}
run: |
poetry run pylint ${{ matrix.library.name }} --disable=W --rcfile=.pylintrc
# - name: Static type checking with Mypy
# working-directory: ${{ matrix.library.path }}
# run: |
# poetry run mypy ${{ matrix.library.name }} --ignore-missing-imports
- name: Static type checking with Mypy
working-directory: ${{ matrix.library.path }}
run: |
poetry run mypy ${{ matrix.library.name }}
- name: Run the tests
working-directory: ${{ matrix.library.path }}
run: |
poetry run pytest

validation-workflow-with-generated-code:
runs-on: ubuntu-latest
timeout-minutes: 40
strategy:
max-parallel: 10
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
working-directory: "./tests/validation"
run: |
python -m pip install --upgrade poetry
poetry install
- name: Static type checking with Mypy
working-directory: "./tests/validation"
run: |
poetry run mypy validation

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License.
# See License in the project root for license information.
# ------------------------------------
from typing import Any, Dict, Union
from typing import Any, Dict, Optional, Union

from .request_adapter import RequestAdapter
from .request_information import RequestInformation
Expand All @@ -14,7 +14,7 @@ class BaseRequestBuilder:

def __init__(
self, request_adapter: RequestAdapter, url_template: str,
path_parameters: Union[Dict[str, Any], str]
path_parameters: Optional[Union[Dict[str, Any], str]]
) -> None:
"""Initializes a new instance of the BaseRequestBuilder class."""
if path_parameters is None:
Expand All @@ -28,7 +28,7 @@ def __init__(
raise TypeError("url_template cannot be null.") # Empty string is allowed

# Path parameters for the request
self.path_parameters: Union[Dict[str, Any], str] = path_parameters
self.path_parameters: Dict[str, Any] = path_parameters
# Url template to use to build the URL for the current request builder
self.url_template: str = url_template
# The request adapter to use to execute the requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class NativeResponseHandler(ResponseHandler):
async def handle_response_async(
self,
response: NativeResponseType,
error_map: Optional[Dict[str, Optional[ParsableFactory]]] = None
error_map: Optional[Dict[str, ParsableFactory]] = None
) -> NativeResponseType:
"""Callback method that is invoked when a response is received.
Args:
response (NativeResponseType): The type of the native response object.
error_map (Optional[Dict[str, Optional[ParsableFactory]]]): the error dict to use
error_map (Optional[Dict[str, ParsableFactory]]): the error dict to use
in case of a failed request.
Returns:
Any: The native response object.
Expand Down
38 changes: 20 additions & 18 deletions packages/abstractions/kiota_abstractions/request_adapter.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from datetime import datetime
from io import BytesIO
from typing import Dict, Generic, List, Optional, TypeVar
from typing import Dict, Generic, List, Optional, TypeVar, Union

from .request_information import RequestInformation
from .serialization import Parsable, ParsableFactory, SerializationWriterFactory
from .store import BackingStoreFactory

ResponseType = TypeVar("ResponseType", str, int, float, bool, datetime, BytesIO)
ResponseType = TypeVar("ResponseType")
ModelType = TypeVar("ModelType", bound=Parsable)
RequestType = TypeVar("RequestType")


class RequestAdapter(ABC, Generic[ResponseType, ModelType, RequestType]):
class RequestAdapter(ABC, Generic[RequestType]):
"""Service responsible for translating abstract Request Info into concrete native HTTP requests.
"""
# The base url for every request.
base_url = str
base_url = str()

@abstractmethod
def get_serialization_writer_factory(self) -> SerializationWriterFactory:
Expand All @@ -30,8 +31,8 @@ def get_serialization_writer_factory(self) -> SerializationWriterFactory:

@abstractmethod
async def send_async(
self, request_info: RequestInformation, parsable_factory: ParsableFactory,
error_map: Dict[str, Optional[ParsableFactory]]
self, request_info: RequestInformation, parsable_factory: ParsableFactory[ModelType],
error_map: Optional[Dict[str, type[ParsableFactory]]]
) -> Optional[ModelType]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized response model.
Expand All @@ -40,7 +41,7 @@ async def send_async(
request_info (RequestInformation): the request info to execute.
parsable_factory (ParsableFactory): the class of response model to
deserialize the response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in case
error_map (Optional[Dict[str, type[ParsableFactory]]]): the error dict to use in case
of a failed request.

Returns:
Expand All @@ -52,8 +53,8 @@ async def send_async(
async def send_collection_async(
self,
request_info: RequestInformation,
parsable_factory: ParsableFactory,
error_map: Dict[str, Optional[ParsableFactory]],
parsable_factory: ParsableFactory[ModelType],
error_map: Optional[Dict[str, type[ParsableFactory]]],
) -> Optional[List[ModelType]]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized response model collection.
Expand All @@ -62,7 +63,7 @@ async def send_collection_async(
request_info (RequestInformation): the request info to execute.
parsable_factory (ParsableFactory): the class of response model to
deserialize the response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in
error_map (Optional[Dict[str, type[ParsableFactory]]]): the error dict to use in
case of a failed request.

Returns:
Expand All @@ -75,7 +76,7 @@ async def send_collection_of_primitive_async(
self,
request_info: RequestInformation,
response_type: ResponseType,
error_map: Dict[str, Optional[ParsableFactory]],
error_map: Optional[Dict[str, type[ParsableFactory]]],
) -> Optional[List[ResponseType]]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized response model collection.
Expand All @@ -84,7 +85,7 @@ async def send_collection_of_primitive_async(
request_info (RequestInformation): the request info to execute.
response_type (ResponseType): the class of the response model to deserialize the
response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in
error_map (Optional[Dict[str, type[ParsableFactory]]]): the error dict to use in
case of a failed request.

Returns:
Expand All @@ -94,17 +95,17 @@ async def send_collection_of_primitive_async(

@abstractmethod
async def send_primitive_async(
self, request_info: RequestInformation, response_type: ResponseType,
error_map: Dict[str, Optional[ParsableFactory]]
self, request_info: RequestInformation, response_type: str,
error_map: Optional[Dict[str, type[ParsableFactory]]]
) -> Optional[ResponseType]:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized primitive response model.

Args:
request_info (RequestInformation): the request info to execute.
response_type (ResponseType): the class of the response model to deserialize the
response_type (str): the class name of the response model to deserialize the
response into.
error_map (Dict[str, Optional[ParsableFactory]]): the error dict to use in
error_map (Optional[Dict[str, type[ParsableFactory]]]): the error dict to use in
case of a failed request.

Returns:
Expand All @@ -114,14 +115,15 @@ async def send_primitive_async(

@abstractmethod
async def send_no_response_content_async(
self, request_info: RequestInformation, error_map: Dict[str, Optional[ParsableFactory]]
self, request_info: RequestInformation, error_map: Optional[Dict[str,
type[ParsableFactory]]]
) -> None:
"""Excutes the HTTP request specified by the given RequestInformation and returns the
deserialized primitive response model.

Args:
request_info (RequestInformation):the request info to execute.
error_map (Dict[str, Optional[Optional[ParsableFactory]]): the error dict to use in
error_map (Optional[Dict[str, type[ParsableFactory]]]): the error dict to use in
case of a failed request.
"""
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(
# The Request Body
self.content: Optional[bytes] = None

def configure(self, request_configuration: RequestConfiguration) -> None:
def configure(self, request_configuration: Optional[RequestConfiguration]) -> None:
"""Configures the current request information headers, query parameters, and options
based on the request configuration provided

Expand Down
5 changes: 2 additions & 3 deletions packages/abstractions/kiota_abstractions/response_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ class ResponseHandler(ABC):

@abstractmethod
async def handle_response_async(
self, response: NativeResponseType, error_map: Optional[Dict[str,
Optional[ParsableFactory]]]
self, response: NativeResponseType, error_map: Optional[Dict[str, ParsableFactory]]
) -> Any:
"""Callback method that is invoked when a response is received.
Args:
response (NativeResponseType): The type of the native response object.
error_map (Optional[Dict[str, Optional[ParsableFactory]]]): the error dict to use
error_map (Optional[Dict[str, ParsableFactory]]]): the error dict to use
in case of a failed request.
Returns:
Any: The deserialized response.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from abc import abstractmethod
from typing import Optional
from typing import Generic, Optional, Protocol, TypeVar

from .parsable import Parsable
from .parse_node import ParseNode

U_co = TypeVar("U_co", bound="Parsable", covariant=True)

class ParsableFactory(Parsable):

class ParsableFactory(Protocol, Generic[U_co]):
"""Defines the factory for creating parsable objects.
"""

@staticmethod
@abstractmethod
def create_from_discriminator_value(parse_node: Optional[ParseNode]) -> Parsable:
def create_from_discriminator_value(parse_node: ParseNode) -> U_co:
"""Create a new parsable object from the given serialized data.

Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .parsable import Parsable

T = TypeVar("T")
T = TypeVar("T", bool, str, int, float, UUID, datetime, timedelta, date, time, bytes)

U = TypeVar("U", bound=Parsable)

Expand All @@ -25,7 +25,7 @@ class ParseNode(ABC):
"""

@abstractmethod
def get_str_value(self) -> str:
def get_str_value(self) -> Optional[str]:
"""Gets the string value of the node

Returns:
Expand All @@ -34,7 +34,7 @@ def get_str_value(self) -> str:
pass

@abstractmethod
def get_child_node(self, identifier: str) -> ParseNode:
def get_child_node(self, identifier: str) -> Optional[ParseNode]:
"""Gets a new parse node for the given identifier

Args:
Expand Down Expand Up @@ -118,16 +118,17 @@ def get_time_value(self) -> Optional[time]:
pass

@abstractmethod
def get_collection_of_primitive_values(self) -> List[T]:
def get_collection_of_primitive_values(self, primitive_type) -> Optional[List[T]]:
andrueastman marked this conversation as resolved.
Show resolved Hide resolved
"""Gets the collection of primitive values of the node
andrueastman marked this conversation as resolved.
Show resolved Hide resolved

Args:
primitive_type: The type of primitive to return.
Returns:
List[T]: The collection of primitive values
"""
pass

@abstractmethod
def get_collection_of_object_values(self, factory: ParsableFactory) -> List[U]:
def get_collection_of_object_values(self, factory: ParsableFactory) -> 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.
Expand All @@ -137,7 +138,7 @@ def get_collection_of_object_values(self, factory: ParsableFactory) -> List[U]:
pass

@abstractmethod
def get_collection_of_enum_values(self) -> List[K]:
def get_collection_of_enum_values(self, enum_class: K) -> List[Optional[K]]:
andrueastman marked this conversation as resolved.
Show resolved Hide resolved
"""Gets the collection of enum values of the node

Returns:
Expand All @@ -146,7 +147,7 @@ def get_collection_of_enum_values(self) -> List[K]:
pass

@abstractmethod
def get_enum_value(self) -> Enum:
def get_enum_value(self, enum_class: K) -> Optional[K]:
andrueastman marked this conversation as resolved.
Show resolved Hide resolved
"""Gets the enum value of the node

Returns:
Expand All @@ -155,17 +156,17 @@ def get_enum_value(self) -> Enum:
pass

@abstractmethod
def get_object_value(self, factory: ParsableFactory) -> Parsable:
def get_object_value(self, factory: ParsableFactory[U]) -> U:
baywet marked this conversation as resolved.
Show resolved Hide resolved
"""Gets the model object value of the node
Args:
factory (ParsableFactory): The factory to use to create the model object.
Returns:
Parsable: The model object value of the node
U: The model object value of the node
"""
pass

@abstractmethod
def get_bytes_value(self) -> bytes:
def get_bytes_value(self) -> Optional[bytes]:
"""Get a bytes value from the nodes

Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def write_collection_of_enum_values(
pass

@abstractmethod
def write_bytes_value(self, key: Optional[str], value: bytes) -> None:
def write_bytes_value(self, key: Optional[str], value: Optional[bytes]) -> None:
"""Writes the specified byte array as a base64 string to the stream with an optional
given key.

Expand All @@ -157,7 +157,7 @@ def write_bytes_value(self, key: Optional[str], value: bytes) -> None:

@abstractmethod
def write_object_value(
self, key: Optional[str], value: U, additional_values_to_merge: Optional[List[U]]
self, key: Optional[str], value: Optional[U], *additional_values_to_merge: Optional[List[U]]
) -> None:
"""Writes the specified model object to the stream with an optional given key.

Expand Down
Loading