Skip to content

Commit

Permalink
Merge 8886eca into e4707a8
Browse files Browse the repository at this point in the history
  • Loading branch information
avanov committed Aug 22, 2019
2 parents e4707a8 + 8886eca commit 4dcea0c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,6 +2,11 @@
CHANGELOG
=========

0.18.0
===============

* SumType variants are attribute-strict #48

0.16.0 - 0.17.0
===============

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Expand Up @@ -51,9 +51,9 @@
# built documents.
#
# The short X.Y version.
version = '0.17'
version = '0.18'
# The full version, including alpha/beta/rc tags.
release = '0.17.0'
release = '0.18.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -38,7 +38,7 @@ def requirements(at_path: Path):
# ----------------------------

setup(name='typeit',
version='0.17.0',
version='0.18.0',
description='typeit brings typed data into your project',
long_description=README,
classifiers=[
Expand Down
26 changes: 24 additions & 2 deletions tests/test_sums.py
@@ -1,4 +1,4 @@
from typing import Dict
from typing import Dict, NamedTuple

import pytest
import pickle
Expand Down Expand Up @@ -148,4 +148,26 @@ class VariantB:
'b': True,
}
x = mk_x(data)
assert serialize_x(x) == data
assert serialize_x(x) == data


def test_sumtype_attr_strictness():
class Versioning(SumType):
class V1:
a: int

class V2:
a: int
b: int

class X(NamedTuple):
payload: Versioning

mk_x, serialize_x = (
typeit.type_constructor & typeit.flags.SUM_TYPE_DICT('_version_') ^ X
)

with pytest.raises(typeit.Error):
x = mk_x({'payload': {'_version_': 'v1', 'a': 1, 'b': 1}})

x = mk_x({'payload': {'_version_': 'v2', 'a': 1, 'b': 1}})
1 change: 1 addition & 0 deletions typeit/parser.py
Expand Up @@ -175,6 +175,7 @@ def _maybe_node_for_sum_type(
overrides,
memo
)
node.typ.unknown = 'raise'
variant_nodes.append((variant, node))
sum_node = schema.nodes.SchemaNode(
schema.types.Sum(
Expand Down
9 changes: 6 additions & 3 deletions typeit/utils.py
Expand Up @@ -23,9 +23,12 @@ def is_named_tuple(typ: Type[Any]) -> bool:
def clone_schema_node(node):
""" Clonning the node and reassigning the same children,
because clonning is recursive, but we are only interested
in new version of the outermost schema node, the children nodes
in a new version of the outermost schema node, the children nodes
should be shared to avoid unnecessary duplicates.
"""
new_node = node.clone()
new_node.children = node.children
return new_node
# a list comprehension to place the same nodes into
# a new wrapping list object, so that extending
# the cloned node with new children doesn't affect the original node
new_node.children = [x for x in node.children]
return new_node

0 comments on commit 4dcea0c

Please sign in to comment.