Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource(many=True) and issubclass #8

Closed
matrixise opened this issue Mar 15, 2019 · 2 comments
Closed

Resource(many=True) and issubclass #8

matrixise opened this issue Mar 15, 2019 · 2 comments

Comments

@matrixise
Copy link
Contributor

matrixise commented Mar 15, 2019

I have added this test in tests/test_endpoints
You can check here:

master...matrixise:test_resources_many

    def test_nested_api(self, app, client):
        class Resource(marshmallow.Schema):
            name = marshmallow.fields.String()

        @app.route('/resources', methods=['GET'])
        class ResourcesHTTPEndpoint(HTTPEndpoint):
            async def get(self) -> Resource(many=True):
                return [{'name': 'Name'}]
        response = client.get('/resources')
        assert response.status_code == 200

but I get this exception because you are waiting for a class and not an instance, but in the case of Resource(many=True) it's an instance.

Do you know why?

tests/test_endpoints.py:92: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/requests/sessions.py:546: in get
    return self.request('GET', url, **kwargs)
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/starlette/testclient.py:382: in request
    json=json,
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/requests/sessions.py:533: in request
    resp = self.send(prep, **send_kwargs)
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/requests/sessions.py:646: in send
    r = adapter.send(request, **kwargs)
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/starlette/testclient.py:211: in send
    raise exc from None
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/starlette/testclient.py:208: in send
    loop.run_until_complete(connection(receive, send))
/usr/lib64/python3.7/asyncio/base_events.py:584: in run_until_complete
    return future.result()
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/starlette/middleware/errors.py:125: in asgi
    raise exc from None
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/starlette/middleware/errors.py:103: in asgi
    await asgi(receive, _send)
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/starlette/exceptions.py:74: in app
    raise exc from None
../../../../.virtualenvs/tempenv-713715370977/lib/python3.7/site-packages/starlette/exceptions.py:63: in app
    await instance(receive, sender)
starlette_api/endpoints.py:37: in __call__
    response = await self.dispatch(request, state, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <tests.test_endpoints.TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint object at 0x7f0193c775c0>, request = <starlette.requests.Request object at 0x7f0193c776a0>
state = {'app': <starlette_api.applications.Starlette object at 0x7f0193c52cf8>, 'exc': None, 'path_params': {}, 'receive': <function _ASGIAdapter.send.<locals>.receive at 0x7f0193ca7bf8>, ...}
kwargs = {}, handler_name = 'get'
handler = <bound method TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint.get of <tests.test_endpoints.TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint object at 0x7f0193c775c0>>
app = <starlette_api.applications.Starlette object at 0x7f0193c52cf8>
injected_func = functools.partial(<bound method TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint.get of <tests.test_endpoints.TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint object at 0x7f0193c775c0>>)
response = [{'name': 'Name'}], return_annotation = <Resource(many=True)>

    async def dispatch(self, request: Request, state: typing.Dict, **kwargs) -> Response:
        handler_name = "get" if request.method == "HEAD" else request.method.lower()
        handler = getattr(self, handler_name, self.method_not_allowed)
    
        app = state["app"]
        injected_func = await app.injector.inject(handler, state)
    
        if asyncio.iscoroutinefunction(handler):
            response = await injected_func()
        else:
            response = injected_func()
    
        return_annotation = inspect.signature(handler).return_annotation
>       if issubclass(return_annotation, marshmallow.Schema):
E       TypeError: issubclass() arg 1 must be a class

app        = <starlette_api.applications.Starlette object at 0x7f0193c52cf8>
handler    = <bound method TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint.get of <tests.test_endpoints.TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint object at 0x7f0193c775c0>>
handler_name = 'get'
injected_func = functools.partial(<bound method TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint.get of <tests.test_endpoints.TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint object at 0x7f0193c775c0>>)
kwargs     = {}
request    = <starlette.requests.Request object at 0x7f0193c776a0>
response   = [{'name': 'Name'}]
return_annotation = <Resource(many=True)>
self       = <tests.test_endpoints.TestCaseHTTPEndpoint.test_nested_api.<locals>.ResourcesHTTPEndpoint object at 0x7f0193c775c0>
state      = {'app': <starlette_api.applications.Starlette object at 0x7f0193c52cf8>, 'exc': None, 'path_params': {}, 'receive': <function _ASGIAdapter.send.<locals>.receive at 0x7f0193ca7bf8>, ...}

starlette_api/endpoints.py:54: TypeError

Thank you,

@matrixise
Copy link
Contributor Author

ok, found

@perdy
Copy link
Collaborator

perdy commented Mar 15, 2019

It seems that the wrapping of request -> response in endpoints is slightly different from the same process done in function views. I'll modify it and push a new version with these changes.

@perdy perdy closed this as completed in 6e7f514 Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants