From 6b4768b462de3220187d643058186f23d65d8dd5 Mon Sep 17 00:00:00 2001 From: rohitkrsoni Date: Sat, 1 Jun 2024 14:25:48 +0930 Subject: [PATCH 1/3] refs #285 Add support for untyped nodes. --- CHANGELOG.md | 7 +++ .../serialization/untyped_boolean.py | 24 +++++++++ .../serialization/untyped_dict.py | 24 +++++++++ .../serialization/untyped_float.py | 24 +++++++++ .../serialization/untyped_integer.py | 24 +++++++++ .../serialization/untyped_list.py | 24 +++++++++ .../serialization/untyped_node.py | 51 +++++++++++++++++++ .../serialization/untyped_none.py | 15 ++++++ .../serialization/untyped_string.py | 24 +++++++++ 9 files changed, 217 insertions(+) create mode 100644 kiota_abstractions/serialization/untyped_boolean.py create mode 100644 kiota_abstractions/serialization/untyped_dict.py create mode 100644 kiota_abstractions/serialization/untyped_float.py create mode 100644 kiota_abstractions/serialization/untyped_integer.py create mode 100644 kiota_abstractions/serialization/untyped_list.py create mode 100644 kiota_abstractions/serialization/untyped_node.py create mode 100644 kiota_abstractions/serialization/untyped_none.py create mode 100644 kiota_abstractions/serialization/untyped_string.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4179d15..8fa9aa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Added support for untyped nodes. [#285](https://github.com/microsoft/kiota-abstractions-python/issues/285) + +### Changed ## [1.3.3] - 2024-05-21 diff --git a/kiota_abstractions/serialization/untyped_boolean.py b/kiota_abstractions/serialization/untyped_boolean.py new file mode 100644 index 0000000..06ddfd8 --- /dev/null +++ b/kiota_abstractions/serialization/untyped_boolean.py @@ -0,0 +1,24 @@ +from .untyped_node import UntypedNode + +class UntypedBoolean(UntypedNode): + """ + Represents an untyped node with boolean value. + """ + + def __init__(self, value: bool) -> None: + """ + Creates a new instance of UntypedBoolean. + + Args: + value (bool): The boolean value associated with the node. + """ + self.__value = value + + def get_value(self) -> bool: + """ + Gets the value associated with untyped boolean node. + + Returns: + The value associated with untyped boolean node. + """ + return self.__value \ No newline at end of file diff --git a/kiota_abstractions/serialization/untyped_dict.py b/kiota_abstractions/serialization/untyped_dict.py new file mode 100644 index 0000000..4070663 --- /dev/null +++ b/kiota_abstractions/serialization/untyped_dict.py @@ -0,0 +1,24 @@ +from .untyped_node import UntypedNode + +class UntypedDictionary(UntypedNode): + """ + Represents an untyped node with dictionary value. + """ + + def __init__(self, value: dict) -> None: + """ + Creates a new instance of UntypedDictionary. + + Args: + value (dict): The key-value pair associated with the node. + """ + self.__value = value + + def get_value(self) -> dict: + """ + Gets the value associated with untyped dictionary node. + + Returns: + The value associated with untyped dictionary node. + """ + return self.__value \ No newline at end of file diff --git a/kiota_abstractions/serialization/untyped_float.py b/kiota_abstractions/serialization/untyped_float.py new file mode 100644 index 0000000..1ba259a --- /dev/null +++ b/kiota_abstractions/serialization/untyped_float.py @@ -0,0 +1,24 @@ +from .untyped_node import UntypedNode + +class UntypedFloat(UntypedNode): + """ + Represents an untyped node with float value. + """ + + def __init__(self, value: float) -> None: + """ + Creates a new instance of UntypedFloat. + + Args: + value (bool): The float value associated with the node. + """ + self.__value = value + + def get_value(self) -> float: + """ + Gets the value associated with untyped float node. + + Returns: + The value associated with untyped float node. + """ + return self.__value \ No newline at end of file diff --git a/kiota_abstractions/serialization/untyped_integer.py b/kiota_abstractions/serialization/untyped_integer.py new file mode 100644 index 0000000..bc67b8c --- /dev/null +++ b/kiota_abstractions/serialization/untyped_integer.py @@ -0,0 +1,24 @@ +from .untyped_node import UntypedNode + +class UntypedInteger(UntypedNode): + """ + Represents an untyped node with integer value. + """ + + def __init__(self, value: int) -> None: + """ + Creates a new instance of UntypedInteger. + + Args: + value (bool): The integer value associated with the node. + """ + self.__value = value + + def get_value(self) -> int: + """ + Gets the value associated with untyped integer node. + + Returns: + The value associated with untyped integer node. + """ + return self.__value \ No newline at end of file diff --git a/kiota_abstractions/serialization/untyped_list.py b/kiota_abstractions/serialization/untyped_list.py new file mode 100644 index 0000000..f5bc6ef --- /dev/null +++ b/kiota_abstractions/serialization/untyped_list.py @@ -0,0 +1,24 @@ +from .untyped_node import UntypedNode + +class UntypedList(UntypedNode): + """ + Represents an untyped node with list value. + """ + + def __init__(self, value: list) -> None: + """ + Creates a new instance of UntypedList. + + Args: + value (bool): The list value associated with the node. + """ + self.__value = value + + def get_value(self) -> list: + """ + Gets the value associated with untyped list node. + + Returns: + The value associated with untyped list node. + """ + return self.__value \ No newline at end of file diff --git a/kiota_abstractions/serialization/untyped_node.py b/kiota_abstractions/serialization/untyped_node.py new file mode 100644 index 0000000..6c5ba6b --- /dev/null +++ b/kiota_abstractions/serialization/untyped_node.py @@ -0,0 +1,51 @@ +from collections import defaultdict +from parsable import Parsable +from typing import TYPE_CHECKING, Callable, Dict + +if TYPE_CHECKING: + from .parse_node import ParseNode + from .serialization_writer import SerializationWriter + +class UntypedNode(Parsable): + """ + Base class for untyped node. + """ + + __field_deserializers: Dict[str, Callable[['ParseNode'], None]] = {} + + def get_field_deserializers(self) -> Dict[str, Callable[['ParseNode'], None]]: + """ + The deserialization information for the current model + """ + return UntypedNode.__field_deserializers + + def serialize(self, writer: 'SerializationWriter'): + """ + Serializes information about the current object. + + Args: + writer (SerializationWriter): Serialization writer to use to serialize this model. + """ + if not writer: + raise ValueError("writer cannot be None") + + @staticmethod + def create_from_discriminator_value(parse_node: 'Parsable'): + """ + Creates a new instance of the appropriate class based on discriminator value + + Args: + parse_node (ParseNode): The parse node to use to read the discriminator value and create the object + """ + if not parse_node: + raise ValueError("parse_node cannot be None") + return UntypedNode() + + def get_value(self): + """ + Gets the value of the current node. + + Returns: + The value assigned to untyped node. + """ + raise NotImplementedError("This method is not implemented") \ No newline at end of file diff --git a/kiota_abstractions/serialization/untyped_none.py b/kiota_abstractions/serialization/untyped_none.py new file mode 100644 index 0000000..c5bd8c5 --- /dev/null +++ b/kiota_abstractions/serialization/untyped_none.py @@ -0,0 +1,15 @@ +from .untyped_node import UntypedNode + +class UntypedNone(UntypedNode): + """ + Represents an untyped node with none value. + """ + + def get_value(self) -> None: + """ + Gets the value associated with untyped none node. + + Returns: + The value associated with untyped none node. + """ + return None \ No newline at end of file diff --git a/kiota_abstractions/serialization/untyped_string.py b/kiota_abstractions/serialization/untyped_string.py new file mode 100644 index 0000000..a2b5b98 --- /dev/null +++ b/kiota_abstractions/serialization/untyped_string.py @@ -0,0 +1,24 @@ +from .untyped_node import UntypedNode + +class UntypedString(UntypedNode): + """ + Represents an untyped node with string value. + """ + + def __init__(self, value: str) -> None: + """ + Creates a new instance of UntypedStr. + + Args: + value (bool): The string value associated with the node. + """ + self.__value = value + + def get_value(self) -> str: + """ + Gets the value associated with untyped string node. + + Returns: + The value associated with untyped string node. + """ + return self.__value From 3cf2a67233d21c374c4b06847a0676fcb54b9b46 Mon Sep 17 00:00:00 2001 From: rohitkrsoni Date: Tue, 4 Jun 2024 02:46:04 +0930 Subject: [PATCH 2/3] refs #285 make list of type UntypedNode --- kiota_abstractions/serialization/untyped_list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kiota_abstractions/serialization/untyped_list.py b/kiota_abstractions/serialization/untyped_list.py index f5bc6ef..c356c66 100644 --- a/kiota_abstractions/serialization/untyped_list.py +++ b/kiota_abstractions/serialization/untyped_list.py @@ -5,7 +5,7 @@ class UntypedList(UntypedNode): Represents an untyped node with list value. """ - def __init__(self, value: list) -> None: + def __init__(self, value: list[UntypedNode]) -> None: """ Creates a new instance of UntypedList. @@ -14,7 +14,7 @@ def __init__(self, value: list) -> None: """ self.__value = value - def get_value(self) -> list: + def get_value(self) -> list[UntypedNode]: """ Gets the value associated with untyped list node. From 88572f7b3392191148f46ff519ec40c9e745df5a Mon Sep 17 00:00:00 2001 From: rohitkrsoni Date: Fri, 7 Jun 2024 01:04:51 +0930 Subject: [PATCH 3/3] refs #285 Add type constraint for constructor arguments --- kiota_abstractions/serialization/untyped_boolean.py | 4 +++- kiota_abstractions/serialization/untyped_dict.py | 10 ++++++++-- kiota_abstractions/serialization/untyped_float.py | 2 ++ kiota_abstractions/serialization/untyped_integer.py | 2 ++ kiota_abstractions/serialization/untyped_list.py | 4 ++++ kiota_abstractions/serialization/untyped_string.py | 2 ++ 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/kiota_abstractions/serialization/untyped_boolean.py b/kiota_abstractions/serialization/untyped_boolean.py index 06ddfd8..1179d3c 100644 --- a/kiota_abstractions/serialization/untyped_boolean.py +++ b/kiota_abstractions/serialization/untyped_boolean.py @@ -12,8 +12,10 @@ def __init__(self, value: bool) -> None: Args: value (bool): The boolean value associated with the node. """ + if not isinstance(value, bool): + raise TypeError("Value of UntypedBoolean must be of type bool") self.__value = value - + def get_value(self) -> bool: """ Gets the value associated with untyped boolean node. diff --git a/kiota_abstractions/serialization/untyped_dict.py b/kiota_abstractions/serialization/untyped_dict.py index 4070663..567e352 100644 --- a/kiota_abstractions/serialization/untyped_dict.py +++ b/kiota_abstractions/serialization/untyped_dict.py @@ -5,16 +5,22 @@ class UntypedDictionary(UntypedNode): Represents an untyped node with dictionary value. """ - def __init__(self, value: dict) -> None: + def __init__(self, value: dict[str:UntypedNode]) -> None: """ Creates a new instance of UntypedDictionary. Args: value (dict): The key-value pair associated with the node. """ + if not isinstance(value, dict): + if not all(isinstance(key, str) for key in value.keys()): + raise TypeError("Keys of UntypedDictionary must be of type str") + if not all(isinstance(value, UntypedNode) for value in value.values()): + raise TypeError("Values of UntypedDictionary must be of type UntypedNode") + raise TypeError("Value of UntypedDictionary must be of type dict") self.__value = value - def get_value(self) -> dict: + def get_value(self) -> dict[str:UntypedNode]: """ Gets the value associated with untyped dictionary node. diff --git a/kiota_abstractions/serialization/untyped_float.py b/kiota_abstractions/serialization/untyped_float.py index 1ba259a..2329df6 100644 --- a/kiota_abstractions/serialization/untyped_float.py +++ b/kiota_abstractions/serialization/untyped_float.py @@ -12,6 +12,8 @@ def __init__(self, value: float) -> None: Args: value (bool): The float value associated with the node. """ + if not isinstance(value, float): + raise TypeError("Value of UntypedFloat must be of type float") self.__value = value def get_value(self) -> float: diff --git a/kiota_abstractions/serialization/untyped_integer.py b/kiota_abstractions/serialization/untyped_integer.py index bc67b8c..11a72f6 100644 --- a/kiota_abstractions/serialization/untyped_integer.py +++ b/kiota_abstractions/serialization/untyped_integer.py @@ -12,6 +12,8 @@ def __init__(self, value: int) -> None: Args: value (bool): The integer value associated with the node. """ + if not isinstance(value, int): + raise TypeError("Value of UntypedInteger must be of type int") self.__value = value def get_value(self) -> int: diff --git a/kiota_abstractions/serialization/untyped_list.py b/kiota_abstractions/serialization/untyped_list.py index c356c66..c55f350 100644 --- a/kiota_abstractions/serialization/untyped_list.py +++ b/kiota_abstractions/serialization/untyped_list.py @@ -12,6 +12,10 @@ def __init__(self, value: list[UntypedNode]) -> None: Args: value (bool): The list value associated with the node. """ + if not isinstance(value, list): + if not all(isinstance(value, UntypedNode) for value in value): + raise TypeError("Values of UntypedList must be of type UntypedNode") + raise TypeError("Value of UntypedList must be of type list") self.__value = value def get_value(self) -> list[UntypedNode]: diff --git a/kiota_abstractions/serialization/untyped_string.py b/kiota_abstractions/serialization/untyped_string.py index a2b5b98..011cb6e 100644 --- a/kiota_abstractions/serialization/untyped_string.py +++ b/kiota_abstractions/serialization/untyped_string.py @@ -12,6 +12,8 @@ def __init__(self, value: str) -> None: Args: value (bool): The string value associated with the node. """ + if not isinstance(value, str): + raise TypeError("Value of UntypedStr must be of type str") self.__value = value def get_value(self) -> str: