autospec check: Allow 'wraps' to be an alternative to autospec

Don't cause an H210 error if the mock.patch/mock.patch.object call uses
the 'wraps' keyword. As that serves the same purpose in catching wrong
attributes.

In testing, if using 'autospec=True' with 'wraps', then the object
specified by 'wraps' does not get called.

Change-Id: I29a4df15c2511bd12655c09edcae8530cd5ce0d3
This commit is contained in:
John L. Villalovos 2017-10-17 16:49:51 -07:00
parent 59c9c41394
commit c878b26c75
2 changed files with 8 additions and 2 deletions

View File

@ -339,7 +339,9 @@ exception possible should be used.
not force them to use ``autospec=True``, but requires that they define some
value for ``autospec``, ``spec``, or ``spec_set``. It could be
``autospec=False``. We just want them to make a conscious decision on using
or not using ``autospec``.
or not using ``autospec``. If any of the following are used then ``autospec``
will not be required: ``new``, ``new_callable``, ``spec``, ``spec_set``,
``wraps``
OpenStack Trademark
-------------------

View File

@ -29,6 +29,7 @@ class MockAutospecCheck(object):
Okay: mock.patch('target_module_1', defined_mock)
Okay: mock.patch('target_module_1', spec=1000)
Okay: mock.patch('target_module_1', spec_set=['data'])
Okay: mock.patch('target_module_1', wraps=some_obj)
H210: mock.patch('target_module_1')
Okay: mock.patch('target_module_1') # noqa
@ -43,6 +44,8 @@ class MockAutospecCheck(object):
Okay: mock.patch.object('target_module_2', 'attribute', new_callable=AFunc)
Okay: mock.patch.object('target_module_2', 'attribute', spec=3)
Okay: mock.patch.object('target_module_2', 'attribute', spec_set=[3])
Okay: mock.patch.object('target_module_2', 'attribute', wraps=some_obj)
H210: mock.patch.object('target_module_2', 'attribute', somearg=2)
H210: mock.patch.object('target_module_2', 'attribute')
@ -67,7 +70,8 @@ class MockCheckVisitor(ast.NodeVisitor):
# Patchers we are looking for and minimum number of 'args' without
# 'autospec' to not be flagged
patchers = {'mock.patch': 2, 'mock.patch.object': 3}
spec_keywords = {"autospec", "new", "new_callable", "spec", "spec_set"}
spec_keywords = {"autospec", "new", "new_callable", "spec", "spec_set",
"wraps"}
def __init__(self, filename):
super(MockCheckVisitor, self).__init__()