Skip to content

Commit

Permalink
Merge pull request #378 from svisser/fix/issue_370
Browse files Browse the repository at this point in the history
Allow extend to return a subclass of Schema
  • Loading branch information
alecthomas committed Dec 31, 2018
2 parents c2e1cfd + e11b20a commit 9644956
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,10 @@ def key_literal(key):
result[key] = value

# recompile and send old object
result_cls = type(self)
result_required = (required if required is not None else self.required)
result_extra = (extra if extra is not None else self.extra)
return Schema(result, required=result_required, extra=result_extra)
return result_cls(result, required=result_required, extra=result_extra)


def _compile_scalar(schema):
Expand Down
15 changes: 15 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def test_schema_extend():
assert extended.schema == {'a': int, 'b': str}
assert extended.required == base.required
assert extended.extra == base.extra
assert isinstance(extended, Schema)


def test_schema_extend_overrides():
Expand Down Expand Up @@ -411,6 +412,20 @@ def test_subschema_extension():
assert_equal(extended.schema, {'a': {'b': str, 'c': float, 'e': int}, 'd': str})


def test_schema_extend_handles_schema_subclass():
"""Verify that Schema.extend handles a subclass of Schema"""
class S(Schema):
pass

base = S({Required('a'): int})
extension = {Optional('b'): str}
extended = base.extend(extension)

expected_schema = {Required('a'): int, Optional('b'): str}
assert extended.schema == expected_schema
assert isinstance(extended, S)


def test_equality():
assert_equal(Schema('foo'), Schema('foo'))

Expand Down

0 comments on commit 9644956

Please sign in to comment.