From 9e6c9240404e9db8269538c2dc7b8f5f231eb15d Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Fri, 8 Jul 2016 12:45:30 +1000 Subject: 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 --- oslo_context/context.py | 3 +++ oslo_context/tests/test_context.py | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) (limited to 'oslo_context') diff --git a/oslo_context/context.py b/oslo_context/context.py index 0238533..d3da603 100644 --- a/oslo_context/context.py +++ b/oslo_context/context.py @@ -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, diff --git a/oslo_context/tests/test_context.py b/oslo_context/tests/test_context.py index e50b99c..559562b 100644 --- a/oslo_context/tests/test_context.py +++ b/oslo_context/tests/test_context.py @@ -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)) -- cgit v1.2.1