-
Notifications
You must be signed in to change notification settings - Fork 11
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
Create Types for untyped nodes #286
Open
rohitkrsoni
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
rohitkrsoni:feature/285/rohitkrsoni
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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. | ||
""" | ||
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. | ||
|
||
Returns: | ||
The value associated with untyped boolean node. | ||
""" | ||
return self.__value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from .untyped_node import UntypedNode | ||
|
||
class UntypedDictionary(UntypedNode): | ||
""" | ||
Represents an untyped node with dictionary value. | ||
""" | ||
|
||
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[str:UntypedNode]: | ||
""" | ||
Gets the value associated with untyped dictionary node. | ||
|
||
Returns: | ||
The value associated with untyped dictionary node. | ||
""" | ||
return self.__value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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. | ||
""" | ||
if not isinstance(value, float): | ||
raise TypeError("Value of UntypedFloat must be of type float") | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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. | ||
""" | ||
if not isinstance(value, int): | ||
raise TypeError("Value of UntypedInteger must be of type int") | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from .untyped_node import UntypedNode | ||
|
||
class UntypedList(UntypedNode): | ||
andrueastman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Represents an untyped node with list value. | ||
""" | ||
|
||
def __init__(self, value: list[UntypedNode]) -> None: | ||
""" | ||
Creates a new instance of UntypedList. | ||
|
||
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]: | ||
""" | ||
Gets the value associated with untyped list node. | ||
|
||
Returns: | ||
The value associated with untyped list node. | ||
""" | ||
return self.__value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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. | ||
""" | ||
if not isinstance(value, str): | ||
raise TypeError("Value of UntypedStr must be of type str") | ||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason to not call it
UntypedObject
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially, I though of adding it as
UntypedObject
but in python we call it dictionary which stores key-value pairs similar toHashMap
inJava
. https://docs.python.org/3/tutorial/datastructures.html#dictionariesI can change it back to
Object
if you like.Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have strong opinion on this one. It's better to align with what the language thinks is best.
Maybe @shemogumbe can weigh in on the naming here..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My view is -
arrays
andobjects
instead oflist
anddict
might create confusion as the codebase of the project using python client will have lists and dictionaries.open for suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rohitkrsoni Lets keep this as is for now since the it makes sense for the language to have the naming as so and then start working on the serialization bits. In the event of any feedback before we merge, we can do a renaming.
Are you able to work on the serialization bits with this branch as a source?