Пример #1
0
def _install_request_parser_annotation(f, base_generic_type: Type):
    handler_type_hints = get_handler_original_typehints(f)
    path_params: List[RequestParserAnnotationSpec] = []

    for name, _type in handler_type_hints.items():
        if is_base_type(_type, base_generic_type):
            generic_type_args = get_args(_type)
            if not generic_type_args:
                raise TypeError(
                    f"{base_generic_type} must be Generic Type. Your handler {get_handler_original_qualname(f)} declares a parametrer that's not {base_generic_type}[T]"
                )
            if generic_type_args:
                path_params.append(
                    RequestParserAnnotationSpec(name=name,
                                                base=_type,
                                                arg_type=generic_type_args[0]))

    @wraps(f)
    async def _wrap(wrapper: RequestWrapper):
        for p in path_params:
            try:
                typed_val = await p.base.from_request(request=wrapper,
                                                      arg_name=p.name,
                                                      arg_type=p.arg_type)
                wrapper.types_registry.set(typed_val,
                                           p.base,
                                           param_name=p.name)
            except ValueError as e:
                raise HTTPBadRequest(text=e.args[0])
        return await call_http_handler(wrapper, f)

    return _wrap
Пример #2
0
    def get(self,
            _type: Type,
            param_name: str = None,
            type_args=None) -> Optional[Any]:
        origin = get_origin(_type) or _type
        _type_args = type_args or get_args(_type)
        if param_name:
            try:
                item = self._by_name[param_name]
                if (item.type, item.type_args) == (origin, _type_args):
                    return item.value
            except KeyError:
                return None

        try:
            return self._data[(origin, get_args(_type))].value
        except KeyError:
            return None
Пример #3
0
    def set(
        self,
        obj: Any,
        type_definition: Type = None,
        param_name: Optional[str] = None,
    ) -> None:
        has_type_args = get_args(type_definition)
        origin = get_origin(obj) or obj.__class__

        registry_item = RegistryItem(type=origin,
                                     value=obj,
                                     type_args=has_type_args)

        self._data[(origin, has_type_args)] = registry_item

        if param_name:
            self._by_name[param_name] = registry_item
 async def test_get_args_concrete_type(self):
     self.assertIsNone(get_args(MyObject))
 async def test_get_args_generic_type(self):
     self.assertEqual((MyObject,), get_args(MyGeneric[MyObject]))
     self.assertEqual(
         (int, MyObject), get_args(MyGenericTwoArguments[int, MyObject])
     )