Comma is not always OK in the argument list?!

R

Roman Susi

Hi!

it is interesting that I found this syntax error:
File "<stdin>", line 1
str('sdfd', **a,)
^
SyntaxError: invalid syntax


I just wonder is it intentional or by-product (bug or feature)?
(The behaviour makes sense, of course... I tend to leave commas when I
expect a list to be added to (i.e. almost always ;-)))


Regards,
Roman
 
N

Nick Vatamaniuc

Roman,

According to the Python call syntax definition
(http://docs.python.org/ref/calls.html) commas should be allowed, so it
seems like a minor bug. Here are the lines in question:
-----http://docs.python.org/ref/calls.html-----------
call ::= primary "(" [argument_list [","]] ")"
argument_list::=positional_arguments ["," keyword_arguments] ["," "*"
expression] ["," "**" expression]
| keyword_arguments ["," "*" expression] ["," "**" expression]
| "*" expression ["," "**" expression]
| "**" expression
----------------------------------------------------------
If you notice in the 'call' definition, no matter what the
'argument_list' is, it can be followed by an optional ',' right before
the closing ')'. Your code is a counterexample to this. Here is a more
exhaustive example:
--------------------------------------------------------.... pass
....
f(1,2)
f(1,2,)
f(1,*[2])
f(1,*[2],)
File "<stdin>", line 1
f(1,*[2],)
^
SyntaxError: invalid syntax File "<stdin>", line 1
f(1,**{'b':2},)
^
SyntaxError: invalid syntax
File "<stdin>", line 1
f(*[1,2],)
^
SyntaxError: invalid syntax File "<stdin>", line 1
f(**{'a':1,'b':2},)
^
SyntaxError: invalid syntax-----------------------------------------------------

Anybody else knows more about this?
/invoking the spirit of GvR... ;-)

Nick Vatamaniuc
 
O

olsongt

Nick said:
Roman,

