-
Notifications
You must be signed in to change notification settings - Fork 10
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
first attempt at an adjacently tagged union for consideration #94
Draft
altendky
wants to merge
60
commits into
python-desert:main
Choose a base branch
from
altendky:36-altendky-first_attempt
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.
Draft
Changes from 7 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
921e8e4
first attempt at an adjacently tagged union for consideration
altendky 4f98346
black
altendky 14c7273
raise on extra keys when deserializing adjacently tagged
altendky cd0f7b6
remove trailing comma on single line, thanks black
altendky 08c6e0c
correct TypeTagField.field type hint
altendky ddb5df4
add str and decimal.Decimal examples
altendky 2492519
break by adding a couple of different list examples
altendky 1ce7e67
remove explicit registry collision check
altendky cac94de
correct tests, better demonstrate failures, prep for additional regis…
altendky f2cffbe
black...
altendky 84cd5ed
maybe interesting
altendky e76a271
use typeguard instead, for now at least
altendky 18e78d3
black
altendky b233cf4
cleanup
altendky 7a44b41
no protocol for now
altendky 7731c00
remove duplicate code
altendky b30170e
pop a little
altendky 21c9e1b
extract adjacent functions and generalize to TaggedUnion
altendky 6a4bae8
group adjacently tagged functions
altendky 8b97cf6
add externally tagged support
altendky f63f290
add internally tagged support
altendky 1b41217
make tagged type and value keys configurable
altendky 74494eb
complain if other than exactly one hint matches
altendky d5d7183
isinstance() for str vs. typing.Sequence[str] handling
altendky 39104a7
Merge branch 'master' into 36-altendky-first_attempt
altendky f29ef21
black
altendky 8633a8e
Merge branch 'main' into 36-altendky-first_attempt
altendky d76ec24
additional heuristics for List vs. Sequence, 3.7+ only
altendky 4b4892f
Merge branch 'main' into 36-altendky-first_attempt
altendky 39caf6f
make test examples frozen
altendky e615380
mypy
altendky 3090a14
Merge branch 'main' into 36-altendky-first_attempt
altendky 0e9c3a9
actually use typing-extensions
altendky 419fd30
black
altendky 7034ea2
check
altendky fc889f6
coverage
altendky 408b21f
create specific exceptions
altendky cc9279f
check
altendky a6dfccd
specify field registry protocol and check it
altendky c7a385e
add test that looks like real user code
altendky d610e5e
just use Any for now
altendky 47197f6
drop some args/kwargs to be more explicit
altendky c2cf8a6
docstrings and some more
altendky 6722760
use typing_inspect.get_origin() instead of .__origin__
altendky 47861f9
Merge branch 'main' into 36-altendky-first_attempt
altendky 76bc4da
type hinting catchup
altendky 918f2ab
Merge branch 'main' into 36-altendky-first_attempt
altendky aa85230
tagged union documentation
altendky 64a20d9
add missing snippets
altendky c0f1ef6
Merge branch 'main' into 36-altendky-first_attempt
altendky 82f92be
black
altendky 2b8ca42
doc warning tidy
altendky c256d80
docutils < 0.17 for circular include error
altendky dc5d731
avoid trailing blank lines in example in docs
altendky bd64aa0
xfail for working-.__origin__-requiring tests on < 3.7
altendky f8a377f
parametrize against *_tagged_union[_from_registry] for coverage
altendky 694000f
actually test tagged union examples
altendky d5acb63
add new example resources
altendky f6d2314
isort
altendky ea773b7
CatsAndDogs corrected to CatOrDog
altendky 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import typing | ||
|
||
import attr | ||
import marshmallow.fields | ||
|
||
|
||
T = typing.TypeVar("T") | ||
|
||
|
||
@attr.s(frozen=True, auto_attribs=True) | ||
class TypeTagField: | ||
cls: type | ||
tag: str | ||
field: typing.Type[marshmallow.fields.Field] | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class TypeDictRegistry: | ||
the_dict: typing.Dict[typing.Union[type, str], marshmallow.fields.Field] = attr.ib( | ||
factory=dict | ||
) | ||
|
||
def register(self, cls, tag, field): | ||
if any(key in self.the_dict for key in [cls, tag]): | ||
raise Exception() | ||
|
||
type_tag_field = TypeTagField(cls=cls, tag=tag, field=field) | ||
|
||
self.the_dict[cls] = type_tag_field | ||
self.the_dict[tag] = type_tag_field | ||
|
||
# TODO: this type hinting... doesn't help much as it could return | ||
# another cls | ||
def __call__(self, tag: str, field: marshmallow.fields) -> typing.Callable[[T], T]: | ||
return lambda cls: self.register(cls=cls, tag=tag, field=field) | ||
|
||
def from_object(self, value): | ||
return self.the_dict[type(value)] | ||
|
||
def from_tag(self, tag): | ||
return self.the_dict[tag] | ||
|
||
|
||
class AdjacentlyTaggedUnion(marshmallow.fields.Field): | ||
def __init__( | ||
self, | ||
*, | ||
from_object: typing.Callable[[typing.Any], TypeTagField], | ||
from_tag: typing.Callable[[str], TypeTagField], | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
|
||
self.from_object = from_object | ||
self.from_tag = from_tag | ||
|
||
def _deserialize( | ||
self, | ||
value: typing.Any, | ||
attr: typing.Optional[str], | ||
data: typing.Optional[typing.Mapping[str, typing.Any]], | ||
**kwargs, | ||
) -> typing.Any: | ||
tag = value["type"] | ||
serialized_value = value["value"] | ||
|
||
if len(value) > 2: | ||
raise Exception() | ||
|
||
type_tag_field = self.from_tag(tag) | ||
field = type_tag_field.field() | ||
|
||
return field.deserialize(serialized_value) | ||
|
||
def _serialize( | ||
self, value: typing.Any, attr: str, obj: typing.Any, **kwargs, | ||
) -> typing.Any: | ||
type_tag_field = self.from_object(value) | ||
field = type_tag_field.field() | ||
tag = type_tag_field.tag | ||
serialized_value = field.serialize(attr, obj) | ||
|
||
return {"type": tag, "value": serialized_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,98 @@ | ||
import decimal | ||
import typing | ||
|
||
import attr | ||
import marshmallow | ||
import pytest | ||
|
||
import desert._fields | ||
|
||
|
||
# TODO: test that field constructor doesn't tromple Field parameters | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class ExampleData: | ||
object: typing.Any | ||
tag: str | ||
field: typing.Callable[[], marshmallow.fields.Field] | ||
|
||
|
||
example_data_list = [ | ||
ExampleData(object=3.7, tag="float_tag", field=marshmallow.fields.Float), | ||
ExampleData(object="29", tag="str_tag", field=marshmallow.fields.String), | ||
ExampleData( | ||
object=decimal.Decimal("4.2"), | ||
tag="decimal_tag", | ||
field=marshmallow.fields.Decimal, | ||
), | ||
ExampleData( | ||
object=[1, 2, 3], | ||
tag="integer_list_tag", | ||
field=lambda: marshmallow.fields.List(marshmallow.fields.Integer()), | ||
), | ||
ExampleData( | ||
object=['abc', '2', 'mno'], | ||
tag="string_list_tag", | ||
field=lambda: marshmallow.fields.List(marshmallow.fields.String()), | ||
), | ||
] | ||
|
||
|
||
@pytest.fixture( | ||
name="example_data", | ||
params=example_data_list, | ||
ids=[str(example) for example in example_data_list], | ||
) | ||
def _example_data(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture(name="registry", scope="session") | ||
altendky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def _registry(): | ||
registry = desert._fields.TypeDictRegistry() | ||
|
||
for example in example_data_list: | ||
registry.register( | ||
cls=type(example.object), tag=example.tag, field=example.field, | ||
) | ||
|
||
return registry | ||
|
||
|
||
@pytest.fixture(name="adjacently_tagged_field", scope="session") | ||
def _adjacently_tagged_field(registry): | ||
return desert._fields.AdjacentlyTaggedUnion( | ||
from_object=registry.from_object, from_tag=registry.from_tag, | ||
) | ||
|
||
|
||
def test_adjacently_tagged_deserialize(example_data, adjacently_tagged_field): | ||
serialized_value = {"type": example_data.tag, "value": example_data.object} | ||
|
||
deserialized_value = adjacently_tagged_field.deserialize(serialized_value) | ||
|
||
assert (type(deserialized_value) == type(example_data.object)) and ( | ||
deserialized_value == example_data.object | ||
) | ||
|
||
|
||
def test_adjacently_tagged_deserialize_extra_key_raises( | ||
example_data, adjacently_tagged_field, | ||
): | ||
serialized_value = { | ||
"type": example_data.tag, | ||
"value": example_data.object, | ||
"extra": 29, | ||
} | ||
|
||
with pytest.raises(expected_exception=Exception): | ||
adjacently_tagged_field.deserialize(serialized_value) | ||
|
||
|
||
def test_adjacently_tagged_serialize(example_data, adjacently_tagged_field): | ||
obj = {"key": example_data.object} | ||
|
||
serialized_value = adjacently_tagged_field.serialize("key", obj) | ||
|
||
assert serialized_value == {"type": example_data.tag, "value": example_data.object} |
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.
I'm not a big
the_foo
user, perhaps.mapping
or something.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.
Heh, yeah, that was a garbage name so as to not think about it and worry about other things. Could also just be
.dict