Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
Extended string types now subclass Text not Str
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Feb 15, 2020
1 parent a06fd2a commit 5862375
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
85 changes: 42 additions & 43 deletions src/serde/fields.py
Expand Up @@ -1049,7 +1049,7 @@ def normalize(self, value):
return value


class Regex(Str):
class Regex(Text):
"""
A regex field.
Expand Down Expand Up @@ -1118,48 +1118,9 @@ def normalize(self, value):
return value


FIELD_CLASS_MAP = {
# Built-in types
bool: Bool,
bytes: Bytes,
complex: Complex,
dict: Dict,
float: Float,
int: Int,
list: List,
set: Set,
str: Str,
tuple: Tuple,
# Collections
collections.deque: Deque,
collections.OrderedDict: OrderedDict,
# Datetimes
datetime.datetime: DateTime,
datetime.date: Date,
datetime.time: Time,
# Others
uuid.UUID: Uuid,
}

try:
FIELD_CLASS_MAP[basestring] = BaseString
except NameError:
pass

try:
FIELD_CLASS_MAP[long] = Long
except NameError:
pass

try:
FIELD_CLASS_MAP[unicode] = Unicode
except NameError:
pass


def create_from(foreign, name=None, human=None):
"""
Create a new `Str` class from a `validators` function.
Create a new `Text` class from a `validators` function.
"""
suffix = foreign.split('.', 1)[1]

Expand All @@ -1169,7 +1130,7 @@ def create_from(foreign, name=None, human=None):
human = suffix

doc = """\
A string field that asserts the string is a valid {}.
A text field that asserts the text is a valid {}.
The validation is delegated to `{}`.
Expand All @@ -1179,7 +1140,7 @@ def create_from(foreign, name=None, human=None):
human, foreign
)

field_cls = type(name, (Str,), {'__doc__': doc})
field_cls = type(name, (Text,), {'__doc__': doc})

def __init__(self, **kwargs): # noqa: N807
super(field_cls, self).__init__(**kwargs)
Expand Down Expand Up @@ -1213,4 +1174,42 @@ def validate(self, value):

del create_from

FIELD_CLASS_MAP = {
# Built-in types
bool: Bool,
bytes: Bytes,
complex: Complex,
dict: Dict,
float: Float,
int: Int,
list: List,
set: Set,
str: Str,
tuple: Tuple,
# Collections
collections.deque: Deque,
collections.OrderedDict: OrderedDict,
# Datetimes
datetime.datetime: DateTime,
datetime.date: Date,
datetime.time: Time,
# Others
uuid.UUID: Uuid,
}

try:
FIELD_CLASS_MAP[basestring] = BaseString
except NameError:
pass

try:
FIELD_CLASS_MAP[long] = Long
except NameError:
pass

try:
FIELD_CLASS_MAP[unicode] = Unicode
except NameError:
pass

__all__ = [name for name, obj in globals().items() if is_subclass(obj, Field)]
6 changes: 3 additions & 3 deletions tests/test_fields.py
Expand Up @@ -1349,10 +1349,10 @@ def test___init__(self):
def test_validate(self):
# A Regex simply validates the given value matches the regex.
field = Regex(r'[est]{4}')
field.validate('test')
field.validate('tset')
field.validate(u'test')
field.validate(u'tset')
with raises(ValidationError):
field.validate('btesttest')
field.validate(u'btesttest')


class TestUuid:
Expand Down

0 comments on commit 5862375

Please sign in to comment.