From d2b452d7f5e03970a5341d84066f5ce0d1997f8e Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Fri, 12 Jul 2019 14:27:41 +0100 Subject: [PATCH] Simplify placement.microversion:_fully_qualified_name _fully_qualified_name was copied from Twisted. The original version does more than Placement needs. We only need to handle module-level classes and functions (and really only functions). No nested of those, nor object methods in any form. This is because we only use the method for finding the right microversioned handler of a method with the same name and placement HTTP handlers are universally functions. The previous version of the method had code that was never called. Here, that dead code is removed and the remainder is simplified. If it is called incorrectly there will be an (intentionally) untrapped TypeError during running of the tests. N.B.: This was discovered while reviewing coverage data. Change-Id: Id8f96801116818dda6c68c301619aee185e8fbff --- placement/microversion.py | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/placement/microversion.py b/placement/microversion.py index ff16cd428..88718d232 100644 --- a/placement/microversion.py +++ b/placement/microversion.py @@ -99,33 +99,19 @@ def min_version_string(): return VERSIONS[0] -# From twisted +# Based on code in twisted # https://github.com/twisted/twisted/blob/trunk/twisted/python/deprecate.py -def _fully_qualified_name(obj): - """Return the fully qualified name of a module, class, method or function. - - Classes and functions need to be module level ones to be correctly - qualified. +def _fully_qualified_name(handler): + """Return the name of a function or class used as an HTTP API handler, + qualified by module name. """ - try: - name = obj.__qualname__ - except AttributeError: - name = obj.__name__ + if inspect.isfunction(handler) or inspect.isclass(handler): + module_name = handler.__module__ + return "%s.%s" % (module_name, handler.__name__) - if inspect.isclass(obj) or inspect.isfunction(obj): - moduleName = obj.__module__ - return "%s.%s" % (moduleName, name) - elif inspect.ismethod(obj): - try: - cls = obj.im_class - except AttributeError: - # Python 3 eliminates im_class, substitutes __module__ and - # __qualname__ to provide similar information. - return "%s.%s" % (obj.__module__, obj.__qualname__) - else: - className = _fully_qualified_name(cls) - return "%s.%s" % (className, name) - return name + # We got an object method or module. This is a coding error. + raise TypeError("_fully_qualified_name received bad handler type. " + "Module-level class or function required.") def _find_method(f, version, status_code):