0

I have a base class like

class BaseTest:
  @setup_test(param='foo')
  def test_something():
    do stuff

I now want to override the param to the decorator

class NewTest:
  @setup_test(param='different value')
  def test_something():
    super().test_something()

The trouble is when I call super().test_something() it will call BaseTest.test_something wrapped with @setup_test(param='foo') which does some bootstrapping that will overwrite what was done in @setup_test(param='different value').

I need to directly call the undecorated BaseTest.test_something

wonton
  • 7,568
  • 9
  • 56
  • 93
  • By itself, decorating a function literally replaces it with another object in the enclosing namespace. After passing `test_something` to `setup_test` the former is replaced by _whatever_ the decorator returns. _Usually_ a decorator returns another function that is at least similar to the one passed into it. And _usually_ (though less common) the decorator uses some mechanism to attach a reference to the original function as an attribute of the object it returns. (This is what the [`functools.wraps`](https://docs.python.org/3/library/functools.html#functools.wraps) decorator does for example.) – Daniil Fajnberg Feb 07 '23 at 09:12
  • Does this answer your question? [How to strip decorators from a function in Python](https://stackoverflow.com/questions/1166118/how-to-strip-decorators-from-a-function-in-python) – Daniil Fajnberg Feb 07 '23 at 09:21

1 Answers1

1

The solution I came up with is to use the __wrapped__ attribute on BaseTest.test_something

class NewTest:
  @setup_test(param='different value')
  def test_something():
    super().test_something.__wrapped__(self)

This bypasses @setup_test(param='foo')

wonton
  • 7,568
  • 9
  • 56
  • 93
  • This solution only works, if the decorator adds a corresponding `__wrapped__` attribute. You did not specify in your question that the decorators were using the `functools.update_wrapper` function. The `__wrapped__` attribute is not some built-in Python magic. It is just an attribute that that particular `functools` function attaches to the output. – Daniil Fajnberg Feb 07 '23 at 09:13
  • this is true, although best practice these days is to use @wraps when writing a decorator. If you didn't do that then this solution would not work – wonton Feb 07 '23 at 21:11