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

Allow extend to return a subclass of Schema #378

Merged
merged 1 commit into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
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
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