diff --git a/jsonschema/_format.py b/jsonschema/_format.py index bd2f95d..caae127 100644 --- a/jsonschema/_format.py +++ b/jsonschema/_format.py @@ -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}$") + @_checks_drafts(draft3="ip-address", draft4="ipv4") def is_ipv4(instance): 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}$") + @_checks_drafts(draft3="host-name", draft4="hostname") def is_host_name(instance): if not isinstance(instance, str_types): @@ -209,13 +211,13 @@ except ImportError: pass else: @_checks_drafts("date-time", raises=(ValueError, isodate.ISO8601Error)) - def is_date(instance): + def is_datetime(instance): if not isinstance(instance, str_types): return True return isodate.parse_datetime(instance) else: @_checks_drafts("date-time") - def is_date(instance): + def is_datetime(instance): if not isinstance(instance, str_types): return True return strict_rfc3339.validate_rfc3339(instance) @@ -250,7 +252,6 @@ else: def is_css_color_code(instance): return webcolors.normalize_hex(instance) - @_checks_drafts(draft3="color", raises=(ValueError, TypeError)) def is_css21_color(instance): if ( @@ -260,7 +261,6 @@ else: return True return is_css_color_code(instance) - def is_css3_color(instance): if instance.lower() in webcolors.css3_names_to_hex: return True diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 40fe340..cfdf66a 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -255,7 +255,7 @@ def properties_draft3(validator, properties, instance, schema): def disallow_draft3(validator, disallow, instance, schema): for disallowed in _utils.ensure_list(disallow): - if validator.is_valid(instance, {"type" : [disallowed]}): + if validator.is_valid(instance, {"type": [disallowed]}): yield ValidationError( "%r is disallowed for %r" % (disallowed, instance) ) diff --git a/jsonschema/cli.py b/jsonschema/cli.py index 6975264..fb1b0f5 100644 --- a/jsonschema/cli.py +++ b/jsonschema/cli.py @@ -26,22 +26,28 @@ parser.add_argument( action="append", dest="instances", type=_json_file, - help="a path to a JSON instance (i.e. filename.json)" - "to validate (may be specified multiple times)", + help=( + "a path to a JSON instance (i.e. filename.json)" + "to validate (may be specified multiple times)" + ), ) parser.add_argument( "-F", "--error-format", default="{error.instance}: {error.message}\n", - help="the format to use for each error output message, specified in " - "a form suitable for passing to str.format, which will be called " - "with 'error' for each error", + help=( + "the format to use for each error output message, specified in " + "a form suitable for passing to str.format, which will be called " + "with 'error' for each error" + ), ) parser.add_argument( "-V", "--validator", type=_namedAnyWithDefault, - help="the fully qualified object name of a validator to use, or, for " - "validators that are registered with jsonschema, simply the name " - "of the class.", + help=( + "the fully qualified object name of a validator to use, or, for " + "validators that are registered with jsonschema, simply the name " + "of the class." + ), ) parser.add_argument( "schema", diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 829034c..da3e852 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -56,9 +56,6 @@ class _Error(Exception): def __repr__(self): return "<%s: %r>" % (self.__class__.__name__, self.message) - def __str__(self): - return unicode(self).encode("utf-8") - def __unicode__(self): essential_for_verbose = ( self.validator, self.validator_value, self.instance, self.schema, @@ -86,6 +83,9 @@ class _Error(Exception): if PY3: __str__ = __unicode__ + else: + def __str__(self): + return unicode(self).encode("utf-8") @classmethod def create_from(cls, other): @@ -142,9 +142,6 @@ class UnknownType(Exception): self.instance = instance self.schema = schema - def __str__(self): - return unicode(self).encode("utf-8") - def __unicode__(self): pschema = pprint.pformat(self.schema, width=72) pinstance = pprint.pformat(self.instance, width=72) @@ -159,7 +156,9 @@ class UnknownType(Exception): if PY3: __str__ = __unicode__ - + else: + def __str__(self): + return unicode(self).encode("utf-8") class FormatError(Exception): @@ -168,14 +167,14 @@ class FormatError(Exception): self.message = message self.cause = self.__cause__ = cause - def __str__(self): - return self.message.encode("utf-8") - def __unicode__(self): return self.message if PY3: __str__ = __unicode__ + else: + def __str__(self): + return self.message.encode("utf-8") class ErrorTree(object): diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index f277af8..1d7a6c6 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -10,11 +10,9 @@ class TestBestMatch(unittest.TestCase): errors = list(errors) best = exceptions.best_match(errors) reversed_best = exceptions.best_match(reversed(errors)) + msg = "Didn't return a consistent best match!\nGot: {0}\n\nThen: {1}" self.assertEqual( - best, - reversed_best, - msg="Didn't return a consistent best match!\n" - "Got: {0}\n\nThen: {1}".format(best, reversed_best), + best, reversed_best, msg=msg.format(best, reversed_best), ) return best diff --git a/jsonschema/validators.py b/jsonschema/validators.py index db76845..ee1ec94 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -14,7 +14,7 @@ from jsonschema.compat import ( Sequence, urljoin, urlsplit, urldefrag, unquote, urlopen, 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 @@ -51,7 +51,7 @@ def validates(version): 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: default_types = { u"array": list, u"boolean": bool, u"integer": int_types, @@ -329,7 +329,7 @@ class RefResolver(object): raise RefResolutionError( "Failed to pop the scope from an empty stack. " "`pop_scope()` should only be called once for every " - "`push_scope()`", + "`push_scope()`" ) @property diff --git a/tox.ini b/tox.ini index fcc0954..b131119 100644 --- a/tox.ini +++ b/tox.ini @@ -60,4 +60,4 @@ commands = [flake8] -ignore = E203,E302,E303,E701,F811 +exclude = jsonschema/_reflect.py