Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing arbitrary expressions in the @expression syntax #81377

Closed
maggyero mannequin opened this issue Jun 7, 2019 · 5 comments
Closed

Allowing arbitrary expressions in the @expression syntax #81377

maggyero mannequin opened this issue Jun 7, 2019 · 5 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@maggyero
Copy link
Mannequin

maggyero mannequin commented Jun 7, 2019

BPO 37196
Nosy @mwhudson, @rhettinger, @etrepum, @tiran, @jimfulton, @maggyero, @brandtbucher
Superseder
  • bpo-39702: PEP 614: Relaxing Grammar Restrictions On Decorators
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-02-20.16:20:23.015>
    created_at = <Date 2019-06-07.14:05:15.545>
    labels = ['interpreter-core', 'type-feature', '3.7']
    title = 'Allowing arbitrary expressions in the @expression syntax'
    updated_at = <Date 2020-02-20.16:20:23.014>
    user = 'https://github.com/maggyero'

    bugs.python.org fields:

    activity = <Date 2020-02-20.16:20:23.014>
    actor = 'brandtbucher'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-02-20.16:20:23.015>
    closer = 'brandtbucher'
    components = ['Interpreter Core']
    creation = <Date 2019-06-07.14:05:15.545>
    creator = 'maggyero'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37196
    keywords = []
    message_count = 5.0
    messages = ['344939', '344940', '344944', '344946', '344952']
    nosy_count = 8.0
    nosy_names = ['mwh', 'rhettinger', 'bob.ippolito', 'exarkun', 'christian.heimes', 'j1m', 'maggyero', 'brandtbucher']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '39702'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue37196'
    versions = ['Python 3.7']

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented Jun 7, 2019

    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

    @maggyero maggyero mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Jun 7, 2019
    @tiran
    Copy link
    Member

    tiran commented Jun 7, 2019

    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

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented Jun 7, 2019

    @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?!".

    @gvanrossum
    Copy link
    Member

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

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented Jun 7, 2019

    @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.

    @maggyero maggyero mannequin reopened this Jun 7, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants