Issue 33268: iteration over attrs in metaclass' new affects class' name (original) (raw)
The following script, run with python 3.6.5, highlights an issue with the class' name attribute being set incorrectly just because of a loop in the metaclass' new method:
class MC(type): def new(mcs, name, bases, attrs): for name, attr in attrs.items(): pass return super(MC, mcs).new(mcs, name, bases, attrs)
class C(metaclass=MC): a = None
print(C.name)
Expected output: "C" Actual output: "a"
Comment the for loop and you get the expected output!
On Python 2.7.13, the amended code exposes the same bug:
class MC(type): def new(mcs, name, bases, attrs): for name, attr in attrs.items(): pass return super(MC, mcs).new(mcs, name, bases, attrs)
class C(object): metaclass = MC a = None
print C.name
output is "metaclass" and should be "C"