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

list/generator comprehension parser doesn't match spec #46781

Closed
kousu mannequin opened this issue Apr 1, 2008 · 4 comments
Closed

list/generator comprehension parser doesn't match spec #46781

kousu mannequin opened this issue Apr 1, 2008 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@kousu
Copy link
Mannequin

kousu mannequin commented Apr 1, 2008

BPO 2529
Nosy @amauryfa, @kousu

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 2008-04-01.20:56:04.957>
created_at = <Date 2008-04-01.19:19:09.269>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = "list/generator comprehension parser doesn't match spec"
updated_at = <Date 2008-04-02.15:33:02.236>
user = 'https://github.com/kousu'

bugs.python.org fields:

activity = <Date 2008-04-02.15:33:02.236>
actor = 'kousu'
assignee = 'none'
closed = True
closed_date = <Date 2008-04-01.20:56:04.957>
closer = 'amaury.forgeotdarc'
components = ['Interpreter Core']
creation = <Date 2008-04-01.19:19:09.269>
creator = 'kousu'
dependencies = []
files = []
hgrepos = []
issue_num = 2529
keywords = []
message_count = 4.0
messages = ['64818', '64819', '64823', '64861']
nosy_count = 3.0
nosy_names = ['amaury.forgeotdarc', 'kousu', 'lehmannro']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2529'
versions = ['Python 2.5']

@kousu
Copy link
Mannequin Author

kousu mannequin commented Apr 1, 2008

I think I've found a bug in python's list comprehension parser. Observe:

>>> [e for i in j in ['a','b','c']]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined

Now, according to the grammar at http://docs.python.org/ref/lists.html,
a list comprehension is (condensed for clarity):
list_comprehension ::= expression list_for
list_for ::= "for" target_list "in" old_expression_list [list_for]

So a list comprehension should always be
[.... for ... in .... for ... in ... for ... in ...]
(that is, alternating 'for's and 'in's) but here I have a test case that
python happily tries to run that looks like
[... for ... in ... in ....]

@kousu kousu mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Apr 1, 2008
@lehmannro
Copy link
Mannequin

lehmannro mannequin commented Apr 1, 2008

Your example is parsed as [e for i in (j in ['a','b','c'])] and since
j is not defined, you get a NameError. If it was defined, you would
still be iterating a boolean (which is not defined).

Grammatically, this is the following (just the important parts, again):
list_comprehension ::= expression list_for
list_for ::= "for" target_list "in" old_expression_list
old_expression_list ::= old_expression
old_expression ::= <stripped test hierarchy...> comparison
comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "in"

So your basic misconception is that both in keywords are belonging to
the list comprehension syntax -- the former does while the latter is
simply an operator.

@amauryfa
Copy link
Member

amauryfa commented Apr 1, 2008

Indeed; your sample is equivalent to:

temp = (j in ['a','b','c'])        # the "contains" operator
[e for i in temp]                  # basic list comprehension

Even if not meaningful, this code is syntactically correct.

@kousu
Copy link
Mannequin Author

kousu mannequin commented Apr 2, 2008

Oh, okay. That's really confusing because I expect "in" to always return
a bool, but in the spirit of python there's no reason it should I guess.

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

1 participant