Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Imp: Drop support for marshmallow 2 #68

Merged
merged 3 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 3 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@ branches:

python:
- "3.6"
- "2.7"

env:
- MARSHMALLOW_VERSION=2.19.2
- MARSHMALLOW_VERSION=3.0.0

matrix:
exclude:
- python: "2.7"
env: MARSHMALLOW_VERSION=3.0.0

install:
- pip install marshmallow==$MARSHMALLOW_VERSION
- pip install -r requirements.txt -r test-requirements.txt

script: coverage run --source=marshmallow_objects setup.py test
after_success: codecov

stages:
- pep8
- test
Expand All @@ -36,6 +22,9 @@ jobs:
script: flake8 --show-source
install: pip install flake8
after_success: skip
SVilgelm marked this conversation as resolved.
Show resolved Hide resolved
- stage: test
script: coverage run --source=marshmallow_objects setup.py test
after_success: codecov
- stage: deploy
script: skip
install: skip
Expand Down
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CHANGES
=======

SVilgelm marked this conversation as resolved.
Show resolved Hide resolved
* Drop support for marshmallow 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove changes in this file. ChangeLoge will be generated automatically

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^^ please revert. The bump version script automatically generates the change log file


1.0.23
------

Expand Down
60 changes: 26 additions & 34 deletions marshmallow_objects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
import contextlib
import json
import pprint
import sys
import threading
try:
import configparser
import io
except ImportError:
import ConfigParser as configparser
import StringIO as io
import configparser
import io

import marshmallow
from marshmallow import fields
Expand All @@ -18,10 +13,6 @@
except ImportError:
pass

# Checking Marshmallow version
MM2 = marshmallow.__version__.startswith('2')
PY2 = int(sys.version_info[0]) == 2


@marshmallow.post_load
def __make_object__(self, data, **kwargs):
Expand Down Expand Up @@ -51,7 +42,7 @@ def __new__(mcs, name, parents, dct):
schema_fields[method_name] = dct[method_name]

elif hasattr(
value, '__marshmallow_tags__' if MM2 else
value,
'__marshmallow_hook__') or key in ('Meta', 'on_bind_field',
'handle_error'):
schema_fields[key] = value
Expand All @@ -72,6 +63,7 @@ def __new__(mcs, name, parents, dct):
def __call__(cls, *args, **kwargs):
if kwargs.pop('__post_load__', False):
kwargs.pop("many", None)
kwargs.pop("unknown", None)
schema = kwargs.pop('__schema__')
obj = cls.__new__(cls, *args, **kwargs)
obj.__dump_lock__ = threading.RLock()
Expand All @@ -87,7 +79,14 @@ def __call__(cls, *args, **kwargs):
context = kwargs.pop('context', None)
partial = kwargs.pop('partial', None)
many = kwargs.pop("many", None)
obj = cls.load(kwargs, many=many, context=context, partial=partial)
unknown = kwargs.pop('unknown', None)
obj = cls.load(
kwargs,
many=many,
context=context,
partial=partial,
unknown=unknown
)
return obj


Expand Down Expand Up @@ -126,8 +125,6 @@ class Model(with_metaclass(ModelMeta)):

@classmethod
def __get_schema_class__(cls, **kwargs):
if MM2:
kwargs.setdefault('strict', True)
return cls.__schema_class__(**kwargs)

def __setattr_default__(self, key, value):
Expand Down Expand Up @@ -179,18 +176,14 @@ def context(self, value):
self.__schema__.context = value

@classmethod
def load(cls, data, context=None, many=None, partial=None):
def load(cls, data, context=None, many=None, partial=None, unknown=None):
schema = cls.__get_schema_class__(context=context, partial=partial)
loaded = schema.load(data, many=many)
if MM2:
return loaded[0]
loaded = schema.load(data, many=many, unknown=unknown)
return loaded

def dump(self):
with self.__dump_mode_on__():
dump = self.__schema__.dump(self)
if MM2:
return dump.data
return dump

@classmethod
Expand All @@ -199,16 +192,16 @@ def load_json(cls,
context=None,
many=None,
partial=None,
unknown=None,
*args,
**kwargs):
schema = cls.__get_schema_class__(context=context)
loaded = schema.loads(data,
many=many,
partial=partial,
unknown=unknown,
*args,
**kwargs)
if MM2:
return loaded[0]
return loaded

def dump_json(self):
Expand All @@ -220,22 +213,25 @@ def load_yaml(cls,
context=None,
many=None,
partial=None,
unknown=None,
*args,
**kwargs):
loaded = yaml.load(data, *args, **kwargs)
return cls.load(loaded, context=context, many=many, partial=partial)
loaded = yaml.load(data, Loader=yaml.FullLoader)
return cls.load(
loaded,
context=context,
many=many,
partial=partial,
unknown=unknown
)

def dump_yaml(self, default_flow_style=False):
return yaml.dump(self.dump(), default_flow_style=default_flow_style)

@classmethod
def load_ini(cls, data, context=None, partial=None, **kwargs):
parser = configparser.ConfigParser(**kwargs)
if PY2:
fp = io.StringIO(data)
parser.readfp(fp)
else:
parser.read_string(data)
parser.read_string(data)
ddata = {s: dict(parser.items(s)) for s in parser.sections()}
ddata.update(parser.defaults())
return cls.load(ddata, context=context, partial=partial)
Expand All @@ -258,8 +254,6 @@ def dump_ini(self, **kwargs):
@classmethod
def validate(cls, data, context=None, many=None, partial=None):
kwargs = {'context': context}
if MM2:
kwargs['strict'] = False
schema = cls.__get_schema_class__(**kwargs)
return schema.validate(data, many=many, partial=partial)

Expand Down Expand Up @@ -297,8 +291,6 @@ def dump_many(data, context=None):
else:
schema = obj.__get_schema_class__(context=context)
obj_data = schema.dump(obj)
if MM2:
obj_data = obj_data[0]
ret.append(obj_data)
elif (isinstance(obj, collections.Sequence)
and not isinstance(obj, str)):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
marshmallow
marshmallow>=3.0.0
18 changes: 15 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Meta:
def on_bind_field(self, field_name, field_obj):
pass

def handle_error(self, error, data):
def handle_error(self, error, data, many, partial):
pass


Expand Down Expand Up @@ -289,7 +289,7 @@ def test_load_yaml_partial(self):
@unittest.skipIf(skip_yaml, 'PyYaml is not installed')
def test_dump_yaml(self):
a = A(test_field='foo')
ydata = yaml.load(a.dump_yaml())
ydata = yaml.load(a.dump_yaml(), Loader=yaml.FullLoader)
self.assertEqual(self.data, ydata)

def test_dump_ordered(self):
Expand All @@ -298,6 +298,18 @@ def test_dump_ordered(self):
self.assertIsInstance(a, collections.OrderedDict)
self.assertIsInstance(b, dict)

def test_load_unknwon(self):
data = dict(
test_field='foo',
unknown_b="B",
a=dict(test_field='bar', unknown_b="B")
)
with self.assertRaises(marshmallow.ValidationError):
B.load(data)
b = B.load(data, unknown=marshmallow.EXCLUDE)
self.assertEqual(b.test_field, 'foo')
self.assertEqual(b.a.test_field, 'bar')


class TestContext(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -439,7 +451,7 @@ def test_dump_json(self):
def test_dump_yaml(self):
bb = B.load(self.data, many=True)
ydata = marshmallow.dump_many_yaml(bb)
ddata = yaml.load(ydata)
ddata = yaml.load(ydata, Loader=yaml.FullLoader)
self.assertEqual(self.data, ddata)


Expand Down