From c878b26c7525c97936f4b52e0cf7c16f02d022ea Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Tue, 17 Oct 2017 16:49:51 -0700 Subject: [PATCH] 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 --- HACKING.rst | 4 +++- hacking/checks/mock_checks.py | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 14eaa609..073a0307 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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 ------------------- diff --git a/hacking/checks/mock_checks.py b/hacking/checks/mock_checks.py index caa6b826..51efc178 100644 --- a/hacking/checks/mock_checks.py +++ b/hacking/checks/mock_checks.py @@ -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__()