Mailing List Archive

Riddle: Funny decorator chaining behaviour
Chatting in a Spanish python podcast about how convenient would be for
abstract classes to raise exceptions intermediately instead of at
instantiation time (how could you create an abstract class in that
case?), somebody produced this code:

import abc

class a(abc.ABC):
@classmethod
@property
@abc.abstractmethod
def x(cls):
print("Hello world")

class b(a):
def x(*args):
pass

(NOTHING happens, the abstract method/property is overriden)

class c(a):
pass

"HELLO WORLD" is printed, creating the class (not instanciating an
object of that type but creating the class itself), because the abstract
method/property is not overriden.

Note that the method is called at class creation time.

if you replace the body of "x" method with a "raise NotImplementedError"
you can produce that exception at import time, if some class remains
abstract.

This is an interesting interaction between decorators trying to be
clever supporting both regular and abstract methods/properties.

Trying to figure out how it worked took some time and python source code
diving. I think this is interesting enough to share.

I don't think this is a bug to solve, just some brain teasing. I let
other to decide what to do with this... collateral effect.

Let me know what do you think after trying to understand how it works.

--
Jesús Cea Avión _/_/ _/_/_/ _/_/_/
jcea@jcea.es - https://www.jcea.es/ _/_/ _/_/ _/_/ _/_/ _/_/
Twitter: @jcea _/_/ _/_/ _/_/_/_/_/
jabber / xmpp:jcea@jabber.org _/_/ _/_/ _/_/ _/_/ _/_/
"Things are not so easy" _/_/ _/_/ _/_/ _/_/ _/_/ _/_/
"My name is Dump, Core Dump" _/_/_/ _/_/_/ _/_/ _/_/
"El amor es poner tu felicidad en la felicidad de otro" - Leibniz
Re: Riddle: Funny decorator chaining behaviour [ In reply to ]
I believe that your example is the same as this recently opened bug
report.

https://github.com/python/cpython/issues/93167


--
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/ZIJWJS7DPPP5QSUYS5LOXRQ7BYATNQNJ/
Code of Conduct: http://python.org/psf/codeofconduct/