-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Tricky SyntaxErrors #1
Comments
Hey @MostAwesomeDude I really liked your "knot not" treasure, but not sure if I understood correctly the "surprising comma" one. As far as I understand, "having a trailing comma after *args or *kwargs is not a valid syntax". But I guess you're trying to show something more than that in that example which I'm not able to catch. Could you please explain 😅 |
I kinda prepared draft for the "not knot" one not knot.Originally suggested by @MostAwesomeDude in this issue. x = True
y = False Output: >>> not x == y
True
>>> x == not y
File "<input>", line 1
x == not y
^
SyntaxError: invalid syntax 💡 Explanation:
Let me know, if it needs any corrections |
Hi! The surprising comma is surprising because, normally, these sorts of commas are not a problem. In two similar situations, the trailing commas are legal:
And one might expect that a trailing comma is always legal in argument lists. However, due to the way that the Python syntax is designed, the argument list is defined partially with leading commas and partially with trailing commas. This conflict causes situations where a comma is trapped in the middle, and no rule will accept it. The not knot analysis is quite good, although it misses a critical detail: the reason why |
I see, a final question, would it be correct to say that "parser expected the |
I worry that it glosses over how parsers work, but OTOH I don't really expect folks to understand how parsers work, so sure, that's close enough. |
Hey, I've added the Not Knot! example in the commit 6abfb50 🎉 Please feel free to suggest changes (if any). And here's a draft I prepared for the "The surprising comma" one. The surprising commaSuggested by @MostAwesomeDude in this issue. Output: >>> def f(x, y,):
... print(x, y)
...
>>> def g(x=4, y=5,):
... print(x, y)
...
>>> def h(x, **kwargs,):
File "<stdin>", line 1
def h(x, **kwargs,):
^
SyntaxError: invalid syntax
>>> def h(*args,):
File "<stdin>", line 1
def h(*args,):
^
SyntaxError: invalid syntax 💡 Explanation:
Would like to know if you've ideas to present it in more interesting way or if the explanation is insufficient. |
Probably should mention the trailing comma problem is fixed in Python 3.6. The linked issue also contains some detail explanation why this happens (grammar definition in the parser). |
Thank you so much @MostAwesomeDude for the examples from your "treasure". I've added both of them in the commits be98d88 and 6abfb50. 🎉 Please feel free to reopen the issue if you think the explanations/snippets are insufficient or inaccurate. |
These are some of my treasures, discovered while implementing a Python parser years ago.
First, the "surprising comma."
Codepad: http://codepad.org/JiFQi7vr
Technically only
f(*args,)
orf(**kwargs,)
is needed to tickle this one.Second, the "not knot."
Codepad: http://codepad.org/IA7k5jEg
Again, a smaller test case, like
x == not y
, will provoke it.The text was updated successfully, but these errors were encountered: