From 198424133584acc18cb41c8d18d91f803abc810f Mon Sep 17 00:00:00 2001 From: Tim Savage Date: Thu, 6 Sep 2018 22:35:02 +1000 Subject: [PATCH] Correct type hints --- odinweb/containers.py | 11 +++++------ odinweb/data_structures.py | 8 ++++---- odinweb/helpers.py | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/odinweb/containers.py b/odinweb/containers.py index 1892824..eab47ce 100644 --- a/odinweb/containers.py +++ b/odinweb/containers.py @@ -75,11 +75,14 @@ def __new__(mcs, name, bases, attrs): if api_resource: break + new_class = super_new(mcs, name, bases, attrs) + # Get operations operations = [] for obj in attrs.values(): if isinstance(obj, Operation): operations.append(obj) + obj.bind_to_instance(new_class) # Get routes from parent objects for parent in parents: @@ -87,7 +90,6 @@ def __new__(mcs, name, bases, attrs): if parent_ops: operations.extend(parent_ops) - new_class = super_new(mcs, name, bases, attrs) setattr(new_class, '_operations', sorted(operations, key=lambda o: o.sort_key)) return new_class @@ -126,9 +128,6 @@ def __init__(self): # Append APIs name to path prefix self.path_prefix += self.api_name - for operation in self._operations: - operation.bind_to_instance(self) - def op_paths(self, path_base): # type: (Union[str, UrlPath]) -> Generator[Tuple[UrlPath, Operation]] """ @@ -174,7 +173,7 @@ def __init__(self, *containers, **options): raise TypeError("Got an unexpected keyword argument(s) {}", options.keys()) def operation(self, path, methods=Method.GET, resource=None, tags=None, summary=None, middleware=None): - # type: (UrlPath, Union(Method, Tuple[Method]), Type[Resource], Tags, str, list) -> Operation + # type: (UrlPath, Union[Method, Tuple[Method]], Type[Resource], Tags, str, list) -> Operation """ :param path: A sub path that can be used as a action. :param methods: HTTP method(s) this function responses to. @@ -274,7 +273,7 @@ def __init__(self, *containers, **options): raise ValueError("Path prefix must be an absolute path (eg start with a '/')") def handle_500(self, request, exception): - # type: (BaseHttpRequest, BaseException) + # type: (BaseHttpRequest, BaseException) -> Resource """ Handle an *un-handled* exception. """ diff --git a/odinweb/data_structures.py b/odinweb/data_structures.py index 463f002..5d7c7c6 100644 --- a/odinweb/data_structures.py +++ b/odinweb/data_structures.py @@ -295,7 +295,7 @@ def parse(cls, url_path): return cls(*nodes) def __init__(self, *nodes): - # type: (*Union(str, PathParam)) -> None + # type: (*Union[str, PathParam]) -> None self._nodes = nodes def __hash__(self): @@ -482,7 +482,7 @@ def form(cls, name, type_=Type.String, description=None, required=None, default= enum=enum, **options) def __init__(self, name, in_, type_=None, resource=None, description=None, **options): - # type: (str, In, Optional[Type], Optional(Resource), Optional(str), **Any) -> None + # type: (str, In, Optional[Type], Optional[Resource], Optional[str], **Any) -> None self.name = name self.in_ = in_ self.type = type_ @@ -527,7 +527,7 @@ class Response(object): __slots__ = ('status', 'description', 'resource') def __init__(self, status, description=None, resource=DefaultResource): - # type: (HTTPStatus, str, Optional(Resource)) -> None + # type: (HTTPStatus, str, Optional[Resource]) -> None self.status = status self.description = description self.resource = resource @@ -567,7 +567,7 @@ class DefaultResponse(Response): Default response object """ def __init__(self, description, resource=DefaultResource): - # type: (str, Optional(Resource)) -> None + # type: (str, Optional[Resource]) -> None super(DefaultResponse, self).__init__('default', description, resource) diff --git a/odinweb/helpers.py b/odinweb/helpers.py index 8b654d1..b326331 100644 --- a/odinweb/helpers.py +++ b/odinweb/helpers.py @@ -25,7 +25,7 @@ def parse_content_type(value): def resolve_content_type(type_resolvers, request): - # type: (Iterable[Callable((Any,), str), Any]) -> Optional[str] + # type: (Iterable[Callable[[Any], str]], Any) -> Optional[str] """ Resolve content types from a request. """