This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: __metaclass__ in locals is ignored
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, jepler
Priority: normal Keywords:

Created on 2004-08-30 12:52 by jepler, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
localmeta.py jepler, 2004-08-30 12:52 Demonstrates problem
Messages (2)
msg22275 - (view) Author: Jeff Epler (jepler) Date: 2004-08-30 12:52
The local namespace is not checked for a __metaclass__
when constructing a new class.  This may be consistent
with a strict reading of the language standard
[http://docs.python.org/ref/metaclasses.html#l2h-216]
but it's surprising.

I think that the lookup for __metaclass__ should
proceed in the same way as any other name lookup (after
it is not found in the namespace of the class-to-be),
which would include a lookup in the local namespace.

The attached file should print "metaclass called with
..." and "C constructed as None..." but it doesn't in
the "local case"

If this behavior is not a bug, would a doc patch
emphasizing the non-lookup in locals be accepted?
msg22276 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2004-09-05 20:10
Logged In: YES 
user_id=357491

It's not a bug.  If you wanted your little class to have a metaclass it 
should have been defined in a __metaclass__ attribute.  The whole global 
__metaclass__ thing in the global namespace is just a helper situation 
when you want all code to have the same metaclass, but also just to 
move old code over to new-style.  It is still better practice to list the 
metaclass explicitly in the class.

As for a patch about locals, I don't think it is needed since it is fairly 
explicit about what cases lead to a metaclass being used.  Always go 
with a strict reading of the docs.

I wouldn't mind seeing a patch that clarified the first bullet point about 
``dict[__metaclass__]`` and a line saying that specifying it as a class 
attribute  is the best practice, though.  But that should be a separate 
patch and not tied to this bug report which I am closing.
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40844
2004-08-30 12:52:28jeplercreate