flask-allows icon indicating copy to clipboard operation
flask-allows copied to clipboard

Allows.fulfill always passes request as an arg

Open cerealcable opened this issue 6 years ago • 9 comments

My tests are enjoying a few logs, which was rather confusing since I have never passed request in so...sure enough, Allows.fulfill injects it and causes a DeprecationWarning.

/usr/local/lib/python3.6/site-packages/flask_allows/allows.py:156: DeprecationWarning: Passing request to requirements is now deprecated and will be removed in 1.0                                                                           
  _call_requirement(r, identity, request) for r in all_requirements

My class based Requirements is:

class HasPermission(Requirement):
    def __init__(self, permission):
        self.permission = permission

    def fulfill(self, user):
        return user.has_permission(self.permission)

cerealcable avatar Sep 15 '18 19:09 cerealcable

Can you share the context it's being called in?

Almost all of allows tests use the new user only style requirements

justanr avatar Sep 16 '18 01:09 justanr

I haven't had a chance to really get a minimal test going on yet, but maybe this is enough to start with. I should be able to get a single-file example in the future but, I'm just doing

@allows.requires(HasPermission("random_persmission"))
def my_view():
    pass

cerealcable avatar Sep 16 '18 23:09 cerealcable

I'll try to reproduce when I get home tonight. Which version of allows and flask are you using?

justanr avatar Sep 17 '18 15:09 justanr

flask-allows==0.7.0
Flask==0.12.2
flask-restplus==0.11.0
Werkzeug==0.13

That's effectively the stack I'm running with. I'll see if I can reproduce it really quick.

cerealcable avatar Sep 21 '18 20:09 cerealcable

So far I couldn't reproduce it, the context I'm seeing in this warning is making it rather difficult to track down what is causing it however. Not seeing where the execution path for the cause of the DeprecationWarning is making it rather hard to even know exactly where to look.

cerealcable avatar Sep 21 '18 20:09 cerealcable

I can't reproduce this either. If you're still seeing this, you can set PYTHONWARNINGS=error::DeprecationWarning in your environment and that will transform deprecation warnings into exceptions. That's not a great fix as you might encounter other deprecated behavior on the execution path to this and the flask debugger doesn't seem to pick up the initial exception that triggers this (the type error triggered by passing (self, user) to a method that expects (self, user, request))

There are a few things that I can do to make this issue easier to debug:

  1. Include the requirement repr that triggered this message in the message
  2. Use a subclass of deprecation warning so turning them into errors is a more focused target (e.g. you'd PYTHONWARNINGS=error::AllowsDeprecation instead of the above).

justanr avatar Sep 22 '18 16:09 justanr

~~So I'm only seeing this in pytest, and when I run with PYTHONWARNINGS=error::AllowsDeprecation they disappear instead of raising exceptions like I was expecting which doesn't make a whole lot of sense. I've yet to see these messages outside of pytest however which is interesting.~~

And...now I've read your # 2 fully, a subclass of deprecation warning would be so helpful in finding this needle! I know I'd greatly appreciate it.

cerealcable avatar Sep 25 '18 06:09 cerealcable

Some more investigation, and my implementation was fixed by adding my own __call__ method. Requirement.__call__ still requires request as an arg, thus if you inherit from Requirement you should be seeing this error anytime we attempt to call req(user). Maybe I'm misunderstanding the docs?

class HasPermission(Requirement):
    def __init__(self, permission):
        self.permission = permission

    def __call__(self, user):
        return self.fulfill(user)

    def fulfill(self, user):
        return user.has_permission(self.permission)

cerealcable avatar Sep 25 '18 07:09 cerealcable

Just released 0.7.1 with the updated warnings support. Hopefully this helps track down your issue. Let me know what you find and I can help track down further problems with it.

justanr avatar Oct 04 '18 01:10 justanr