8000 Use abstract class to compare with immutable type (#73) · nirum-lang/nirum-python@698ecaa · GitHub
[go: up one dir, main page]

Skip to content

Commit 698ecaa

Browse files
committed
Use abstract class to compare with immutable type (#73)
* Use abstract class to compare with immutable type * Write changelog
1 parent f66fde4 commit 698ecaa

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ Release on March 20, 2017.
1616
- Aliased :class:`~nirum.datastructures.Map` as ``map_type``, and
1717
:class:`~nirum.datastructures.List` as ``list_type`` to avoid name
1818
conflict with user-defined types.
19-
19+
- Compare type with its abstract type in :func:`nirum.validate.validate_type`.
2020

2121
__ https://github.com/spoqa/nirum/blob/f1629787f45fef17eeab8b4f030c34580e0446b8/docs/serialization.md

nirum/validate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def validate_type(data, type_):
1919
if hasattr(type_, '__origin__') and type_.__origin__ in abstract_types:
2020
param_type = get_abstract_param_types(type_)
2121
imp_types = {
22-
typing.AbstractSet: set,
23-
typing.Sequence: list,
22+
typing.AbstractSet: collections.Set,
23+
typing.Sequence: collections.Sequence,
2424
typing.Mapping: collections.Mapping,
2525
}
2626
instance_check = isinstance(data, imp_types[type_.__origin__]) and \

tests/validate_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pytest import raises
55
from six import text_type
66

7+
from nirum.datastructures import List
78
from nirum.validate import (validate_unboxed_type, validate_record_type,
89
validate_union_type, validate_type)
910

@@ -56,3 +57,9 @@ def test_validate_layered_boxed_types(fx_layered_boxed_types):
5657

5758
def test_validate_abstract_set():
5859
assert validate_type({1, 2, 3}, typing.AbstractSet[int])
60+
assert validate_type(frozenset([1, 2, 3]), typing.AbstractSet[int])
61+
62+
63+
def test_validate_list():
64+
assert validate_type([1, 2, 3], typing.Sequence[int])
65+
assert validate_type(List([1, 2, 3]), typing.Sequence[int])

0 commit comments

Comments
 (0)
0