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

unittest: self is not passed with specced/autospecced PropertyMock #100291

Open
WilliamDEdwards opened this issue Dec 16, 2022 · 0 comments
Open
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@WilliamDEdwards
Copy link

WilliamDEdwards commented Dec 16, 2022

Bug report

self is not passed with specced/autospecced PropertyMock.

MRE

# test.py

import module
from unittest import mock

def test_a():
  def thing_side_effect(self):
    pass

  with mock.patch('module.class_.thing', new=mock.PropertyMock(side_effect=thing_side_effect, autospec=True)):
    module.class_().thing()

# module.py

class class_:
  @property
  def thing(self):
    return

The issue also occurs with:

  • spec instead of autospec: mock.patch('module.class.thing', new=mock.PropertyMock(side_effect=thing_side_effect, spec=class.thing))
  • spec and new_callable instead of new: mock.patch('module.class.thing', spec=mock.create_autospec(class.thing, side_effect=thing_side_effect, new_callable=mock.PropertyMock)

Result

TypeError: thing_side_effect() missing 1 required positional argument: 'self'

However, the same notation works for non-PropertyMocks:

# test.py

import module
from unittest import mock
import pytest

def test_a():
  def thing_side_effect(self):
    pass

  with mock.patch('module.class_.thing', side_effect=thing_side_effect, autospec=True):
    module.class_().thing()

# module.py

class class_:
  def thing(self):
    return

Your environment

  • CPython versions tested on: 3.10
  • Operating system and architecture: macOS
@WilliamDEdwards WilliamDEdwards added the type-bug An unexpected behavior, bug, or error label Dec 16, 2022
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants