Emit deprecation warnings when positional args passed

We should try and enforce that all arguments passed to an oslo_context
are passed as keyword arguments. positional is a library maintained by
the openstack community for exactly this.

We can't simply switch over to raising an error, so by setting
enforcement to warn we will issue a deprecation warning whenever
arguments are being passed positionally.

Change-Id: I47456ac65911d4cc4a5acbacfd1d0dae8429684a
This commit is contained in:
Jamie Lennox
2016-07-08 12:45:30 +10:00
parent 9feb9db8d6
commit 9e6c924040
4 changed files with 31 additions and 0 deletions

View File

@@ -31,6 +31,8 @@ import itertools
import threading
import uuid
from positional import positional
_request_store = threading.local()
@@ -68,6 +70,7 @@ class RequestContext(object):
user_idt_format = u'{user} {tenant} {domain} {user_domain} {p_domain}'
@positional(enforcement=positional.WARN)
def __init__(self, auth_token=None, user=None, tenant=None, domain=None,
user_domain=None, project_domain=None, is_admin=False,
read_only=False, show_deleted=False, request_id=None,

View File

@@ -14,8 +14,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import fixtures
import hashlib
import uuid
import warnings
from oslotest import base as test_base
@@ -27,6 +29,21 @@ def generate_id(name):
return hashlib.md5(name.encode('utf-8')).hexdigest()
class WarningsFixture(fixtures.Fixture):
def __init__(self, action="always", category=DeprecationWarning):
super(WarningsFixture, self).__init__()
self.action = action
self.category = category
def setUp(self):
super(WarningsFixture, self).setUp()
self._w = warnings.catch_warnings(record=True)
self.log = self._w.__enter__()
self.addCleanup(self._w.__exit__)
warnings.simplefilter(self.action, self.category)
class Object(object):
pass
@@ -35,6 +52,7 @@ class ContextTest(test_base.BaseTestCase):
def setUp(self):
super(ContextTest, self).setUp()
self.warnings = self.useFixture(WarningsFixture())
self.useFixture(fixture.ClearRequestContext())
def test_context(self):
@@ -439,3 +457,10 @@ class ContextTest(test_base.BaseTestCase):
'roles': roles,
'is_admin_project': False},
ctx.to_policy_values())
def test_positional_args(self):
context.RequestContext('abc', 'def')
self.assertEqual(1, len(self.warnings.log))
self.assertIn('__init__ takes at most 1 positional',
str(self.warnings.log[0].message))

View File

@@ -3,3 +3,5 @@
# process, which may cause wedges in the gate later.
pbr>=1.6 # Apache-2.0
positional>=1.0.1 # Apache-2.0

View File

@@ -2,6 +2,7 @@
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
fixtures>=3.0.0 # Apache-2.0/BSD
hacking<0.11,>=0.10.0
oslotest>=1.10.0 # Apache-2.0
coverage>=3.6 # Apache-2.0