classification
Title: Allowing arbitrary expressions in the @expression syntax
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: PEP 614: Relaxing Grammar Restrictions On Decorators
View: 39702
Assigned To: Nosy List: bob.ippolito, brandtbucher, christian.heimes, exarkun, j1m, maggyero, mwh, rhettinger
Priority: normal Keywords:

Created on 2019-06-07 14:05 by maggyero, last changed 2020-02-20 16:20 by brandtbucher. This issue is now closed.

Messages (5)
msg344939 - (view) Author: Géry (maggyero) * Date: 2019-06-07 14:05
Could we allow arbitrary expressions in the @expression syntax for applying decorators to functions/classes? The current grammar restriction to:

    decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE

is very surprising and I don't understand the real motivation.

I find it weird that you are not able to do that:

    def f():
        def g():
            def h(x):
                pass
            return h
        return g
    
    @f()()
    def i():
        pass

since you get:

        @f()()
            ^
    SyntaxError: invalid syntax

but the following is perfectly valid:

    def f():
        def g():
            def h(x):
                pass
            return h
        return g
    
    def g(x):
        def h(x):
            pass
        return g
    
    @g(f()())
    def h():
        pass

See this post for more details: https://stackoverflow.com/questions/56490579
msg344940 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2019-06-07 14:06
The syntax is deliberately limited. The reasons are explained at:

https://www.python.org/dev/peps/pep-0318/#current-syntax
https://mail.python.org/pipermail/python-dev/2004-August/046711.html
msg344944 - (view) Author: Géry (maggyero) * Date: 2019-06-07 14:28
@Christian Heimes

> The reasons are explained at:

Yes I read that. But I am wondering if after 15 years Guido still has this "gut feeling". Because my gut feeling as a Python *user* who has discovered decorators and stumbled on this restriction was: "what?!".
msg344946 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-06-07 14:46
What you want to write is as unreadable as it ever was.
msg344952 - (view) Author: Géry (maggyero) * Date: 2019-06-07 15:35
@Guido van Rossum

> What you want to write is as unreadable as it ever was.

How is @g(f()()) more readable than @f()()? Yet the former is allowed.
History
Date User Action Args
2020-02-20 16:20:23brandtbuchersetstatus: open -> closed
nosy: + brandtbucher
superseder: PEP 614: Relaxing Grammar Restrictions On Decorators
resolution: duplicate
2019-06-07 20:19:30gvanrossumsetnosy: - gvanrossum
2019-06-07 15:35:16maggyerosetstatus: closed -> open
resolution: wont fix -> (no value)
messages: + msg344952
2019-06-07 14:46:40gvanrossumsetstatus: open -> closed
resolution: wont fix
messages: + msg344946

stage: resolved
2019-06-07 14:28:11maggyerosetmessages: + msg344944
2019-06-07 14:06:42christian.heimessetnosy: + christian.heimes
messages: + msg344940
2019-06-07 14:05:15maggyerocreate