-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Full support of
typing.Self
as a serializable type-hint.
- Required rewriting some of the container serializers (Array, Union) to properly handle the fact that recursion (and thus re-use mid-pack/unpack) of a serializer can happen now.
- Loading branch information
Showing
11 changed files
with
286 additions
and
59 deletions.
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
from .structured import * | ||
from .tuples import * | ||
from .unions import * | ||
from .self import * |
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
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,42 @@ | ||
""" | ||
Serializer for special handling of the typing.Self typehint. | ||
""" | ||
|
||
__all__ = [ | ||
'SelfSerializer', | ||
] | ||
|
||
|
||
from ..type_checking import ( | ||
Any, | ||
ClassVar, | ||
TYPE_CHECKING, | ||
annotated, | ||
Self, | ||
) | ||
from .api import Serializer | ||
from .structured import StructuredSerializer | ||
|
||
|
||
if TYPE_CHECKING: | ||
from ..structured import Structured, _Proxy | ||
else: | ||
Structured = 'Structured' | ||
_Proxy = '_Proxy' | ||
|
||
class SelfSerializer(Serializer[Structured]): | ||
num_values: ClassVar[int] = 1 | ||
|
||
def prepack(self, partial_object: Structured) -> Serializer: | ||
return StructuredSerializer(type(partial_object)) | ||
|
||
def preunpack(self, partial_object: _Proxy) -> Serializer: | ||
return StructuredSerializer(partial_object.cls) | ||
|
||
@classmethod | ||
def _transform(cls, unwrapped: Any, actual: Any) -> Any: | ||
if unwrapped is Self: | ||
return cls() | ||
|
||
|
||
annotated.register_transform(SelfSerializer._transform) |
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
Oops, something went wrong.