← 输入框|github.com|刷新
Skip to content

Allowing arbitrary expressions in the @expression syntax #81377

Closed

Description

@maggyero
mannequin
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']

    Activity

    1. maggyero commented on Jun 7, 2019

      maggyeromannequin
      MannequinAuthor

      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

    2. added
      3.7 (EOL)
      interpreter-core
      type-feature
      on Jun 7, 2019
    3. tiran commented on Jun 7, 2019

      @tiran
      Member
    4. maggyero commented on Jun 7, 2019

      maggyeromannequin
      MannequinAuthor

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

    5. gvanrossum commented on Jun 7, 2019

      @gvanrossum
      Member

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

    6. maggyero commented on Jun 7, 2019

      maggyeromannequin
      MannequinAuthor

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

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)interpreter-coretype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions