summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2018-08-24 12:06:18 -0500
committerJason Madden <jamadden@gmail.com>2018-08-24 12:06:18 -0500
commitbab4c3104b82d4f4172180390bde5aec10a53e4e (patch)
tree0a7406788fdc45f80fc63a4dd1044c172f92104f
parent845376502821b0ad4a08f5a3691b06cd49f1c9a3 (diff)
downloadzope-schema-issue16.tar.gz
Raise proper validation errors from Int and Float as well as Decimal.issue16
Fix the doctests that pointed out this should be done.
-rw-r--r--CHANGES.rst4
-rw-r--r--docs/fields.rst24
-rw-r--r--src/zope/schema/_bootstrapfields.py11
-rw-r--r--src/zope/schema/_field.py9
-rw-r--r--tox.ini5
5 files changed, 46 insertions, 7 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index d39da58..01bb7c1 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -46,6 +46,10 @@
attribute that is set to the field that raised the exception and the
value that failed validation.
+- ``Float``, ``Int`` and ``Decimal`` fields raise ``ValidationError``
+ subclasses for literals that cannot be parsed. These subclasses also
+ subclass ``ValueError`` for backwards compatibility.
+
- Add a new exception ``SchemaNotCorrectlyImplemented``, a subclass of
``WrongContainedType`` that is raised by the ``Object`` field. It
has a dictionary (``schema_errors``) mapping invalid schema
diff --git a/docs/fields.rst b/docs/fields.rst
index 7799441..7ebabaf 100644
--- a/docs/fields.rst
+++ b/docs/fields.rst
@@ -101,7 +101,27 @@ Conversion from Unicode:
>>> f.fromUnicode("1.25.6") #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
- ValueError: invalid literal for float(): 1.25.6
+ InvalidFloatLiteral: invalid literal for float(): 1.25.6
+
+Int
+###
+
+:class:`zope.schema.Int` fields contain binary data, represented
+as a a Python ``int``.
+
+Conversion from Unicode:
+
+.. doctest::
+
+ >>> from zope.schema import Int
+ >>> f = Int()
+ >>> f.fromUnicode("1")
+ 1
+ >>> f.fromUnicode("1.25.6") #doctest: +IGNORE_EXCEPTION_DETAIL
+ Traceback (most recent call last):
+ ...
+ InvalidIntLiteral: invalid literal for int() with base 10: 1.25.6
+
Decimal
#######
@@ -123,7 +143,7 @@ Conversion from Unicode:
>>> f.fromUnicode("1.25.6")
Traceback (most recent call last):
...
- ValueError: invalid literal for Decimal(): 1.25.6
+ InvalidDecimalLiteral: invalid literal for Decimal(): 1.25.6
Datetime
########
diff --git a/src/zope/schema/_bootstrapfields.py b/src/zope/schema/_bootstrapfields.py
index c96c7d8..6501310 100644
--- a/src/zope/schema/_bootstrapfields.py
+++ b/src/zope/schema/_bootstrapfields.py
@@ -19,6 +19,7 @@ from zope.interface import Attribute
from zope.interface import providedBy
from zope.interface import implementer
+from zope.schema._bootstrapinterfaces import ValidationError
from zope.schema._bootstrapinterfaces import ConstraintNotSatisfied
from zope.schema._bootstrapinterfaces import IContextAwareDefaultFactory
from zope.schema._bootstrapinterfaces import IFromUnicode
@@ -440,6 +441,9 @@ class Bool(Field):
self.validate(v)
return v
+class InvalidIntLiteral(ValueError, ValidationError):
+ """Invalid int literal."""
+
@implementer(IFromUnicode)
class Int(Orderable, Field):
@@ -458,8 +462,11 @@ class Int(Orderable, Field):
>>> f.fromUnicode("125.6") #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
- ValueError: invalid literal for int(): 125.6
+ InvalidIntLiteral: invalid literal for int(): 125.6
"""
- v = int(str)
+ try:
+ v = int(str)
+ except ValueError as v:
+ raise InvalidIntLiteral(*v.args).with_field_and_value(self, str)
self.validate(v)
return v
diff --git a/src/zope/schema/_field.py b/src/zope/schema/_field.py
index 21edeb6..807ca48 100644
--- a/src/zope/schema/_field.py
+++ b/src/zope/schema/_field.py
@@ -185,6 +185,10 @@ class ASCIILine(ASCII):
return '\n' not in value
+class InvalidFloatLiteral(ValueError, ValidationError):
+ """Raised by Float fields."""
+
+
@implementer(IFloat, IFromUnicode)
class Float(Orderable, Field):
__doc__ = IFloat.__doc__
@@ -196,7 +200,10 @@ class Float(Orderable, Field):
def fromUnicode(self, uc):
""" See IFromUnicode.
"""
- v = float(uc)
+ try:
+ v = float(uc)
+ except ValueError as v:
+ raise InvalidFloatLiteral(*v.args).with_field_and_value(self, uc)
self.validate(v)
return v
diff --git a/tox.ini b/tox.ini
index 8012429..184397e 100644
--- a/tox.ini
+++ b/tox.ini
@@ -7,9 +7,10 @@ envlist =
[testenv]
deps =
- .[test]
+ .[test,docs]
commands =
zope-testrunner --test-path=src
+ sphinx-build -b doctest -d {envdir}/.cache/doctrees docs {envdir}/.cache/doctest
[testenv:coverage]
usedevelop = true
@@ -24,7 +25,7 @@ deps =
[testenv:docs]
basepython =
- python2.7
+ python3.6
commands =
sphinx-build -b html -d docs/_build/doctrees docs docs/_build/html
sphinx-build -b doctest -d docs/_build/doctrees docs docs/_build/doctest