Closed steog88 closed 5 years ago
Thanks for the report. I guess I added it so that inspect can use __signature__
to give signature of the mock object. Can you reproduce it with normal objects or is this something you see with QObject only? Do you have the same error in Python 3?
Sorry, I read it again now that it's a non-writable property. One solution would be to have catch the AttributeError. I think this would be an upstream bug too.
@tirkarthi - would you prefer @steog88 to re-log this on https://bugs.python.org/ or would you be happy to do so and get a PR for a workaround/fix going there?
I have to admit, I'm confused as to why setting an attribute on a Mock-ish class is failing like this.
That said, _check_signature
has a pretty bad code smell now: it's called _check but sets stuff, it takes a func arg that is often not a func. @tirkarthi - would you be up for sorting that out upstream too?
@tirkarthi no problems with the builtin unittest.mock
in python3
.
It also works if I define
class SampleClass():
def __init__(self, txt):
self.txt = txt
@property
def __signature__(self):
return self.txt
Curious thing, even if SampleClass
does not inherit from QObject
like here, the test stops working if I import QObject
anywhere in the code. I have no idea why.
I am also confused but this is only reproducible with pyside2. type(mock) should actually be setting it to the mocked object but there is some code in NonCallableMock where __new__
that I wish to understand to see this has some metaclass
Opened upstream report : https://bugs.python.org/issue36848 Another report in pytest-qt : https://github.com/pytest-dev/pytest-qt/issues/258
Yes, I don't know how to write a pure python reproducer without pyside2. I just installed pyside2 and grepped for __signature__
. There is some code comment that it triggers signature initialization. I am not sure too :( I will update if I find anything.
$ rg -B 1 '__signature__' foo-venv/
foo-venv/lib/python3.8/site-packages/PySide2/__init__.py
22- # Trigger signature initialization.
23: type.__signature__
foo-venv/lib/python3.8/site-packages/shiboken2/__init__.py
29-# Trigger signature initialization.
30:type.__signature__
foo-venv/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/signature/lib/enum_sig.py
117- ret = self.result_type()
118: signature = getattr(func, '__signature__', None)
foo-venv/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/signature/layout.py
46-differently formatted versions of signatures. The default
47:layout is known from the "__signature__" attribute.
Closing this in favour of where in appears to need to be fixed: https://bugreports.qt.io/browse/PYSIDE-1004
@steog88 Just to add to this you can pass autospec=False
as a workaround to mock the class where this signature setting code is not executed and you can use the mocked class but with the tradeoff that the calls to mock are signature validated.
Thanks for the report.
Thanks, I will do as suggested then.
I found a problem with
mock>2.0.0
. When I want to patch a class withautospec=True
, if the class itself has already the attribute__signature__
and it is not writable, anAttributeError
is raised when running_check_signature
.In the real case, the object I want to patch inherits
__signature__
fromPySide2.QtWidgets.QObject
, so renaming the property is not a viable option. Although my code is much more complicate, I can reproduce the error with the following code (save totest_mock3.py
and run, I usepython
2.7.15,mock
3.0.5,unittest2
1.1.0 andPySide2
5.12.3):The error is the following:
The same code works with
mock==2.0.0
.