Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define __prepare__() in with_metaclass() #178

Merged
merged 2 commits into from
Sep 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ Peter Ruibal
Miroslav Shubernetskiy
Anthony Sottile
Lucas Wiman
Jordan Moldow

If you think you belong on this list, please let me know! --Benjamin
4 changes: 4 additions & 0 deletions six.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,10 @@ class metaclass(meta):

def __new__(cls, name, this_bases, d):
return meta(name, bases, d)

@classmethod
def __prepare__(cls, name, this_bases):
return meta.__prepare__(name, bases)
return type.__new__(metaclass, 'temporary_class', (), {})


Expand Down
28 changes: 28 additions & 0 deletions test_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,34 @@ class X(six.with_metaclass(Meta, Base, Base2)):
assert X.__mro__ == (X, Base, Base2, object)


@py.test.mark.skipif("sys.version_info[:2] < (3, 0)")
def test_with_metaclass_prepare():
"""Test that with_metaclass causes Meta.__prepare__ to be called with the correct arguments."""

class MyDict(dict):
pass

class Meta(type):

@classmethod
def __prepare__(cls, name, bases):
namespace = MyDict(super().__prepare__(name, bases), cls=cls, bases=bases)
namespace['namespace'] = namespace
return namespace

class Base(object):
pass

bases = (Base,)

class X(six.with_metaclass(Meta, *bases)):
pass

assert getattr(X, 'cls', type) is Meta
assert getattr(X, 'bases', ()) == bases
assert isinstance(getattr(X, 'namespace', {}), MyDict)


def test_wraps():
def f(g):
@six.wraps(g)
Expand Down