Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:15:38+00:00 2026-06-15T00:15:38+00:00

This question is a follow-up to this brilliant answer on decorators in Python :

  • 0

This question is a follow-up to this brilliant answer on decorators in Python :

I use the given “snippet to make any decorator accept generically any argument”.

Then I have this (here simplified) decorator:

@decorator_with_args
def has_permission_from_kwarg(func, *args, **kwargs):
    """Decorator to check simple access/view rights by the kwarg."""
    def wrapper(*args_1, **kwargs_1):
        if 'kwarg' in kwargs_1:
            kwarg = kwargs_1['kwarg']
        else:
            raise HTTP403Error()

        return func(*args_1, **kwargs_1)

    return wrapper
  1. Working with this decorator, no problem it does the job very well.
  2. Testing a similar decorator that does not require absolutely the kwargs, same outcome.
  3. But testing this decorator with the following mock does not work:

    def test_can_access_kwarg(self):
        """Test simple permission decorator."""
        func = Mock(return_value='OK')
        decorated_func = has_permission_from_slug()(func(kwarg=self.kwarg))
        # It will raise at the following line, whereas the kwarg is provided...
        response = decorated_func()
        self.assertTrue(func.called)
        self.assertEqual(response, 'OK')
    

It returns me the exception I am raising when I do not have a ‘kwarg’ keyword-argument…

Does anyone has a clue how to test (by mocking would be preferable) such a decorator decorated by another decorator that requires the access to one of the keyword arguments passed to the function ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:15:39+00:00Added an answer on June 15, 2026 at 12:15 am
    decorated_func = has_permission_from_slug()(func(kwarg=self.kwarg))
    

    This will:

    1. Execute func(kwarg=self.kwarg)
    2. Generate an instance of the actual decorator.
    3. Call the decorator on the result of the func-call (i.e. the result).
    4. Return the wrapper which will then later try to call the results from step 3 (which would fail).

      response = decorated_func()

    This will then call the returned wrapper with no arguments, so **kwargs_1 is empty. Also, if you wrapper wouldn’t raise an exception in this case, the subsequent call of func(..) would throw an exception because the return value of func (the original one) is probably not callable (see above).

    What you probably want to do instead, or at least what your decorator supports, is this:

    decorated_func = has_permission_from_kwarg()(func)
    response = decorated_func(kwarg=self.kwarg)
    

    Or, if you want to pass your kwarg in the decorator like this:

    decorated_func = has_permission_from_kwarg(kwarg=self.kwarg)(func)
    response = decorated_func()
    

    Then you need to adjust or decorator to actually use kwargs, and not kwargs_1 in the check (the latter are the arguments to the decorated function).


    I’m testing your original decorator definition (with no changes) and the decorator_with_args as defined in the linked answer with the following code:

    class HTTP403Error (Exception):
        pass
    
    def func (*args, **kwargs):
        print('func {}; {}'.format(args, kwargs))
    
    my_kwarg = 'foo'
    decorated_func = has_permission_from_kwarg()(func)
    decorated_func(kwarg=my_kwarg)
    decorated_func(not_kwarg=my_kwarg)
    

    As expected, I get func (); {'kwarg': 'foo'} printed for the first call, and a HTTP403 exception for the second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is a follow-up on a question about Python variable scope . Additional
I got a follow up question to this question. I use Silverlight and WCF,
This question is a follow-up to the one at Can I use a language
This question is really a follow on to this question In python how can
This is a bit of a follow-on question, related to an answer I saw
This question is a follow on from this one ... I am binding to
This question is a follow up with a previous question Previous Question The previous
This is a follow-up question to this question I asked earlier. Btw thanks Neil
I have a follow up question to this question . Is it possible to
This question is kind of a follow up to this question I asked a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.