classification
Title: autospec fails with AttributeError when mocked class has __signature__ non-writeable
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, mariocj89, michael.foord, xtreak
Priority: normal Keywords: 3.7regression

Created on 2019-05-08 06:57 by xtreak, last changed 2019-06-06 08:30 by xtreak. This issue is now closed.

Messages (9)
msg341855 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-05-08 06:57
With issue17185 __signature__ was set so that inspect can use this to return signature for mock objects. But in PySide2 it has __signature__ set as a non-writeable property and hence trying to set __signature__ fails. It's actually setting __signature__ for mocked object but still has this error for PySide.

mock backport report : https://github.com/testing-cabal/mock/issues/464
pytest-qt report : https://github.com/pytest-dev/pytest-qt/issues/258
msg341869 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-05-08 12:22
As noted in https://github.com/testing-cabal/mock/issues/464#issuecomment-490381389 just importing PySide2 makes __signature__ not writeable for any class defined after the import.

$ python
Python 3.8.0a4+ (heads/master:b1c3167c23, May  8 2019, 05:17:38)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo: pass
...
>>> Foo.__signature__ = 1
>>> import PySide2
>>> class Bar: pass
...
>>> Bar.__signature__ = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: attribute '__signature__' of 'type' objects is not writable
msg341871 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2019-05-08 13:09
Wow, is this just an issue that the pyside guys need to fix?
msg341874 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-05-08 14:16
> Wow, is this just an issue that the pyside guys need to fix?

I guess so. PySide __init__.py has "import shiboken" which also does type.__signature__. It seems like shiboken is some kind of generator to generate Python bindings from C++ QT code. I couldn't dig any deeper. Since this has caused two reports of regression I can add an except block to silence the AttributeError and add a comment to this issue but can only test it manually installing pyside to ensure the AttributeError is not thrown.
msg341875 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2019-05-08 14:19
I'm not sure we should try and work around this; hijacking __signature__ python-wide is going to cause a bunch of other problems. My vote would be to just open a bug on the pyside tracker (wherever that is) and close the issue and and on the backport by pointing to that issue.

thoughts?
msg341877 - (view) Author: Mario Corchero (mariocj89) * (Python triager) Date: 2019-05-08 14:32
Agree, for the general case I think it makes sense that this is fixed in pyside.
Hijacking __signature__ python-wide sounds like it can cause other issues, if they need such a hack I would suggest finding a way (in pyside) to allow for mock to work.

I wonder though if this change in __signature__ will affect in any way any C extension that might not allow setting __signature__ if not set as a lack of __dict__ (I have not managed to find any issue with small examples though).
msg341878 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2019-05-08 14:37
We only attempt to set __signature__ on Mocks, so I don't think extensions will be a problem.

I do think there's a bit of code smell on that method: it's called _check_* and then changes some attributes, that might be worth fixing?
msg341880 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-05-08 14:45
Okay, I opened a bug in pyside tracker : https://bugreports.qt.io/browse/PYSIDE-1004
msg344792 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-06-06 08:30
The jira issue in pyside2 is closed as fixed. I am closing this as third party.
History
Date User Action Args
2019-06-06 08:30:19xtreaksetstatus: open -> closed
resolution: third party
messages: + msg344792

stage: resolved
2019-05-08 14:45:19xtreaksetmessages: + msg341880
2019-05-08 14:37:19cjw296setmessages: + msg341878
2019-05-08 14:32:27mariocj89setmessages: + msg341877
2019-05-08 14:19:41cjw296setmessages: + msg341875
2019-05-08 14:16:21xtreaksetmessages: + msg341874
2019-05-08 13:09:13cjw296setmessages: + msg341871
2019-05-08 12:22:35xtreaksetmessages: + msg341869
2019-05-08 06:57:13xtreakcreate