According to the Python call syntax definition
(http://docs.python.org/ref/calls.html) commas should be allowed, so it
seems like a minor bug. Here are the lines in question:
-----http://docs.python.org/ref/calls.html-----------
call ::= primary "(" [argument_list [","]] ")"
argument_list::=positional_arguments ["," keyword_arguments] ["," "*"
expression] ["," "**" expression]
| keyword_arguments ["," "*" expression] ["," "**" expression]
| "*" expression ["," "**" expression]
| "**" expression
----------------------------------------------------------
If you notice in the 'call' definition, no matter what the
'argument_list' is, it can be followed by an optional ',' right before
the closing ')'. Your code is a counterexample to this. Here is a more
exhaustive example:
--------------------------------------------------------

Actually, in the real BNF it's not allowed:

http://svn.python.org/view/python/trunk/Grammar/Grammar?rev=46209&view=markup

parameters: '(' [varargslist] ')'
varargslist: ((fpdef ['=' test] ',')*
('*' NAME [',' '**' NAME] | '**' NAME) |
fpdef ['=' test] (',' fpdef ['=' test])* [','])
fpdef: NAME | '(' fplist ')'
 
N

Nick Vatamaniuc

True, that is why it behaves the way it does, but which way is the
correct way? i.e. does the code need updating or the documentation?

-Nick V.

Nick said:
Roman,

According to the Python call syntax definition
(http://docs.python.org/ref/calls.html) commas should be allowed, so it
seems like a minor bug. Here are the lines in question:
-----http://docs.python.org/ref/calls.html-----------
call ::= primary "(" [argument_list [","]] ")"
argument_list::=positional_arguments ["," keyword_arguments] ["," "*"
expression] ["," "**" expression]
| keyword_arguments ["," "*" expression] ["," "**" expression]
| "*" expression ["," "**" expression]
| "**" expression
----------------------------------------------------------
If you notice in the 'call' definition, no matter what the
'argument_list' is, it can be followed by an optional ',' right before
the closing ')'. Your code is a counterexample to this. Here is a more
exhaustive example:
--------------------------------------------------------

Actually, in the real BNF it's not allowed:

http://svn.python.org/view/python/trunk/Grammar/Grammar?rev=46209&view=markup

parameters: '(' [varargslist] ')'
varargslist: ((fpdef ['=' test] ',')*
('*' NAME [',' '**' NAME] | '**' NAME) |
fpdef ['=' test] (',' fpdef ['=' test])* [','])
fpdef: NAME | '(' fplist ')'
 
T

Terry Reedy

Nick Vatamaniuc said:
True, that is why it behaves the way it does, but which way is the
correct way? i.e. does the code need updating or the documentation?

I think the doc.
 
R

Roman Susi

Nick said:
True, that is why it behaves the way it does, but which way is the
correct way? i.e. does the code need updating or the documentation?
Perhaps, someone can make a bug report... IMHO, docs are wrong.

-Roman
-Nick V.

(e-mail address removed) wrote:

Nick Vatamaniuc wrote:

Roman,

According to the Python call syntax definition
(http://docs.python.org/ref/calls.html) commas should be allowed, so it
seems like a minor bug. Here are the lines in question:
-----http://docs.python.org/ref/calls.html-----------
call ::= primary "(" [argument_list [","]] ")"
argument_list::=positional_arguments ["," keyword_arguments] ["," "*"
expression] ["," "**" expression]
| keyword_arguments ["," "*" expression] ["," "**" expression]
| "*" expression ["," "**" expression]
| "**" expression
----------------------------------------------------------
If you notice in the 'call' definition, no matter what the
'argument_list' is, it can be followed by an optional ',' right before
the closing ')'. Your code is a counterexample to this. Here is a more
exhaustive example:
Actually, in the real BNF it's not allowed:

http://svn.python.org/view/python/trunk/Grammar/Grammar?rev=46209&view=markup

parameters: '(' [varargslist] ')'
varargslist: ((fpdef ['=' test] ',')*
('*' NAME [',' '**' NAME] | '**' NAME) |
fpdef ['=' test] (',' fpdef ['=' test])* [','])
fpdef: NAME | '(' fplist ')'
 
N

Nick Vatamaniuc

Roman,

The way I see it, it could be either way. In other words if I can
write f(1,2,3) and f(1,2,3,) I should also be able to write
f(1,*[2,3],). It is a really small detail but there sould be some
consistency. Either no extra commas for all kinds of argument types or
extra commas for _all_ of them. It seems though also that if it is
possible to do it with lists, tuples and dictionaries, it should also
be possible to do it with argument lists. In other words if (1,2,3,)
makes sense so should func(1,2,3,) even when written as
func(1,2,*[3],).

Well you are the one who discovered this so you shoud be the one
submitting the bug report! Here is PEP 3 page with the guidelines for
bug reporting:
http://www.python.org/dev/peps/pep-0003/

Just mark it as a very low priority since it is more of a cosmetic bug
than a serious showstopper.

-Nick V


Roman said:
Nick said:
True, that is why it behaves the way it does, but which way is the
correct way? i.e. does the code need updating or the documentation?
Perhaps, someone can make a bug report... IMHO, docs are wrong.

-Roman
-Nick V.

(e-mail address removed) wrote:

Nick Vatamaniuc wrote:


Roman,

According to the Python call syntax definition
(http://docs.python.org/ref/calls.html) commas should be allowed, so it
seems like a minor bug. Here are the lines in question:
-----http://docs.python.org/ref/calls.html-----------
call ::= primary "(" [argument_list [","]] ")"
argument_list::=positional_arguments ["," keyword_arguments] ["," "*"
expression] ["," "**" expression]
| keyword_arguments ["," "*" expression] ["," "**" expression]
| "*" expression ["," "**" expression]
| "**" expression
----------------------------------------------------------
If you notice in the 'call' definition, no matter what the
'argument_list' is, it can be followed by an optional ',' right before
the closing ')'. Your code is a counterexample to this. Here is a more
exhaustive example:
--------------------------------------------------------


Actually, in the real BNF it's not allowed:

http://svn.python.org/view/python/trunk/Grammar/Grammar?rev=46209&view=markup

parameters: '(' [varargslist] ')'
varargslist: ((fpdef ['=' test] ',')*
('*' NAME [',' '**' NAME] | '**' NAME) |
fpdef ['=' test] (',' fpdef ['=' test])* [','])
fpdef: NAME | '(' fplist ')'
 
D

Dennis Lee Bieber

Roman,

The way I see it, it could be either way. In other words if I can
write f(1,2,3) and f(1,2,3,) I should also be able to write
f(1,*[2,3],). It is a really small detail but there sould be some
consistency. Either no extra commas for all kinds of argument types or

Part of the problem may be that the * notation implies that the
associated argument is supposed to fill ALL other supplied positional
arguments -- so what is that empty argument after the , supposed to be
associated with? A positional argument /after/ all positional arguments?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
N

Nick Vatamaniuc

Dennis,

You make a good point, that is what I though first. Semantically I
thought a comma after **kw as in ..., **kw,) doesn't make sense because
there is nothing that could follow **kw except the ')'. But then trying
some other cases (see the previous posts for my examples) I noticed
that commas aren't allowed after *pos_args also. For example if ....,
*pos_args,) would be an error BUT stuff could follow *pos_args and that
could be **kw as in ..., *pos_args,**kw).

In other words the behavior is not consistent. So it seems that 3
things could happen with this:

1) Not allow commas after **kw only. Allow them in any other case,
because semantically 'stuff' could possibly follow.

2) Don't allow extra commas for all the argument lists. In other words
f(1,2,) or f(a=1,b=2,) would be an error. But then do we want argument
lists to be consistent with tuples as far as syntax goes?

3) Allow trailing commas after all kinds of arguments in the argument
lists. This is what the documentation describes at the moment. I think
this is more sensible. I understand that argument lists and tuples are
not the same, but it would be nice to have a _syntactic_ consistency,
as opposed to 'a surprise'. As in t=(1,2,3,) f(1,2,3,) f(1,*[2,3],)
and f(1,*[2],**{'c':3},) should all be 'OK'.

Perhaps more Python core developers would comment...

Nick Vatamaniuc

Roman,

The way I see it, it could be either way. In other words if I can
write f(1,2,3) and f(1,2,3,) I should also be able to write
f(1,*[2,3],). It is a really small detail but there sould be some
consistency. Either no extra commas for all kinds of argument types or

Part of the problem may be that the * notation implies that the
associated argument is supposed to fill ALL other supplied positional
arguments -- so what is that empty argument after the , supposed to be
associated with? A positional argument /after/ all positional arguments?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top