Title: Minor improvement to errors with (no) missing colon · Issue #92858 · python/cpython · GitHub
Open Graph Title: Minor improvement to errors with (no) missing colon · Issue #92858 · python/cpython
X Title: Minor improvement to errors with (no) missing colon · Issue #92858 · python/cpython
Description: Some invalid for statements currently give the specific error expected ':' even if there is a colon on the line. I think it would be a slight improvement to raise a more general error in these cases. This is how while and if already beha...
Open Graph Description: Some invalid for statements currently give the specific error expected ':' even if there is a colon on the line. I think it would be a slight improvement to raise a more general error in these case...
X Description: Some invalid for statements currently give the specific error expected ':' even if there is a colon on the line. I think it would be a slight improvement to raise a more general error in th...
Opengraph URL: https://github.com/python/cpython/issues/92858
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Minor improvement to errors with (no) missing colon","articleBody":"Some invalid `for` statements currently give the specific error `expected ':'` even if there is a colon on the line. I think it would be a slight improvement to raise a more general error in these cases. This is how `while` and `if` already behave.\r\n\r\n**Current behaviour:**\r\n```python\r\n \u003e\u003e\u003e for x in range(10)\r\n ... pass\r\n Traceback (most recent call last):\r\n SyntaxError: expected ':'\r\n\r\n \u003e\u003e\u003e for x in range 10:\r\n ... pass\r\n Traceback (most recent call last):\r\n SyntaxError: expected ':'\r\n\r\n \u003e\u003e\u003e for x in range 10\r\n ... pass\r\n Traceback (most recent call last):\r\n SyntaxError: expected ':'\r\n```\r\n**Proposed behaviour:**\r\n```python\r\n \u003e\u003e\u003e for x in range(10)\r\n ... pass\r\n Traceback (most recent call last):\r\n SyntaxError: expected ':'\r\n\r\n \u003e\u003e\u003e for x in range 10:\r\n ... pass\r\n Traceback (most recent call last):\r\n SyntaxError: invalid syntax\r\n\r\n \u003e\u003e\u003e for x in range 10\r\n ... pass\r\n Traceback (most recent call last):\r\n SyntaxError: invalid syntax\r\n```\r\n\r\n**Proposed Implementation:**\r\nWould do it the same way `while` and `if` do it. For the for loop example it would be something like this:\r\n```diff\r\n--- a/Grammar/python.gram\r\n+++ b/Grammar/python.gram\r\n@@ -379,7 +379,7 @@ while_stmt[stmt_ty]:\r\n\r\n for_stmt[stmt_ty]:\r\n | invalid_for_stmt\r\n- | 'for' t=star_targets 'in' ~ ex=star_expressions \u0026\u0026':' tc=[TYPE_COMMENT] b=block el=[else_block] {\r\n+ | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {\r\n _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }\r\n | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions \u0026\u0026':' tc=[TYPE_COMMENT] b=block el=[else_block] {\r\n CHECK_VERSION(stmt_ty, 5, \"Async for loops are\", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }\r\n@@ -1295,6 +1295,7 @@ invalid_while_stmt:\r\n | a='while' named_expression ':' NEWLINE !INDENT {\r\n RAISE_INDENTATION_ERROR(\"expected an indented block after 'while' statement on line %d\", a-\u003elineno) }\r\n invalid_for_stmt:\r\n+ | 'for' star_targets 'in' star_expressions NEWLINE { RAISE_SYNTAX_ERROR(\"expected ':'\") }\r\n | [ASYNC] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {\r\n RAISE_INDENTATION_ERROR(\"expected an indented block after 'for' statement on line %d\", a-\u003elineno) }\r\n invalid_def_raw:\r\n```\r\nI'd also apply this to other suites where it makes sense (e.g. `with`, `match/case`, and `try`)\r\n\r\nWould it be OK for me to submit a PR making these changes? (this is my first time looking into the CPython parser so apologies if I made any obvious mistakes!)\r\n\r\n","author":{"url":"https://github.com/wookie184","@type":"Person","name":"wookie184"},"datePublished":"2022-05-16T19:38:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/92858/cpython/issues/92858"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:12e5e8de-533a-b104-3372-a6bafef8def2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA5A:2D5987:18EFFB2:21BDC8E:6969AD54 |
| html-safe-nonce | 228ee957dafb5a76441ce7dc6254a1c8fae07377e4deff1e4c55b55f6644d825 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTVBOjJENTk4NzoxOEVGRkIyOjIxQkRDOEU6Njk2OUFENTQiLCJ2aXNpdG9yX2lkIjoiMzM4Mjk4MTc4NDc0NjE3NTgyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ed74c8af8c662398c2a5c3a9abb6d437d7bd958f1b3e103f52c17164bdf484c7 |
| hovercard-subject-tag | issue:1237624052 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/92858/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7353c948fcd7d5151634ae5e4120225d3e8ba57522a72845f36c5dc13325ffa4/python/cpython/issues/92858 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7353c948fcd7d5151634ae5e4120225d3e8ba57522a72845f36c5dc13325ffa4/python/cpython/issues/92858 |
| og:image:alt | Some invalid for statements currently give the specific error expected ':' even if there is a colon on the line. I think it would be a slight improvement to raise a more general error in these case... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wookie184 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 24c4c97a2d520cb286b35e1a4c22d7a4df3c26a2fa28dd7cdf0e65db327b4de7 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 124667f43168afb6c9c03b7c02eb5b1d2e1be3d9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width