Skip to content

Commit

Permalink
Restore Schema.TYPE_MAPPING
Browse files Browse the repository at this point in the history
close #1012
  • Loading branch information
sloria committed Oct 23, 2018
1 parent e4fb206 commit b29b2b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Features:
now take an additional ``partial`` parameter in the ``_deserialize``
method.

Bug fixes:

- Restore ``Schema.TYPE_MAPPING``, which was removed in 3.0.0b17 (:issue:`1012`).

3.0.0b18 (2018-10-15)
+++++++++++++++++++++

Expand Down
21 changes: 2 additions & 19 deletions marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from marshmallow import validate, utils, class_registry
from marshmallow.base import FieldABC, SchemaABC
from marshmallow.utils import is_collection, missing as missing_
from marshmallow.compat import basestring, binary_type, text_type
from marshmallow.compat import basestring, text_type
from marshmallow.exceptions import ValidationError, StringNotCollectionError
from marshmallow.validate import Validator

Expand Down Expand Up @@ -1471,31 +1471,14 @@ class Inferred(Field):
Users should not need to use this class directly.
"""

TYPE_MAPPING = {
text_type: String,
binary_type: String,
dt.datetime: DateTime,
float: Float,
bool: Boolean,
tuple: Raw,
list: Raw,
set: Raw,
int: Integer,
uuid.UUID: UUID,
dt.time: Time,
dt.date: Date,
dt.timedelta: TimeDelta,
decimal.Decimal: Decimal,
}

def __init__(self):
super(Inferred, self).__init__()
# We memoize the fields to avoid creating and binding new fields
# every time on serialization.
self._field_cache = {}

def _serialize(self, value, attr, obj, **kwargs):
field_cls = self.TYPE_MAPPING.get(type(value))
field_cls = self.root.TYPE_MAPPING.get(type(value))
if field_cls is None:
field = super(Inferred, self)
else:
Expand Down
24 changes: 22 additions & 2 deletions marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from __future__ import absolute_import, unicode_literals

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

from marshmallow import base, fields, utils, class_registry, marshalling
from marshmallow.compat import iteritems, iterkeys, with_metaclass
from marshmallow.compat import iteritems, iterkeys, with_metaclass, text_type, binary_type
from marshmallow.exceptions import ValidationError, StringNotCollectionError
from marshmallow.orderedset import OrderedSet
from marshmallow.decorators import (
Expand Down Expand Up @@ -267,7 +270,24 @@ class Meta:
`marshmallow.decorators.pre_load` and `marshmallow.decorators.post_dump`.
`__accessor__` and `__error_handler__` are deprecated. Implement the
`handle_error` and `get_attribute` methods instead.
"""
"""
TYPE_MAPPING = {
text_type: fields.String,
binary_type: fields.String,
dt.datetime: fields.DateTime,
float: fields.Float,
bool: fields.Boolean,
tuple: fields.Raw,
list: fields.Raw,
set: fields.Raw,
int: fields.Integer,
uuid.UUID: fields.UUID,
dt.time: fields.Time,
dt.date: fields.Date,
dt.timedelta: fields.TimeDelta,
decimal.Decimal: fields.Decimal,
}

OPTIONS_CLASS = SchemaOpts

class Meta(object):
Expand Down

0 comments on commit b29b2b6

Please sign in to comment.