Skip to content

Commit

Permalink
Merge pull request #209 from aranzgeo/feat/deprecate_bool
Browse files Browse the repository at this point in the history
Deprecate Bool in favor of Boolean
  • Loading branch information
fwkoch authored Feb 15, 2018
2 parents 5218f2b + 64fdcc7 commit 2316ab8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
6 changes: 3 additions & 3 deletions docs/content/primitive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Primitive Properties
====================

Bool
----
Boolean
-------

.. autoclass:: properties.Bool
.. autoclass:: properties.Boolean

Integer
-------
Expand Down
22 changes: 11 additions & 11 deletions docs/content/serialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ this state could be anything from an alternative JSON form to a saved file
to a web request.

Validation vs. Serialization/Deserialization
----------------------------
--------------------------------------------

For some Property types, validation and serialization/deserialization
look very similar; they both convert between an invalid-but-understood
value and a valid Property value. However, they remain separate because
they serve different purposes:

**Validation** and coercion happen on input of Property values and on
:code:`validate()`. This is taking "human-accessible" user input and
**Validation** and coercion happen on input of Property values and on
:code:`validate()`. This is taking "human-accessible" user input and
ensuring it is the "valid" type.

**Serialization** takes the *valid* :code:`HasProperties` class and converts it to
something that can be saved to a file. Deserialization is the reverse
**Serialization** takes the *valid* :code:`HasProperties` class and converts it to
something that can be saved to a file. Deserialization is the reverse
of that process, and should be used only on serialization's output.

With simple properties like strings, validation and serialization
almost identical. User input, valid value, and saveable-to-file value
are all just the same string. However, the differences are apparent with
more complicated properties like Array - in that case, user input may be
a list or a numpy array, valid type is a numpy array, and serialized
value may be a binary file or something. Validate needs to deal with the
With simple properties like strings, validation and serialization
almost identical. User input, valid value, and saveable-to-file value
are all just the same string. However, the differences are apparent with
more complicated properties like Array - in that case, user input may be
a list or a numpy array, valid type is a numpy array, and serialized
value may be a binary file or something. Validate needs to deal with the
user input whereas deserialize needs to deal with the binary file.
1 change: 1 addition & 0 deletions properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Profile(properties.HasProperties):

from .basic import (
Bool,
Boolean,
Color,
Complex,
DateTime,
Expand Down
11 changes: 8 additions & 3 deletions properties/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def sphinx(self):
return '{doc}{default}'.format(doc=prop_doc, default=default_str)


class Bool(Property):
class Boolean(Property):
"""Property for True or False values
**Available keywords** (in addition to those inherited from
Expand Down Expand Up @@ -744,6 +744,11 @@ def from_json(value, **kwargs):
raise ValueError('Could not load boolean from JSON: {}'.format(value))


# Alias Bool for backwards compatibility - this will be removed in a future
# release
Bool = Boolean


def _in_bounds(prop, instance, value):
"""Checks if the value is in the range (min, max)"""
if (
Expand All @@ -753,7 +758,7 @@ def _in_bounds(prop, instance, value):
prop.error(instance, value)


class Integer(Bool):
class Integer(Boolean):
"""Property for integer values
**Available keywords** (in addition to those inherited from
Expand Down Expand Up @@ -869,7 +874,7 @@ def from_json(value, **kwargs):
return float(value)


class Complex(Bool):
class Complex(Boolean):
"""Property for complex numbers
**Available keywords** (in addition to those inherited from
Expand Down
43 changes: 24 additions & 19 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,43 +180,48 @@ class UndocPrivate(PrivateProperty):

def test_bool(self):

for boolean in (properties.Bool, properties.Boolean):
self._test_bool_with(boolean)

def _test_bool_with(self, boolean):

class BoolOpts(properties.HasProperties):
mybool = properties.Bool('My bool')
mybool = boolean('My bool')

opt = BoolOpts(mybool=True)
assert opt.mybool is True
self.assertRaises(ValueError, lambda: setattr(opt, 'mybool', 'true'))
opt.mybool = False
assert opt.mybool is False

assert properties.Bool('').equal(True, True)
assert not properties.Bool('').equal(True, 1)
assert not properties.Bool('').equal(True, 'true')
assert boolean('').equal(True, True)
assert not boolean('').equal(True, 1)
assert not boolean('').equal(True, 'true')

json = properties.Bool.to_json(opt.mybool)
json = boolean.to_json(opt.mybool)
assert not json
assert not properties.Bool.from_json(json)
assert not boolean.from_json(json)
with self.assertRaises(ValueError):
properties.Bool.from_json({})
boolean.from_json({})
with self.assertRaises(ValueError):
properties.Bool.from_json('nope')
assert properties.Bool.from_json('true')
assert properties.Bool.from_json('y')
assert properties.Bool.from_json('Yes')
assert properties.Bool.from_json('ON')
assert not properties.Bool.from_json('false')
assert not properties.Bool.from_json('N')
assert not properties.Bool.from_json('no')
assert not properties.Bool.from_json('OFF')
boolean.from_json('nope')
assert boolean.from_json('true')
assert boolean.from_json('y')
assert boolean.from_json('Yes')
assert boolean.from_json('ON')
assert not boolean.from_json('false')
assert not boolean.from_json('N')
assert not boolean.from_json('no')
assert not boolean.from_json('OFF')

self.assertEqual(opt.serialize(include_class=False), {'mybool': False})

assert BoolOpts.deserialize({'mybool': 'Y'}).mybool
assert BoolOpts._props['mybool'].deserialize(None) is None

assert properties.Bool('').equal(True, True)
assert not properties.Bool('').equal(True, 1)
assert not properties.Bool('').equal(True, 'true')
assert boolean('').equal(True, True)
assert not boolean('').equal(True, 1)
assert not boolean('').equal(True, 'true')

with self.assertRaises(ValueError):
BoolOpts._props['mybool'].assert_valid(opt, 'true')
Expand Down

0 comments on commit 2316ab8

Please sign in to comment.