From 6753274b2811dbed558753a85b96038d3128fd7b Mon Sep 17 00:00:00 2001 From: Sun Austin Date: Mon, 15 Oct 2018 16:08:04 +0800 Subject: [PATCH] Fix: "metaclass" issues for Python 2/3 compatible code. remove "__metaclass__ = abc.ABCMeta" and adding decorator "@six.add_metaclass(abc.ABCMeta)" on class and removing pep8 ignore case Story: 2003430 Task: 27476 Change-Id: If60b8d5662d9a87b217a9b1d48a2c3e9b037884b Signed-off-by: Sun Austin --- service-mgmt-api/sm-api/sm_api/db/api.py | 4 ++-- service-mgmt-api/sm-api/sm_api/objects/base.py | 3 ++- service-mgmt-api/sm-api/sm_api/openstack/common/policy.py | 6 ++---- .../sm-api/sm_api/openstack/common/rpc/serializer.py | 3 ++- tox.ini | 6 ++---- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/service-mgmt-api/sm-api/sm_api/db/api.py b/service-mgmt-api/sm-api/sm_api/db/api.py index 035114db..7aa3d4ba 100644 --- a/service-mgmt-api/sm-api/sm_api/db/api.py +++ b/service-mgmt-api/sm-api/sm_api/db/api.py @@ -24,6 +24,7 @@ Base classes for storage engines """ import abc +import six from sm_api.openstack.common.db import api as db_api @@ -36,11 +37,10 @@ def get_instance(): return IMPL +@six.add_metaclass(abc.ABCMeta) class Connection(object): """Base class for storage system connections.""" - __metaclass__ = abc.ABCMeta - @abc.abstractmethod def __init__(self): """Constructor.""" diff --git a/service-mgmt-api/sm-api/sm_api/objects/base.py b/service-mgmt-api/sm-api/sm_api/objects/base.py index 0137b56f..9f297aaf 100644 --- a/service-mgmt-api/sm-api/sm_api/objects/base.py +++ b/service-mgmt-api/sm-api/sm_api/objects/base.py @@ -19,6 +19,7 @@ """Sm common internal object model""" import collections +import six from sm_api.common import exception from sm_api.objects import utils as obj_utils @@ -154,6 +155,7 @@ def check_object_version(server, client): dict(client=client_minor, server=server_minor)) +@six.add_metaclass(Sm_apiObjectMetaclass) class Sm_apiObject(object): """Base class and object factory. @@ -163,7 +165,6 @@ class Sm_apiObject(object): necessary "get" classmethod routines as well as "save" object methods as appropriate. """ - __metaclass__ = Sm_apiObjectMetaclass # Version of this object (see rules above check_object_version()) version = '1.0' diff --git a/service-mgmt-api/sm-api/sm_api/openstack/common/policy.py b/service-mgmt-api/sm-api/sm_api/openstack/common/policy.py index 1f875afd..42330654 100644 --- a/service-mgmt-api/sm-api/sm_api/openstack/common/policy.py +++ b/service-mgmt-api/sm-api/sm_api/openstack/common/policy.py @@ -189,13 +189,12 @@ def check(rule, target, creds, exc=None, *args, **kwargs): return result +@six.add_metaclass(abc.ABCMeta) class BaseCheck(object): """ Abstract base class for Check classes. """ - __metaclass__ = abc.ABCMeta - @abc.abstractmethod def __str__(self): """ @@ -549,6 +548,7 @@ def reducer(*tokens): return decorator +@six.add_metaclass(ParseStateMeta) class ParseState(object): """ Implement the core of parsing the policy language. Uses a greedy @@ -561,8 +561,6 @@ class ParseState(object): shouldn't be that big a problem. """ - __metaclass__ = ParseStateMeta - def __init__(self): """Initialize the ParseState.""" diff --git a/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/serializer.py b/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/serializer.py index 3f23de2c..e133f80c 100644 --- a/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/serializer.py +++ b/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/serializer.py @@ -19,11 +19,12 @@ """Provides the definition of an RPC serialization handler""" import abc +import six +@six.add_metaclass(abc.ABCMeta) class Serializer(object): """Generic (de-)serialization definition base class""" - __metaclass__ = abc.ABCMeta @abc.abstractmethod def serialize_entity(self, context, entity): diff --git a/tox.ini b/tox.ini index d08ea660..4ae299d1 100644 --- a/tox.ini +++ b/tox.ini @@ -85,7 +85,6 @@ commands = # H105: Don't use author tags # H201: no 'except:' # H233: Python 3.x incompatible use of print operator -# H236: Python 3.x incompatible __metaclass__, use six.add_metaclass() # H306: imports not in alphabetical order # H401: docstring should not start with a space # H403: multi line docstrings should end on a new line @@ -103,14 +102,13 @@ commands = # B006 Do not use mutable data structures for argument defaults. (python3) # B007 Loop control variable 'key' not used within the loop body. If this is intended, start the name with an underscore. # B008 Do not perform calls in argument defaults. The call is performed only once at function definition time. -# B303 `__metaclass__` does nothing on Python 3. For Python 2 compatibility, use `six.add_metaclass`. # B305 `.next()` is not a thing on Python 3. Use the `next()` builtin. For Python 2 compatibility, use `six.next()`. # B306 `BaseException.message` has been deprecated as of Python 2.6 and is removed in Python 3. ignore= E114,E116,E121,E122,E123,E124,E125,E126,E127,E128,E129,E131,E201,E228,E231,E241,E251,E265,E271, E302,E303,E305,E402,E713,E714,E722,E731,E999, - H102,H104,H105,H106,H201,H233,H236,H306,H401,H403,H404,H405,H501, + H102,H104,H105,H106,H201,H233,H306,H401,H403,H404,H405,H501, F401,F811,F821,F841, - B001,B004,B006,B007,B008,B303,B305,B306 + B001,B004,B006,B007,B008,B305,B306 # Enable checks which are off by default # H106 Don’t put vim configuration in source files (off by default). SHOULD BE ENABLED. # H203 Use assertIs(Not)None to check for None (off by default).