From 875c62c1bc1a71cdaf45e2feba4ad01e98709e55 Mon Sep 17 00:00:00 2001 From: Will Smith Date: Wed, 23 May 2018 09:39:57 -0400 Subject: [PATCH 1/2] Added Travis job, mypy type checking Didn't annotate much, but I did add type annotations enough to make mypy not complain. I'll expand type annotations in the future as make sure to keep mypy happy. --- .gitignore | 1 + .travis.yml | 10 ++++++++++ openapi/__init__.py | 2 +- openapi/general.py | 6 ++++-- openapi/info.py | 6 ++++-- openapi/object_base.py | 4 +++- openapi/openapi.py | 4 +++- openapi/paths.py | 11 +++++------ openapi/schemas.py | 3 ++- 9 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 .travis.yml diff --git a/.gitignore b/.gitignore index e00ae94..92b8891 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ openapi.yaml *.swp *.pyc +.mypy_cache diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7525cb4 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: python +python: + - "3.3" + - "3.4" + - "3.5" + - "3.6" +install: + - pip install mypy +script: + - mypy openapi diff --git a/openapi/__init__.py b/openapi/__init__.py index decd2fe..4faa103 100644 --- a/openapi/__init__.py +++ b/openapi/__init__.py @@ -3,4 +3,4 @@ # that they may be referenced throughout the schema without issue from . import info, servers, paths, general, schemas, components, security -__all__ = [OpenAPI] +__all__ = ["OpenAPI"] diff --git a/openapi/general.py b/openapi/general.py index 04a178e..4ee3440 100644 --- a/openapi/general.py +++ b/openapi/general.py @@ -1,3 +1,5 @@ +from typing import List + from .object_base import ObjectBase class ExternalDocumentation(ObjectBase): @@ -8,7 +10,7 @@ class ExternalDocumentation(ObjectBase): .. _External Documentation Object: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#externalDocumentationObject """ __slos__ = ['description','url'] - required_fields = 'url' + required_fields: List[str] = ['url'] def _parse_data(self): self.description = self._get('description', str) @@ -21,7 +23,7 @@ class Reference(ObjectBase): .. _Reference Object: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#referenceObject """ __slots__ = ['ref'] # can't start a variable name with a $ - required_fields = ['$ref'] + required_fields: List[str] = ['$ref'] def _parse_data(self): self.ref = self._get('$ref', str) diff --git a/openapi/info.py b/openapi/info.py index 7e016a3..11e1a12 100644 --- a/openapi/info.py +++ b/openapi/info.py @@ -1,3 +1,5 @@ +from typing import List + from .object_base import ObjectBase class Info(ObjectBase): @@ -7,7 +9,7 @@ class Info(ObjectBase): .. _the spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#infoObject """ __slots__ = ['title','description','termsOfService','contact','license','version'] - required_fields = ['title','version'] + required_fields: List[str] = ['title','version'] def _parse_data(self): """ @@ -43,7 +45,7 @@ class License(ObjectBase): .. _here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#license-object """ __slots__ = ['name','url'] - required_fields = ['name'] + required_fields: List[str] = ['name'] def _parse_data(self): """ diff --git a/openapi/object_base.py b/openapi/object_base.py index 70ffb44..aebe044 100644 --- a/openapi/object_base.py +++ b/openapi/object_base.py @@ -1,3 +1,5 @@ +from typing import List + from .errors import SpecError class ObjectBase: @@ -7,7 +9,7 @@ class ObjectBase: """ __slots__ = ['path','raw_element','_accessed_members','strict','extensions', '_root', '_original_ref'] - required_fields = [] + required_fields: List[str] = [] def __init__(self, path, raw_element, root): """ diff --git a/openapi/openapi.py b/openapi/openapi.py index 4404004..0cc3cb9 100644 --- a/openapi/openapi.py +++ b/openapi/openapi.py @@ -1,3 +1,5 @@ +from typing import List + from .object_base import ObjectBase, Map from .errors import ReferenceResolutionError @@ -10,7 +12,7 @@ class OpenAPI(ObjectBase): """ __slots__ = ['openapi','info','servers','paths','components','security','tags', 'externalDocs','_operation_map','_security'] - required_fields=['openapi','info','paths'] + required_fields: List[str] = ['openapi','info','paths'] def __init__(self, raw_document): """ diff --git a/openapi/paths.py b/openapi/paths.py index aa244a1..928a823 100644 --- a/openapi/paths.py +++ b/openapi/paths.py @@ -1,5 +1,6 @@ import json import requests +from typing import List from urllib.parse import urlencode # TODO - this will break in python2 from .errors import SpecError @@ -48,7 +49,7 @@ class Parameter(ObjectBase): __slots__ = ['name','in','in_','description','required','deprecated', 'allowEmptyValue','style','explode','allowReserved','schema', 'example','examples'] - required_fields = ['name','in'] + required_fields: List[str] = ['name','in'] def _parse_data(self): @@ -81,7 +82,7 @@ class Operation(ObjectBase): __slots__ = ['tags','summary','description','externalDocs','operationId', 'parameters','requestBody','responses','callbacks','deprecated', 'security','servers'] - required_fields = ['responses'] + required_fields: List[str] = ['responses'] def _parse_data(self): """ @@ -252,7 +253,6 @@ class SecurityRequirement(ObjectBase): """ """ ___slots__ = ['name','types'] - required_fields=[] def _parse_data(self): """ @@ -280,7 +280,7 @@ class RequestBody(ObjectBase): .. _RequestBody: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#requestBodyObject """ __slots__ = ['description','content','required'] - required_fields = ['content'] + required_fields: List[str] = ['content'] def _parse_data(self): """ @@ -300,7 +300,6 @@ class MediaType(ObjectBase): .. _MediaType: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#mediaTypeObject """ __slots__ = ['schema','example','examples','encoding'] - required_fields = [] def _parse_data(self): """ @@ -323,7 +322,7 @@ class Response(ObjectBase): .. _Response Object: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#response-object """ __slots__ = ['description','headers','content','links'] - required_fields = ['description'] + required_fields: List[str] = ['description'] def _parse_data(self): """ diff --git a/openapi/schemas.py b/openapi/schemas.py index a8ef003..9573be9 100644 --- a/openapi/schemas.py +++ b/openapi/schemas.py @@ -1,3 +1,5 @@ +from typing import List + from .object_base import ObjectBase from .general import Reference # need this for Model below @@ -14,7 +16,6 @@ class Schema(ObjectBase): 'properties','additionalProperties','description','format', 'default','nullable','discriminator','readOnly','writeOnly', 'xml','externalDocs','example','deprecated','_model_type'] - required_fields = [] def _parse_data(self): """ From 02631d5d7cb25d126052d5c5fcca004b27d748d0 Mon Sep 17 00:00:00 2001 From: Will Smith Date: Wed, 23 May 2018 09:52:57 -0400 Subject: [PATCH 2/2] Turns out this only works in python 3.6 --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7525cb4..7046a26 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,5 @@ language: python python: - - "3.3" - - "3.4" - - "3.5" - "3.6" install: - pip install mypy