question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When combined with @property methods, `instance` argument does not seem to match the docs

See original GitHub issue

My understanding from the documentation is that for decorated instance methods, you never should have to extract the self arg, that it is always in instance, not args, and that wrapped is already bound to instance. However, this does not seem true when combined with the property decorator:

from pytest import fixture
from wrapt import decorator

@decorator
def wrapper(wrapped, instance, args, kwargs):
    return {'instance':instance, 'args':args, 'kwargs': kwargs, 'wrapped':wrapped(*args, **kwargs)}

class Decorated:
    @property
    @wrapper
    def prop(self, *args, **kwargs):
        return {'self': self, 'args': args, 'kwargs':kwargs}

@fixture
def instance():  return Decorated()

@fixture
def result(instance):  return instance.prop

def test_instance_is_sent_to_wrapped(instance, result): # passes
    assert result['wrapped']['self'] is instance

def test_instance_arg_is_correct(instance, result): # fails
    assert result['instance'] is instance

def test_instance_is_not_i_args(instance, result): # fails
    assert instance not in result['args']

(running on python 3.7.8, wrapt 1.12.1, pytest 5.3.2)

This doesn’t seem intentional, and I suspect is @property pulling some shenanigans. If these test don’t use @property, and are modified to use a method, they pass. Also, if you grab the property descriptor, calling descriptor.fget requires an instance arg, it isn’t bound.

Can this issue be fixed?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
GrahamDumpletoncommented, Aug 5, 2021

Old issue related to this with more thorough description of how to create a wrapper object for properties can be found at:

0reactions
cableraycommented, Jul 22, 2021

Here’s an example of my use case:

def with_data_item(data_key):
  @decorator
  def wrapper(wrapped, instance, args, kwargs):
    def adapter(*_args, **_kwargs):  # is this adapter needed? I am removing an arg from the decorated signature 
      data_value = instance.get(data_key)
      return wrapped(data_value, *_args, **_kwargs)
    return adapter(*args, **kwargs)
  return wrapper


class DataAdapter:
  def __init__(self, data_source):
    self.data_source = data_source

  def get(self, data_key):
    return self.data_item[data_key]

  @property
  @with_data_item('foo')
  def foo(self, data_item):
    return datai_tem+1

  @with_data_item('bar')
  def calculate_bar(self, data_item):
    do_something_slow_with(data_item)

It’s similar to functools.partial_method, but I need more flexibility.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property Functions - MSBuild - Microsoft Learn
Learn how to use property functions, which are calls to .NET Framework methods that appear in MSBuild property definitions.
Read more >
Classes - Object-Oriented Programming in Python
If we call the class method from an instance, this parameter will contain the instance object, but if we call it from the...
Read more >
functools — Higher-order functions and operations on callable ...
When func is a non-descriptor callable, an appropriate bound method is created dynamically. This behaves like a normal Python function when used as...
Read more >
1.7.1 - jqwik User Guide
Since Gradle does not yet support JUnit platform reporting – see this Github ... At test runtime the exact parameter values of the...
Read more >
Error.prototype.stack - JavaScript - MDN Web Docs - Mozilla
The non-standard stack property of an Error instance offers a trace of which ... MSDN docs also seem to match the PhantomJS implementation....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found