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

Fix #772 Empty Only Treated as None / 2.x-line #782

Merged
merged 2 commits into from
Apr 26, 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
24 changes: 12 additions & 12 deletions marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
"""The :class:`Schema` class, including its metaclass and options (class Meta)."""
from __future__ import absolute_import, unicode_literals

from collections import defaultdict, Mapping
from collections import defaultdict, Mapping, namedtuple
import copy
import datetime as dt
import decimal
import inspect
import json
import uuid
import warnings
from collections import namedtuple
import functools

from marshmallow import base, fields, utils, class_registry, marshalling
Expand Down Expand Up @@ -241,10 +240,12 @@ class Meta:
data # {'release_date': '1968-12-06', 'title': 'Beggars Banquet'}

:param dict extra: A dict of extra attributes to bind to the serialized result.
:param tuple only: A list or tuple of fields to serialize. If `None`, all
fields will be serialized. Nested fields can be represented with dot delimiters.
:param tuple exclude: A list or tuple of fields to exclude from the
serialized result. Nested fields can be represented with dot delimiters.
:param tuple|list only: Whitelist of fields to select when instantiating the Schema.
If None, all fields are used.
Nested fields can be represented with dot delimiters.
:param tuple|list exclude: Blacklist of fields to exclude when instantiating the Schema.
If a field appears in both `only` and `exclude`, it is not used.
Nested fields can be represented with dot delimiters.
:param str prefix: Optional prefix that will be prepended to all the
serialized field names.
:param bool strict: If `True`, raise errors if invalid data are passed in
Expand All @@ -253,9 +254,8 @@ class Meta:
so that the object will be serialized to a list.
:param dict context: Optional context passed to :class:`fields.Method` and
:class:`fields.Function` fields.
:param tuple load_only: A list or tuple of fields to skip during serialization
:param tuple dump_only: A list or tuple of fields to skip during
deserialization, read-only fields
:param tuple|list load_only: Fields to skip during serialization (write-only fields)
:param tuple|list dump_only: Fields to skip during deserialization (read-only fields)
:param bool|tuple partial: Whether to ignore missing fields. If its value
is an iterable, only missing fields listed in that iterable will be
ignored.
Expand Down Expand Up @@ -328,7 +328,7 @@ class Meta:
"""
pass

def __init__(self, extra=None, only=(), exclude=(), prefix='', strict=None,
def __init__(self, extra=None, only=None, exclude=(), prefix='', strict=None,
many=False, context=None, load_only=(), dump_only=(),
partial=False):
# copy declared fields from metaclass
Expand Down Expand Up @@ -704,7 +704,7 @@ def _do_load(self, data, many=None, partial=None, postprocess=True):

def _normalize_nested_options(self):
"""Apply then flatten nested schema options"""
if self.only:
if self.only is not None:
# Apply the only option to nested fields.
self.__apply_nested_option('only', self.only)
# Remove the child field names from the only option.
Expand Down Expand Up @@ -737,7 +737,7 @@ def __apply_nested_option(self, option_name, field_names):

def _update_fields(self, obj=None, many=False):
"""Update fields based on the passed in object."""
if self.only:
if self.only is not None:
# Return only fields specified in only option
if self.opts.fields:
field_names = self.set_class(self.opts.fields) & self.set_class(self.only)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,12 @@ class Meta:
sch = MySchema(only=('baz', ))
assert sch.dump({'foo': 42}).data == {}

def test_only_empty():
class MySchema(Schema):
foo = fields.Field()

sch = MySchema(only=())
assert 'foo' not in sch.dump({'foo': 'bar'})

def test_nested_only_and_exclude():
class Inner(Schema):
Expand Down