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

False-positive deprecation warning for partial async handlers #3252

Closed
nicktimko opened this issue Sep 7, 2018 · 5 comments
Closed

False-positive deprecation warning for partial async handlers #3252

nicktimko opened this issue Sep 7, 2018 · 5 comments

Comments

@nicktimko
Copy link
Contributor

Long story short

Wrapping a coroutine function with a functools.partial causes a DeprecationWarning to trigger.

Steps to reproduce

Something like

def private_route(handler):
    return functools.partial(handler, private=True)

async def hello(request, private: bool):
    return web.Response(text='boo' if private else 'hello')

handler = private_route(hello)
app.router.add_route('get', '/hello', handler)  # DepWarning

causes a DeprecationWarning to be hit.

I think when called as handler(req) will immediately run through the partial and return the coroutine, which can then be awaited. It seems like the dep warning in this case is more of a false-positive. Maybe an expression like the ending one below could suppress it? If this is indeed just a false-positive and I'm the only complainer reporter, I could live.

import functools
import inspect

async def f():
    pass

fp = functools.partial(f)

inspect.iscoroutinefunction(f)  # True
inspect.iscoroutinefunction(fp) # False
isinstance(fp, functools.partial) and inspect.iscoroutinefunction(fp.func) # True

Tweak?:

         if asyncio.iscoroutinefunction(handler):
             pass
         elif inspect.isgeneratorfunction(handler):
             warnings.warn("Bare generators are deprecated, "
                           "use @coroutine wrapper", DeprecationWarning)
+        elif (isinstance(handler, functools.partial) and
+              asyncio.iscoroutinefunction(handler.func)):
+            pass
         elif (isinstance(handler, type) and
               issubclass(handler, AbstractView)):
             pass
         else:
             warnings.warn("Bare functions are deprecated, "
                           "use async ones", DeprecationWarning)

Your environment

I'm using aiohttp 3.3.2 on Python 3.6.5 on Ubuntu 16.04, but it looks like master would still trigger the warning.

@webknjaz
Copy link
Member

webknjaz commented Sep 8, 2018

Feel free to send a PR

@asvetlov
Copy link
Member

asvetlov commented Sep 8, 2018

The issue is essentially the same as #1993 (comment)
I doubt if introspection errors should be fixed on aiohttp level.

@nicktimko
Copy link
Contributor Author

I guess I'm curious if my read was correct though: when synchronous handler support is removed, would this synchronous coroutine-handler-generator still work fine?

@asvetlov
Copy link
Member

Are you asking for functools.partial?
No, partials registration will raise an exception

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants