Skip to content

Commit

Permalink
Ensure that the incoming 'new_class' is actually a class
Browse files Browse the repository at this point in the history
Change-Id: If961874873a3407d3f5293dc5c5f38ae43abf190
  • Loading branch information
Joshua Harlow committed May 5, 2015
1 parent 719ea22 commit aa2e000
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
8 changes: 8 additions & 0 deletions debtcollector/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def deprecation(message, stacklevel=None, category=None):
warnings.warn(message, category=category, stacklevel=stacklevel)


def get_qualified_name(obj):
# Prefer the py3.x name (if we can get at it...)
try:
return (True, obj.__qualname__)
except AttributeError:
return (False, obj.__name__)


def generate_message(prefix, postfix=None, message=None,
version=None, removal_version=None):
"""Helper to generate a common message 'style' for deprecation helpers."""
Expand Down
16 changes: 8 additions & 8 deletions debtcollector/moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import inspect

from oslo_utils import reflection
import six

Expand All @@ -30,14 +32,7 @@ def _moved_decorator(kind, new_attribute_name, message=None,
"""Decorates a method/property that was moved to another location."""

def decorator(f):
try:
# Prefer the py3.x name (if we can get at it...)
old_attribute_name = f.__qualname__
fully_qualified = True
except AttributeError:
old_attribute_name = f.__name__
fully_qualified = False

fully_qualified, old_attribute_name = _utils.get_qualified_name(f)
if attr_postfix:
old_attribute_name += attr_postfix

Expand Down Expand Up @@ -95,6 +90,11 @@ def moved_class(new_class, old_class_name, old_module_name,
improved location for the old class now is.
"""

if not inspect.isclass(new_class):
_qual, type_name = _utils.get_qualified_name(type(new_class))
raise TypeError("Unexpected class type '%s' (expected"
" class type only)" % type_name)

old_name = ".".join((old_module_name, old_class_name))
new_name = reflection.get_class_name(new_class)
prefix = _CLASS_MOVED_PREFIX_TPL % (old_name, new_name)
Expand Down
14 changes: 3 additions & 11 deletions debtcollector/removals.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,8 @@
from debtcollector import _utils


def _get_qualified_name(obj):
# Prefer the py3.x name (if we can get at it...)
try:
return (True, obj.__qualname__)
except AttributeError:
return (False, obj.__name__)


def _get_module_name(mod):
return _get_qualified_name(mod)[1]
return _utils.get_qualified_name(mod)[1]


def remove(f=None, message=None, version=None, removal_version=None,
Expand All @@ -57,7 +49,7 @@ def remove(f=None, message=None, version=None, removal_version=None,

@wrapt.decorator
def wrapper(f, instance, args, kwargs):
qualified, f_name = _get_qualified_name(f)
qualified, f_name = _utils.get_qualified_name(f)
if qualified:
if inspect.isclass(f):
prefix_pre = "Using class"
Expand Down Expand Up @@ -157,7 +149,7 @@ def removed_module(module, replacement=None, message=None,
elif isinstance(module, six.string_types):
module_name = module
else:
_qual, type_name = _get_qualified_name(type(module))
_qual, type_name = _utils.get_qualified_name(type(module))
raise TypeError("Unexpected module type '%s' (expected string or"
" module type only)" % type_name)
prefix = "The '%s' module usage is deprecated" % module_name
Expand Down
3 changes: 3 additions & 0 deletions debtcollector/tests/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ def green_giant():


class MovedInheritableClassTest(test_base.TestCase):
def test_broken_type_class(self):
self.assertRaises(TypeError, moves.moved_class, 'b', __name__)

def test_basics(self):
old = OldHotness()
self.assertIsInstance(old, NewHotness)
Expand Down

0 comments on commit aa2e000

Please sign in to comment.