-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-40864: Fetch instance variables for spec #22572
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
Conversation
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). CLA MissingOur records indicate the following people have not signed the CLA: For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
|
This PR is stale because it has been open for 30 days with no activity. |
|
@cjw296 this should be good to review I believe. Please let me know if I missed anything. I think this feature would improve dev UX when using spec_set tremendously as users will not need to work around missing attributes that should be present in mocks. This does not set the spec of the attributes as they are found though (they will end up just being regular mocks). Figure that is a good enough chunk of work to be it's own PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your example works but running the testmock produces:
.....................................................................F.F...................................................................
FAIL: test_finds_attr_set_in__init__ (main.MockTest)
Traceback (most recent call last):
File "/home/me/Documents/cpython/Lib/unittest/test/testmock/testmock.py", line 908, in test_finds_attr_set_in__init__
self.assertTrue(hasattr(mock, 'z'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: False is not true
======================================================================
FAIL: test_finds_underscore_attr_set_in__init__ (main.MockTest)
Traceback (most recent call last):
File "/home/me/Documents/cpython/Lib/unittest/test/testmock/testmock.py", line 919, in test_finds_underscore_attr_set_in__init__
self.assertTrue(hasattr(mock, '_z'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: False is not true
Ran 139 tests in 0.402s
FAILED (failures=2)
Ran with checkout of this branch. Could you fix please?
|
Hey @MaxwellDupre, I just ran this after updating the branch to be in sync with remote and then rebuilding the python executable, things all passed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see my error: I had not done an make altinstall, sorry.
All tests passed.
|
Hello, is there any update on this PR? Anything I need to do? I would love to get this in as this is an issue that hits me pretty regularly and forces me to use non spec'd mocks. cc @cjw296 |
|
@tirkarthi - what're your thoughts on this? |
|
https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock
The docs state that the import dataclasses
from unittest.mock import Mock
class Person:
def __init__(self, name):
self.name = name
m_instance = Mock(spec=Person("Jim"))
print(m_instance.name)
m_class = Mock(spec=Person)
print(m_class.name)Current behavior With patch |
|
See also https://bugs.python.org/issue36580 |
| spec = [] | ||
| for member_k, member_v in _members: | ||
| spec.append(member_k) | ||
| if member_k == '__init__' and hasattr(member_v, '__code__'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not experienced with code object. This also seems to end up including the functions and methods as part of __init__ as per breakpoint here below where startswith is represented as a valid attribute. In case you are trying to get signature of the __init__ you can use inspect module : https://docs.python.org/3/library/inspect.html#introspecting-callables-with-the-signature-object . But not all the methods in signature become attributes as they could be flags to set another set of attributes dynamically too.
class Person:
def __init__(self, name):
self.name = name
foo = 1
if name.startswith("Test"):
print("Test user")
m_instance = Mock(spec=Person("John"))
print(m_instance.name)
print(m_instance.startswith)./python /tmp/foo.py
> /home/karthikeyan/stuff/python/cpython/Lib/unittest/mock.py(522)_mock_add_spec()
-> spec.extend(member_v.__code__.co_names)
(Pdb) member_v.__code__.co_names
('name', 'startswith', 'print')
(Pdb) c
<Mock name='mock.name' id='140293864592656'>
<Mock name='mock.startswith' id='140293862696912'>
|
Thanks for the feedback. Those are good points. When I have some free time I will see if I can find a more surgical solution and add in some test cases for the examples you folks gave. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #22572 (comment).
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
Any updates on this? I really like this change. |
My main driver for this change is I wanted the security of
spec_setwith the knowledge of the attributes created in init.https://bugs.python.org/issue40864