Created
September 3, 2014 17:43
-
-
Save zyga/6b548b1f52e49342d3f0 to your computer and use it in GitHub Desktop.
type() bug?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NS: | |
PASSTHRU = frozenset(('__name__', '__qualname__', '__doc__', '__module__')) | |
def __init__(self, data=None): | |
if data is None: | |
data = {} | |
self.data = data | |
def __setitem__(self, name, value): | |
if name in self.PASSTHRU: | |
self.data[name] = value | |
elif isinstance(value, str): | |
self.data[name] = value | |
else: | |
raise TypeError | |
def __getitem__(self, name): | |
if name in self.PASSTHRU: | |
return self.data[name] | |
elif name in self.data: | |
return self.data[name] | |
else: | |
self.data[name] = name | |
return self.data[name] | |
class Meta(type): | |
@classmethod | |
def __prepare__(mcls, name, bases): | |
return NS() | |
def __new__(mcls, name, bases, ns): | |
classdict = ns.data | |
return type.__new__(mcls, name, bases, classdict) | |
class Def(metaclass=Meta): | |
pass | |
class Example(Def): | |
a = 'a' | |
b = 'b' | |
c = 'c' | |
assert Example.a == 'a' | |
assert Example.b == 'b' | |
assert Example.c == 'c' | |
BrokenExample1 = type('BrokenExample1', (Def, ), NS({'x': 'x'})) | |
# Traceback (most recent call last): | |
# File "example.py", line 53, in <module> | |
# BrokenExample1 = type('BrokenExample1', (Def, ), NS({'x': 'x'})) | |
# TypeError: type() argument 3 must be dict, not NS | |
BrokenExample2 = type('BrokenExample2', (Def, ), {'x': 'x'}) | |
# Traceback (most recent call last): | |
# File "example.py", line 58, in <module> | |
# BrokenExample2 = type('BrokenExample2', (Def, ), {'x': 'x'}) | |
# File "example.py", line 35, in __new__ | |
# classdict = ns.data | |
# AttributeError: 'dict' object has no attribute 'data' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes I've figured it out... on line 52 I should have used Meta...