More style fun.

This commit is contained in:
Julian Berman
2016-11-27 11:24:05 -05:00
parent e7d2975b08
commit ee2de6d0e0
7 changed files with 34 additions and 31 deletions

View File

@@ -156,6 +156,7 @@ def is_email(instance):
_ipv4_re = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") _ipv4_re = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
@_checks_drafts(draft3="ip-address", draft4="ipv4") @_checks_drafts(draft3="ip-address", draft4="ipv4")
def is_ipv4(instance): def is_ipv4(instance):
if not isinstance(instance, str_types): if not isinstance(instance, str_types):
@@ -175,6 +176,7 @@ if hasattr(socket, "inet_pton"):
_host_name_re = re.compile(r"^[A-Za-z0-9][A-Za-z0-9\.\-]{1,255}$") _host_name_re = re.compile(r"^[A-Za-z0-9][A-Za-z0-9\.\-]{1,255}$")
@_checks_drafts(draft3="host-name", draft4="hostname") @_checks_drafts(draft3="host-name", draft4="hostname")
def is_host_name(instance): def is_host_name(instance):
if not isinstance(instance, str_types): if not isinstance(instance, str_types):
@@ -209,13 +211,13 @@ except ImportError:
pass pass
else: else:
@_checks_drafts("date-time", raises=(ValueError, isodate.ISO8601Error)) @_checks_drafts("date-time", raises=(ValueError, isodate.ISO8601Error))
def is_date(instance): def is_datetime(instance):
if not isinstance(instance, str_types): if not isinstance(instance, str_types):
return True return True
return isodate.parse_datetime(instance) return isodate.parse_datetime(instance)
else: else:
@_checks_drafts("date-time") @_checks_drafts("date-time")
def is_date(instance): def is_datetime(instance):
if not isinstance(instance, str_types): if not isinstance(instance, str_types):
return True return True
return strict_rfc3339.validate_rfc3339(instance) return strict_rfc3339.validate_rfc3339(instance)
@@ -250,7 +252,6 @@ else:
def is_css_color_code(instance): def is_css_color_code(instance):
return webcolors.normalize_hex(instance) return webcolors.normalize_hex(instance)
@_checks_drafts(draft3="color", raises=(ValueError, TypeError)) @_checks_drafts(draft3="color", raises=(ValueError, TypeError))
def is_css21_color(instance): def is_css21_color(instance):
if ( if (
@@ -260,7 +261,6 @@ else:
return True return True
return is_css_color_code(instance) return is_css_color_code(instance)
def is_css3_color(instance): def is_css3_color(instance):
if instance.lower() in webcolors.css3_names_to_hex: if instance.lower() in webcolors.css3_names_to_hex:
return True return True

View File

@@ -255,7 +255,7 @@ def properties_draft3(validator, properties, instance, schema):
def disallow_draft3(validator, disallow, instance, schema): def disallow_draft3(validator, disallow, instance, schema):
for disallowed in _utils.ensure_list(disallow): for disallowed in _utils.ensure_list(disallow):
if validator.is_valid(instance, {"type" : [disallowed]}): if validator.is_valid(instance, {"type": [disallowed]}):
yield ValidationError( yield ValidationError(
"%r is disallowed for %r" % (disallowed, instance) "%r is disallowed for %r" % (disallowed, instance)
) )

View File

@@ -26,22 +26,28 @@ parser.add_argument(
action="append", action="append",
dest="instances", dest="instances",
type=_json_file, type=_json_file,
help="a path to a JSON instance (i.e. filename.json)" help=(
"to validate (may be specified multiple times)", "a path to a JSON instance (i.e. filename.json)"
"to validate (may be specified multiple times)"
),
) )
parser.add_argument( parser.add_argument(
"-F", "--error-format", "-F", "--error-format",
default="{error.instance}: {error.message}\n", default="{error.instance}: {error.message}\n",
help="the format to use for each error output message, specified in " help=(
"the format to use for each error output message, specified in "
"a form suitable for passing to str.format, which will be called " "a form suitable for passing to str.format, which will be called "
"with 'error' for each error", "with 'error' for each error"
),
) )
parser.add_argument( parser.add_argument(
"-V", "--validator", "-V", "--validator",
type=_namedAnyWithDefault, type=_namedAnyWithDefault,
help="the fully qualified object name of a validator to use, or, for " help=(
"the fully qualified object name of a validator to use, or, for "
"validators that are registered with jsonschema, simply the name " "validators that are registered with jsonschema, simply the name "
"of the class.", "of the class."
),
) )
parser.add_argument( parser.add_argument(
"schema", "schema",

View File

@@ -56,9 +56,6 @@ class _Error(Exception):
def __repr__(self): def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.message) return "<%s: %r>" % (self.__class__.__name__, self.message)
def __str__(self):
return unicode(self).encode("utf-8")
def __unicode__(self): def __unicode__(self):
essential_for_verbose = ( essential_for_verbose = (
self.validator, self.validator_value, self.instance, self.schema, self.validator, self.validator_value, self.instance, self.schema,
@@ -86,6 +83,9 @@ class _Error(Exception):
if PY3: if PY3:
__str__ = __unicode__ __str__ = __unicode__
else:
def __str__(self):
return unicode(self).encode("utf-8")
@classmethod @classmethod
def create_from(cls, other): def create_from(cls, other):
@@ -142,9 +142,6 @@ class UnknownType(Exception):
self.instance = instance self.instance = instance
self.schema = schema self.schema = schema
def __str__(self):
return unicode(self).encode("utf-8")
def __unicode__(self): def __unicode__(self):
pschema = pprint.pformat(self.schema, width=72) pschema = pprint.pformat(self.schema, width=72)
pinstance = pprint.pformat(self.instance, width=72) pinstance = pprint.pformat(self.instance, width=72)
@@ -159,7 +156,9 @@ class UnknownType(Exception):
if PY3: if PY3:
__str__ = __unicode__ __str__ = __unicode__
else:
def __str__(self):
return unicode(self).encode("utf-8")
class FormatError(Exception): class FormatError(Exception):
@@ -168,14 +167,14 @@ class FormatError(Exception):
self.message = message self.message = message
self.cause = self.__cause__ = cause self.cause = self.__cause__ = cause
def __str__(self):
return self.message.encode("utf-8")
def __unicode__(self): def __unicode__(self):
return self.message return self.message
if PY3: if PY3:
__str__ = __unicode__ __str__ = __unicode__
else:
def __str__(self):
return self.message.encode("utf-8")
class ErrorTree(object): class ErrorTree(object):

View File

@@ -10,11 +10,9 @@ class TestBestMatch(unittest.TestCase):
errors = list(errors) errors = list(errors)
best = exceptions.best_match(errors) best = exceptions.best_match(errors)
reversed_best = exceptions.best_match(reversed(errors)) reversed_best = exceptions.best_match(reversed(errors))
msg = "Didn't return a consistent best match!\nGot: {0}\n\nThen: {1}"
self.assertEqual( self.assertEqual(
best, best, reversed_best, msg=msg.format(best, reversed_best),
reversed_best,
msg="Didn't return a consistent best match!\n"
"Got: {0}\n\nThen: {1}".format(best, reversed_best),
) )
return best return best

View File

@@ -14,7 +14,7 @@ from jsonschema.compat import (
Sequence, urljoin, urlsplit, urldefrag, unquote, urlopen, Sequence, urljoin, urlsplit, urldefrag, unquote, urlopen,
str_types, int_types, iteritems, lru_cache, str_types, int_types, iteritems, lru_cache,
) )
from jsonschema.exceptions import ErrorTree # Backwards compatibility # noqa from jsonschema.exceptions import ErrorTree # Backwards compat # noqa: F401
from jsonschema.exceptions import RefResolutionError, SchemaError, UnknownType from jsonschema.exceptions import RefResolutionError, SchemaError, UnknownType
@@ -51,7 +51,7 @@ def validates(version):
return _validates return _validates
def create(meta_schema, validators=(), version=None, default_types=None): # noqa def create(meta_schema, validators=(), version=None, default_types=None): # noqa: C901, E501
if default_types is None: if default_types is None:
default_types = { default_types = {
u"array": list, u"boolean": bool, u"integer": int_types, u"array": list, u"boolean": bool, u"integer": int_types,
@@ -329,7 +329,7 @@ class RefResolver(object):
raise RefResolutionError( raise RefResolutionError(
"Failed to pop the scope from an empty stack. " "Failed to pop the scope from an empty stack. "
"`pop_scope()` should only be called once for every " "`pop_scope()` should only be called once for every "
"`push_scope()`", "`push_scope()`"
) )
@property @property

View File

@@ -60,4 +60,4 @@ commands =
[flake8] [flake8]
ignore = E203,E302,E303,E701,F811 exclude = jsonschema/_reflect.py