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: type.__init__ called instead of cls.__init__ when inheriting from type.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Hameer Abbasi, steven.daprano
Priority: normal Keywords:

Created on 2019-03-04 08:44 by Hameer Abbasi, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg337079 - (view) Author: Hameer Abbasi (Hameer Abbasi) Date: 2019-03-04 08:44
I may be completely misunderstanding here, but: here's a reproducible example:

class MyMeta(type):
    def __new__(cls, *args, **kwargs):
        print('__new__', *args, **kwargs)
        super().__new__(cls, *args, **kwargs)
    
    def __init__(self, a):
        print('__init__', *args, **kwargs)
        super().__init__(*args, **kwargs)

class A(metaclass=MyMeta):
    pass

MyMeta('A', (), {'__module__': '__main__', '__qualname__': 'A'})


Output:

__new__ A () {'__module__': '__main__', '__qualname__': 'A'}
__new__ A () {'__module__': '__main__', '__qualname__': 'A'}

Is this by design?
msg337085 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-03-04 09:17
Your metaclass.__new__ method returns None instead of the new class. The rule for calling __init__ is:

- if the constructor __new__ returns an instance of the type, then call the initializer __init__

- otherwise, don't call __init__ at all.

https://docs.python.org/3/reference/datamodel.html#object.__new__

Since your __new__ accidentally returns None, the __init__ is not called. If you change the line to say 

return super().__new__(cls, *args, **kwargs)

you will see that __init__ is called. (And discover the bugs in your init method :-)
msg337086 - (view) Author: Hameer Abbasi (Hameer Abbasi) Date: 2019-03-04 09:22
Ah, I wasn't aware I had to return...

The bug was deliberate to show that not even a different signature makes a difference. ;)
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80359
2019-03-04 09:22:24Hameer Abbasisetmessages: + msg337086
2019-03-04 09:17:57steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg337085

resolution: not a bug
stage: resolved
2019-03-04 08:44:53Hameer Abbasicreate