Skip to content

Commit

Permalink
qcore: Add __prepare__ to some meta classes (#51)
Browse files Browse the repository at this point in the history
Classes passed to `six.with_metaclass()` are expected to have a
`__prepare__` method to pass `tox` checks, which is not defined by
default in PY2 and not used.

See also <benjaminp/six#252>.
  • Loading branch information
behnam authored and JelleZijlstra committed Aug 9, 2018
1 parent 203127b commit 5692d23
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ __pycache__/
.arcconfig
.tox/
.mypy_cache/
build/
6 changes: 6 additions & 0 deletions qcore/disallow_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ def __init__(self, cl_name, bases, namespace):
(cls, cl_name)
raise TypeError(message)
super(DisallowInheritance, self).__init__(cl_name, bases, namespace)

# Needed bcz of a six bug: https://github.com/benjaminp/six/issues/252
@classmethod
def __prepare__(cls, name, bases, **kwargs):
return {}

5 changes: 5 additions & 0 deletions qcore/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def _make_value(self, value):
member.__init__(value)
return member

# Needed bcz of a six bug: https://github.com/benjaminp/six/issues/252
@classmethod
def __prepare__(cls, name, bases, **kwargs):
return {}


class EnumBase(six.with_metaclass(EnumType)):
_name_to_member = {}
Expand Down
10 changes: 10 additions & 0 deletions qcore/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ def __repr__(self):
"""Gets the ``repr`` representation of this object."""
return '%s(%r)' % (self.__class__.__name__, self.__dict__)

# Needed bcz of a six bug: https://github.com/benjaminp/six/issues/252
@classmethod
def __prepare__(cls, name, bases, **kwargs):
return {}


class EnumBasedEventHubType(type):
"""Metaclass for enum-based event hubs.
Expand Down Expand Up @@ -373,6 +378,11 @@ def __init__(cls, what, bases=None, dict=None):
# similar instance members
delattr(cls, 'on_' + name)

# Needed bcz of a six bug: https://github.com/benjaminp/six/issues/252
@classmethod
def __prepare__(cls, name, bases, **kwargs):
return {}


class EnumBasedEventHub(six.with_metaclass(EnumBasedEventHubType, EventHub)):
__based_on__ = []
Expand Down

0 comments on commit 5692d23

Please sign in to comment.