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

Commit

Permalink
Shadow built-in type names less
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Feb 15, 2020
1 parent 2a54886 commit a06fd2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
48 changes: 24 additions & 24 deletions src/serde/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,20 +464,20 @@ class Instance(Field):
**kwargs: keyword arguments for the `Field` constructor.
"""

def __init__(self, type, **kwargs):
def __init__(self, ty, **kwargs):
"""
Create a new `Instance`.
"""
super(Instance, self).__init__(**kwargs)
self.type = type
self.ty = ty

def validate(self, value):
"""
Validate the given value is an instance of the specified type.
"""
if not isinstance(value, self.type):
if not isinstance(value, self.ty):
raise ValidationError(
'invalid type, expected {!r}'.format(self.type.__name__), value=value
'invalid type, expected {!r}'.format(self.ty.__name__), value=value
)


Expand All @@ -503,19 +503,19 @@ def deserialize(self, d):
"""
if not isinstance(d, MappingType):
raise ValidationError("invalid type, expected 'mapping'", value=d)
return self.type.from_dict(d)
return self.ty.from_dict(d)


class _Container(Instance):
"""
A base class for `Dict`, `List`, `Tuple`, and other container fields.
"""

def __init__(self, type, **kwargs):
def __init__(self, ty, **kwargs):
"""
Create a new `_Container`.
"""
super(_Container, self).__init__(type, **kwargs)
super(_Container, self).__init__(ty, **kwargs)
self.kwargs = {}

