From 7360fca581661d0c80fe7783392e555dedd68923 Mon Sep 17 00:00:00 2001 From: Artur Maciag Date: Fri, 17 Jan 2020 14:36:14 +0000 Subject: [PATCH] Flake8 check fixes --- openapi_core/contrib/flask/responses.py | 2 -- openapi_core/schema/content/exceptions.py | 3 +- openapi_core/schema/media_types/exceptions.py | 3 +- openapi_core/schema/media_types/models.py | 3 +- openapi_core/schema/parameters/exceptions.py | 6 ++-- openapi_core/schema/responses/exceptions.py | 3 +- openapi_core/schema/schemas/_format.py | 4 +-- openapi_core/schema/schemas/_types.py | 2 +- openapi_core/schema/schemas/exceptions.py | 2 +- openapi_core/schema/schemas/factories.py | 30 ++++++++++++---- openapi_core/schema/schemas/models.py | 35 ++++++++++--------- openapi_core/schema/schemas/unmarshallers.py | 2 +- openapi_core/schema/schemas/validators.py | 4 +-- openapi_core/schema/specs/models.py | 3 +- setup.cfg | 2 +- 15 files changed, 65 insertions(+), 39 deletions(-) diff --git a/openapi_core/contrib/flask/responses.py b/openapi_core/contrib/flask/responses.py index 6e1f3e2f..73e7605b 100644 --- a/openapi_core/contrib/flask/responses.py +++ b/openapi_core/contrib/flask/responses.py @@ -1,6 +1,4 @@ """OpenAPI core contrib flask responses module""" -import re - from openapi_core.validation.response.datatypes import OpenAPIResponse diff --git a/openapi_core/schema/content/exceptions.py b/openapi_core/schema/content/exceptions.py index 7711ea02..ab29b1b0 100644 --- a/openapi_core/schema/content/exceptions.py +++ b/openapi_core/schema/content/exceptions.py @@ -13,4 +13,5 @@ class MimeTypeNotFound(OpenAPIContentError): availableMimetypes = attr.ib() def __str__(self): - return "Mimetype not found: {0}. Valid mimetypes: {1}".format(self.mimetype, self.availableMimetypes) + return "Mimetype not found: {0}. Valid mimetypes: {1}".format( + self.mimetype, self.availableMimetypes) diff --git a/openapi_core/schema/media_types/exceptions.py b/openapi_core/schema/media_types/exceptions.py index 60a0c200..6e183949 100644 --- a/openapi_core/schema/media_types/exceptions.py +++ b/openapi_core/schema/media_types/exceptions.py @@ -20,4 +20,5 @@ class InvalidContentType(OpenAPIMediaTypeError): mimetype = attr.ib() def __str__(self): - return "Content for following mimetype not found: {0}".format(self.mimetype) + return "Content for following mimetype not found: {0}".format( + self.mimetype) diff --git a/openapi_core/schema/media_types/models.py b/openapi_core/schema/media_types/models.py index e593a1a0..b0ebfb4c 100644 --- a/openapi_core/schema/media_types/models.py +++ b/openapi_core/schema/media_types/models.py @@ -58,6 +58,7 @@ def unmarshal(self, value, custom_formatters=None, resolver=None): raise InvalidMediaTypeValue(exc) try: - return self.schema.unmarshal(value, custom_formatters=custom_formatters) + return self.schema.unmarshal( + value, custom_formatters=custom_formatters) except UnmarshalError as exc: raise InvalidMediaTypeValue(exc) diff --git a/openapi_core/schema/parameters/exceptions.py b/openapi_core/schema/parameters/exceptions.py index 79b61c48..9293eef3 100644 --- a/openapi_core/schema/parameters/exceptions.py +++ b/openapi_core/schema/parameters/exceptions.py @@ -17,7 +17,8 @@ class MissingParameter(MissingParameterError): name = attr.ib() def __str__(self): - return "Missing parameter (without default value): {0}".format(self.name) + return "Missing parameter (without default value): {0}".format( + self.name) @attr.s(hash=True) @@ -42,4 +43,5 @@ class InvalidParameterValue(OpenAPIParameterError): original_exception = attr.ib() def __str__(self): - return "Invalid parameter value for `{0}`: {1}".format(self.name, self.original_exception) + return "Invalid parameter value for `{0}`: {1}".format( + self.name, self.original_exception) diff --git a/openapi_core/schema/responses/exceptions.py b/openapi_core/schema/responses/exceptions.py index 9ec50eeb..577ad83d 100644 --- a/openapi_core/schema/responses/exceptions.py +++ b/openapi_core/schema/responses/exceptions.py @@ -13,7 +13,8 @@ class InvalidResponse(OpenAPIResponseError): responses = attr.ib() def __str__(self): - return "Unknown response http status: {0}".format(str(self.http_status)) + return "Unknown response http status: {0}".format( + str(self.http_status)) @attr.s(hash=True) diff --git a/openapi_core/schema/schemas/_format.py b/openapi_core/schema/schemas/_format.py index e2af856c..61704bd2 100644 --- a/openapi_core/schema/schemas/_format.py +++ b/openapi_core/schema/schemas/_format.py @@ -82,10 +82,10 @@ def is_datetime(instance): return False if not isinstance(instance, text_type): return True - + if DATETIME_HAS_STRICT_RFC3339: return strict_rfc3339.validate_rfc3339(instance) - + if DATETIME_HAS_ISODATE: return isodate.parse_datetime(instance) diff --git a/openapi_core/schema/schemas/_types.py b/openapi_core/schema/schemas/_types.py index 12d1d260..b647dfc3 100644 --- a/openapi_core/schema/schemas/_types.py +++ b/openapi_core/schema/schemas/_types.py @@ -1,5 +1,5 @@ from jsonschema._types import ( - TypeChecker, is_any, is_array, is_bool, is_integer, + TypeChecker, is_array, is_bool, is_integer, is_object, is_number, ) from six import text_type, binary_type diff --git a/openapi_core/schema/schemas/exceptions.py b/openapi_core/schema/schemas/exceptions.py index f325d18f..568812a4 100644 --- a/openapi_core/schema/schemas/exceptions.py +++ b/openapi_core/schema/schemas/exceptions.py @@ -101,7 +101,7 @@ def __str__(self): class UnmarshallerStrictTypeError(UnmarshallerError): value = attr.ib() types = attr.ib() - + def __str__(self): types = ', '.join(list(map(str, self.types))) return "Value {value} is not one of types: {types}".format( diff --git a/openapi_core/schema/schemas/factories.py b/openapi_core/schema/schemas/factories.py index b42fcbba..00e3f025 100644 --- a/openapi_core/schema/schemas/factories.py +++ b/openapi_core/schema/schemas/factories.py @@ -100,20 +100,38 @@ class SchemaDictFactory(object): Contribution('required', dest_default=[]), Contribution('default'), Contribution('nullable', dest_default=False), - Contribution('all_of', dest_prop_name='allOf', is_list=True, dest_default=[]), - Contribution('one_of', dest_prop_name='oneOf', is_list=True, dest_default=[]), - Contribution('additional_properties', dest_prop_name='additionalProperties', dest_default=True), + Contribution( + 'all_of', + dest_prop_name='allOf', is_list=True, dest_default=[], + ), + Contribution( + 'one_of', + dest_prop_name='oneOf', is_list=True, dest_default=[], + ), + Contribution( + 'additional_properties', + dest_prop_name='additionalProperties', dest_default=True, + ), Contribution('min_items', dest_prop_name='minItems'), Contribution('max_items', dest_prop_name='maxItems'), Contribution('min_length', dest_prop_name='minLength'), Contribution('max_length', dest_prop_name='maxLength'), Contribution('pattern', src_prop_attr='pattern'), - Contribution('unique_items', dest_prop_name='uniqueItems', dest_default=False), + Contribution( + 'unique_items', + dest_prop_name='uniqueItems', dest_default=False, + ), Contribution('minimum'), Contribution('maximum'), Contribution('multiple_of', dest_prop_name='multipleOf'), - Contribution('exclusive_minimum', dest_prop_name='exclusiveMinimum', dest_default=False), - Contribution('exclusive_maximum', dest_prop_name='exclusiveMaximum', dest_default=False), + Contribution( + 'exclusive_minimum', + dest_prop_name='exclusiveMinimum', dest_default=False, + ), + Contribution( + 'exclusive_maximum', + dest_prop_name='exclusiveMaximum', dest_default=False, + ), Contribution('min_properties', dest_prop_name='minProperties'), Contribution('max_properties', dest_prop_name='maxProperties'), ) diff --git a/openapi_core/schema/schemas/models.py b/openapi_core/schema/schemas/models.py index 6869cc7c..50957488 100644 --- a/openapi_core/schema/schemas/models.py +++ b/openapi_core/schema/schemas/models.py @@ -3,25 +3,20 @@ import functools import logging from collections import defaultdict -from datetime import date, datetime -from uuid import UUID import re import warnings -from six import iteritems, integer_types, binary_type, text_type +from six import iteritems from jsonschema.exceptions import ValidationError from openapi_core.extensions.models.factories import ModelFactory from openapi_core.schema.schemas._format import oas30_format_checker -from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType +from openapi_core.schema.schemas.enums import SchemaType from openapi_core.schema.schemas.exceptions import ( CastError, InvalidSchemaValue, - UnmarshallerError, UnmarshalValueError, UnmarshalError, -) -from openapi_core.schema.schemas.util import ( - forcebool, format_date, format_datetime, format_byte, format_uuid, - format_number, + UnmarshalValueError, UnmarshalError, ) +from openapi_core.schema.schemas.util import forcebool from openapi_core.schema.schemas.validators import OAS30Validator log = logging.getLogger(__name__) @@ -172,8 +167,9 @@ def get_unmarshal_mapping(self, custom_formatters=None, strict=True): for t, u in primitive_unmarshallers.items() ) - pass_defaults = lambda f: functools.partial( - f, custom_formatters=custom_formatters, strict=strict) + def pass_defaults(f): + return functools.partial( + f, custom_formatters=custom_formatters, strict=strict) mapping = self.DEFAULT_UNMARSHAL_CALLABLE_GETTER.copy() mapping.update(primitive_unmarshallers_partial) mapping.update({ @@ -186,7 +182,9 @@ def get_unmarshal_mapping(self, custom_formatters=None, strict=True): def get_validator(self, resolver=None): return OAS30Validator( - self.__dict__, resolver=resolver, format_checker=oas30_format_checker) + self.__dict__, + resolver=resolver, format_checker=oas30_format_checker, + ) def validate(self, value, resolver=None): validator = self.get_validator(resolver=resolver) @@ -254,7 +252,8 @@ def _unmarshal_any(self, value, custom_formatters=None, strict=True): result = None for subschema in self.one_of: try: - unmarshalled = subschema.unmarshal(value, custom_formatters) + unmarshalled = subschema.unmarshal( + value, custom_formatters) except UnmarshalError: continue else: @@ -278,9 +277,11 @@ def _unmarshal_any(self, value, custom_formatters=None, strict=True): log.warning("failed to unmarshal any type") return value - def _unmarshal_collection(self, value, custom_formatters=None, strict=True): + def _unmarshal_collection( + self, value, custom_formatters=None, strict=True): if not isinstance(value, (list, tuple)): - raise ValueError("Invalid value for collection: {0}".format(value)) + raise ValueError( + "Invalid value for collection: {0}".format(value)) f = functools.partial( self.items.unmarshal, @@ -300,7 +301,9 @@ def _unmarshal_object(self, value, model_factory=None, for one_of_schema in self.one_of: try: unmarshalled = self._unmarshal_properties( - value, one_of_schema, custom_formatters=custom_formatters) + value, one_of_schema, + custom_formatters=custom_formatters, + ) except (UnmarshalError, ValueError): pass else: diff --git a/openapi_core/schema/schemas/unmarshallers.py b/openapi_core/schema/schemas/unmarshallers.py index b3d0ece1..f24188ff 100644 --- a/openapi_core/schema/schemas/unmarshallers.py +++ b/openapi_core/schema/schemas/unmarshallers.py @@ -1,6 +1,6 @@ from six import text_type, binary_type, integer_types -from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType +from openapi_core.schema.schemas.enums import SchemaFormat from openapi_core.schema.schemas.exceptions import ( InvalidCustomFormatSchemaValue, UnmarshallerStrictTypeError, diff --git a/openapi_core/schema/schemas/validators.py b/openapi_core/schema/schemas/validators.py index 52cf1dc2..eabe4d4e 100644 --- a/openapi_core/schema/schemas/validators.py +++ b/openapi_core/schema/schemas/validators.py @@ -1,4 +1,4 @@ -from jsonschema import _legacy_validators, _format, _types, _utils, _validators +from jsonschema import _legacy_validators, _utils, _validators from jsonschema.validators import create from openapi_core.schema.schemas import _types as oas_types @@ -56,7 +56,7 @@ class OAS30Validator(BaseOAS30Validator): def iter_errors(self, instance, _schema=None): if _schema is None: - _schema = self.schema + _schema = self.schema # append defaults to trigger validator (i.e. nullable) if 'nullable' not in _schema: diff --git a/openapi_core/schema/specs/models.py b/openapi_core/schema/specs/models.py index f4a115e5..ad51554c 100644 --- a/openapi_core/schema/specs/models.py +++ b/openapi_core/schema/specs/models.py @@ -14,7 +14,8 @@ class Spec(object): """Represents an OpenAPI Specification for a service.""" - def __init__(self, info, paths, servers=None, components=None, _resolver=None): + def __init__( + self, info, paths, servers=None, components=None, _resolver=None): self.info = info self.paths = paths and dict(paths) self.servers = servers or [] diff --git a/setup.cfg b/setup.cfg index 6cfa7682..f7ff0888 100644 --- a/setup.cfg +++ b/setup.cfg @@ -48,4 +48,4 @@ django = django>=2.2; python_version>="3.0" flask = werkzeug [tool:pytest] -addopts = -sv --flake8 --junitxml reports/junit.xml --cov openapi_core --cov-report term-missing --cov-report xml:reports/coverage.xml tests +addopts = -sv --flake8 --junitxml reports/junit.xml --cov openapi_core --cov-report term-missing --cov-report xml:reports/coverage.xml