Skip to content
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

Extended support from typing.List to typing.Iterable, and from typing.Dict to typing.Mapping #64

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions enforce/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,43 +150,47 @@ def _parse_bytes(node, hint, validator, parsers):


def _parse_generic(node, hint, validator, parsers):
if issubclass(hint, typing.List):
yield _parse_list(node, hint, validator, parsers)
elif issubclass(hint, typing.Dict):
yield _parse_dict(node, hint, validator, parsers)
elif issubclass(hint, typing.Set):
yield _parse_set(node, hint, validator, parsers)
# Fixes https://github.com/RussBaz/enforce/issues/{47, 51, 52}
# Mapping need to be checked BEFORE Iterable (since a Mapping is an Iterable)
if issubclass(hint, typing.Mapping):
yield _parse_mapping(node, hint, validator, parsers)
elif issubclass(hint, typing.Iterable):
yield _parse_iterable(node, hint, validator, parsers)
# Not needed anymore: a Set is just an Iterable
# elif issubclass(hint, typing.Set):
# yield _parse_set(node, hint, validator, parsers)
else:
new_node = yield nodes.GenericNode(hint)
validator.all_nodes.append(new_node)
yield _yield_parsing_result(node, new_node)


def _parse_list(node, hint, validator, parsers):
def _parse_iterable(node, hint, validator, parsers):
new_node = yield nodes.SimpleNode(hint.__extra__)
validator.all_nodes.append(new_node)

# add its type as child
# We need to index first element only as Lists always have 1 argument
# We need to index first element only as Iterable always have 1 argument
if hint.__args__:
yield get_parser(new_node, hint.__args__[0], validator, parsers)

yield _yield_parsing_result(node, new_node)


def _parse_set(node, hint, validator, parsers):
new_node = yield nodes.SimpleNode(hint.__extra__)
validator.all_nodes.append(new_node)

# add its type as child
# We need to index first element only as Sets always have 1 argument
if hint.__args__:
yield get_parser(new_node, hint.__args__[0], validator, parsers)

yield _yield_parsing_result(node, new_node)
# -- Not useful anymore: a Set is an Iterable
# def _parse_set(node, hint, validator, parsers):
# new_node = yield nodes.SimpleNode(hint.__extra__)
# validator.all_nodes.append(new_node)
#
# # add its type as child
# # We need to index first element only as Sets always have 1 argument
# if hint.__args__:
# yield get_parser(new_node, hint.__args__[0], validator, parsers)
#
# yield _yield_parsing_result(node, new_node)


def _parse_dict(node, hint, validator, parsers):
def _parse_mapping(node, hint, validator, parsers):
hint_args = hint.__args__

if hint_args:
Expand Down