def _iter(self, value):
Expand All @@ -537,7 +537,7 @@ def serialize(self, value):
Each element in the container will be serialized with the specified
field instances.
"""
value = self.type(
value = self.ty(
(self._apply('_serialize', element) for element in self._iter(value)),
**self.kwargs
)
Expand All @@ -551,7 +551,7 @@ def deserialize(self, value):
field instances.
"""
value = super(_Container, self).deserialize(value)
return self.type(
return self.ty(
(self._apply('_deserialize', element) for element in self._iter(value)),
**self.kwargs
)
Expand All @@ -564,7 +564,7 @@ def normalize(self, value):
field instances.
"""
value = super(_Container, self).normalize(value)
return self.type(
return self.ty(
(self._apply('_normalize', element) for element in self._iter(value)),
**self.kwargs
)
Expand All @@ -586,8 +586,8 @@ class _Mapping(_Container):
A mapping field to be used as the base class for `Dict` and `OrderedDict`.
"""

def __init__(self, type, key=None, value=None, **kwargs):
super(_Mapping, self).__init__(type, **kwargs)
def __init__(self, ty, key=None, value=None, **kwargs):
super(_Mapping, self).__init__(ty, **kwargs)
self.key = _resolve_to_field_instance(key)
self.value = _resolve_to_field_instance(value)

Expand All @@ -600,7 +600,7 @@ def _iter(self, value):
yield element
except (AttributeError, TypeError):
raise ValidationError(
'invalid type, expected {!r}'.format(self.type.__name__), value=value
'invalid type, expected {!r}'.format(self.ty.__name__), value=value
)

def _apply(self, stage, element):
Expand Down Expand Up @@ -652,8 +652,8 @@ class _Sequence(_Container):
A sequence field to be used as the base class for fields such as `List` and `Set`
"""

def __init__(self, type, element=None, **kwargs):
super(_Sequence, self).__init__(type, **kwargs)
def __init__(self, ty, element=None, **kwargs):
super(_Sequence, self).__init__(ty, **kwargs)
self.element = _resolve_to_field_instance(element)

def _iter(self, value):
Expand All @@ -665,7 +665,7 @@ def _iter(self, value):
yield element
except TypeError:
raise ValidationError(
'invalid type, expected {!r}'.format(self.type.__name__), value=value
'invalid type, expected {!r}'.format(self.ty.__name__), value=value
)


Expand Down Expand Up @@ -785,21 +785,21 @@ def _apply(self, stage, element):
return getattr(f, stage)(v)


def create_primitive(name, type_):
def create_primitive(name, ty):
"""
Create a primitive `Field` class.
"""
doc = """\
This field represents the built-in `{type}` type.
This field represents the built-in `{ty}` type.
Args:
**kwargs: keyword arguments for the `Field` constructor.
""".format(
type=type_
ty=ty
)

def __init__(self, **kwargs): # noqa: N807
Instance.__init__(self, type_, **kwargs)
Instance.__init__(self, ty, **kwargs)

__init__.__doc__ = 'Create a new `{name}`.'.format(name=name)

Expand Down Expand Up @@ -915,13 +915,13 @@ class DateTime(Instance):
**kwargs: keyword arguments for the `Field` constructor.
"""

type = datetime.datetime
ty = datetime.datetime

def __init__(self, format='iso8601', **kwargs):
"""
Create a new `DateTime`.
"""
super(DateTime, self).__init__(self.__class__.type, **kwargs)
super(DateTime, self).__init__(self.__class__.ty, **kwargs)
self.format = format

def serialize(self, value):
Expand Down Expand Up @@ -959,7 +959,7 @@ class Date(DateTime):
This field behaves in a similar fashion to the `DateTime` field.
"""

type = datetime.date
ty = datetime.date

def deserialize(self, value):
"""
Expand Down Expand Up @@ -987,7 +987,7 @@ class Time(DateTime):
This field behaves in a similar fashion to the `DateTime` field.
"""

type = datetime.time
ty = datetime.time

def deserialize(self, value):
"""
Expand Down
18 changes: 9 additions & 9 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,13 @@ class TestInstance:
def test___init___basic(self):
# Construct a basic Instance and check values are set correctly.
field = Instance(int)
assert field.type == int
assert field.ty == int
assert field.validators == []

def test___init___options(self):
# Construct an Instance and make sure values are passed to Field.
field = Instance(int, validators=[None])
assert field.type == int
assert field.ty == int
assert field.validators == [None]

def test_validate(self):
Expand All @@ -620,14 +620,14 @@ class TestNested:
def test___init___basic(self):
# Construct a basic Nested and check values are set correctly.
field = Nested(Model)
assert field.type == Model
assert field.ty == Model
assert field.validators == []

def test___init___options(self):
# Construct a Nested with extra options and make sure values are passed
# to Field.
field = Nested(Model, validators=[None])
assert field.type == Model
assert field.ty == Model
assert field.validators == [None]

def test_serialize(self):
Expand Down Expand Up @@ -665,7 +665,7 @@ class TestMapping:
def test___init___basic(self):
# Construct a basic _Mapping and check values are set correctly.
field = _Mapping(dict)
assert field.type == dict
assert field.ty == dict
assert field.key == Field()
assert field.value == Field()
assert field.validators == []
Expand All @@ -674,7 +674,7 @@ def test___init___options(self):
# Construct a _Mapping with extra options and make sure values are
# passed to Field.
field = _Mapping(dict, key=Str, value=Int, validators=[None])
assert field.type == dict
assert field.ty == dict
assert field.key == Str()
assert field.value == Int()
assert field.validators == [None]
Expand Down Expand Up @@ -791,15 +791,15 @@ class TestSequence:
def test___init___basic(self):
# Construct a basic _Sequence and check values are set correctly.
field = _Sequence(list)
assert field.type == list
assert field.ty == list
assert field.element == Field()
assert field.validators == []

def test___init___options(self):
# Construct a _Sequence with extra options and make sure values are
# passed to Field.
field = _Sequence(list, element=Str, validators=[None])
assert field.type == list
assert field.ty == list
assert field.element == Str()
assert field.validators == [None]

Expand Down Expand Up @@ -1359,7 +1359,7 @@ class TestUuid:
def test___init__(self):
# Construct a basic Uuid and check values are set correctly.
field = Uuid()
assert field.type == uuid.UUID
assert field.ty == uuid.UUID

def test_serialize(self):
# A Uuid should serialize a uuid.UUID as a string.
Expand Down

0 comments on commit a06fd2a

Please sign in to comment.