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

PEG parser doesn't allow lambda in for_if_clause #86014

Closed
slebedev mannequin opened this issue Sep 23, 2020 · 5 comments
Closed

PEG parser doesn't allow lambda in for_if_clause #86014

slebedev mannequin opened this issue Sep 23, 2020 · 5 comments
Labels
3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@slebedev
Copy link
Mannequin

slebedev mannequin commented Sep 23, 2020

BPO 41848
Nosy @gvanrossum, @terryjreedy, @benjaminp, @serhiy-storchaka, @lysnikolaou, @pablogsal, @isidentical
PRs
  • bpo-41848: Allow simple lambdas in if clauses of comprehensions #22394
  • 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-09-26.23:36:40.147>
    created_at = <Date 2020-09-23.23:33:02.167>
    labels = ['interpreter-core', 'type-bug', '3.9', '3.10']
    title = "PEG parser doesn't allow lambda in for_if_clause"
    updated_at = <Date 2020-09-26.23:36:40.145>
    user = 'https://bugs.python.org/slebedev'

    bugs.python.org fields:

    activity = <Date 2020-09-26.23:36:40.145>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-09-26.23:36:40.147>
    closer = 'gvanrossum'
    components = ['Interpreter Core']
    creation = <Date 2020-09-23.23:33:02.167>
    creator = 'slebedev'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41848
    keywords = ['patch']
    message_count = 5.0
    messages = ['377435', '377511', '377514', '377545', '377546']
    nosy_count = 8.0
    nosy_names = ['gvanrossum', 'terry.reedy', 'benjamin.peterson', 'serhiy.storchaka', 'lys.nikolaou', 'pablogsal', 'BTaskaya', 'slebedev']
    pr_nums = ['22394']
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41848'
    versions = ['Python 3.9', 'Python 3.10']

    @slebedev
    Copy link
    Mannequin Author

    slebedev mannequin commented Sep 23, 2020

    Reproducer:

    [x for x in [] if lambda: x]

    This parses fine in 3.8, but doesn't parse in 3.9 because the grammar expects a disjunction after if in for_if_clause *. While this change has zero practical significance, I think it might be useful to maintain a list of such changes for tooling authors (who might need to support multiple Python versions).

    @slebedev slebedev mannequin added 3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 23, 2020
    @terryjreedy
    Copy link
    Member

    After adding parens,
    >>> [x for x in [] if (lambda: x)]
    []

    @gvanrossum
    Copy link
    Member

    I propose not to fix this, since it is pointless (a lambda is always truthy).

    We can call it out in What's New in 3.9 -- as a very small footnote. :-)

    @pablogsal
    Copy link
    Member

    I concur with Guido: it makes the grammar slightly more complex for no gain. Also, the originally allowed rule does not allow *any* lambda but only the ones that are simple, which is a special case that makes it even more complex. In any case you can use parentheses to break this limitation.

    Like Guido, I propose to add a very small comment to What's New in 3.9

    @gvanrossum
    Copy link
    Member

    Okay, let's close this.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    da-woods added a commit to da-woods/cython that referenced this issue Aug 21, 2022
    Note that it wasn't correct before since it didn't pass the
    correct flag to `p_lambdef` and thus was equivalent to just
    using `p_lambdef`.
    
    Note also that there's a difference in behaviour between Python3.9+
    and before. Python <3.9 allowed `[i for i in range(10) if lambda: i]`
    while Python >=3.9 disallows this. Arguably it's pointless because
    the lambda always evaluates to True. See
    python/cpython#86014
    for the Python issue.
    
    With this change Cython will follow the Python 3.9 behaviour at
    the cost of potentially breaking some code that does use the
    pattern above. If that isn't desirable I can produce an
    alternative change that fixes p_lambda_nocond instead.
    
    Part of cleanup in cython#4595.
    da-woods added a commit to da-woods/cython that referenced this issue Aug 21, 2022
    Note that it wasn't correct before since it didn't pass the
    correct flag to `p_lambdef` and thus was equivalent to just
    using `p_lambdef`.
    
    Note also that there's a difference in behaviour between Python3.9+
    and before. Python <3.9 allowed `[i for i in range(10) if lambda: i]`
    while Python >=3.9 disallows this. Arguably it's pointless because
    the lambda always evaluates to True. See
    python/cpython#86014
    for the Python issue.
    
    With this change Cython will follow the Python 3.9 behaviour at
    the cost of potentially breaking some code that does use the
    pattern above. If that isn't desirable I can produce an
    alternative change that fixes p_lambda_nocond instead.
    
    Part of cleanup in cython#4595.
    scoder pushed a commit to cython/cython that referenced this issue Sep 5, 2022
    …-4992)
    
    Note that it wasn't correct before since it didn't pass the
    correct flag to `p_lambdef` and thus was equivalent to just
    using `p_lambdef`.
    
    Note also that there's a difference in behaviour between Python3.9+
    and before. Python <3.9 allowed `[i for i in range(10) if lambda: i]`
    while Python >=3.9 disallows this. Arguably it's pointless because
    the lambda always evaluates to True. See
    python/cpython#86014
    for the Python issue.
    
    With this change Cython will follow the Python 3.9 behaviour at
    the cost of potentially breaking some code that does use the
    pattern above.
    
    Part of the cleanup in #4595
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants