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

Class-based method stub cannot execute #41

Open
jonathan-benn opened this issue Jan 9, 2018 · 2 comments
Open

Class-based method stub cannot execute #41

jonathan-benn opened this issue Jan 9, 2018 · 2 comments

Comments

@jonathan-benn
Copy link

class A:
	def B(self):
		return 5
stub = sinon.stub(A, 'B')
stub() # error
@note35
Copy link
Owner

note35 commented Jan 11, 2018

In python3, this code works.

In python2, it will cause the error below.

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    print(stub()) # error
  File "/Users/kir/python-dev/sinon/sinon/lib/base.py", line 135, in __call__
    return self._get_wrapper()(*args, **kwargs)
TypeError: unbound method wrapped() must be called with A instance as first argument (got nothing instead)

@jonathan-benn
Copy link
Author

I suspect that this bug may have existed for a long time, since it's kind of the opposite of the issue that required the dirty hack in the first place.

The problem is that when trying to directly execute a class method (a class, not an object), Python is expecting the self argument as the first parameter to the function call.

Hence, the following code passes:

class A:
	def B(self):
		return 5
stub = sinon.stub(A, 'B')
stub(A()) # success!

This is because, as a language feature of Python, if you're calling the function B directly without an object then you must supply an instance. e.g.

o = A()
A.B(o) # returns 5

I'm not sure what you think the right thing to do would be in this case? One could consider the current behaviour of Sinon.PY to be correct. However, I do find it counter-intuitive (confusing) to need to pass an object instance into the stub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants