Skip to content

Provide more helpful error message when await is called inside non-async method #70376

Closed

Description

@nchammas
mannequin
BPO 26188
Nosy @gvanrossum, @pitrou, @vstinner, @giampaolo, @asvetlov, @vadmium, @1st1, @nchammas

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 2017-12-20.21:01:36.773>
created_at = <Date 2016-01-24.02:06:03.383>
labels = ['interpreter-core', 'expert-asyncio']
title = 'Provide more helpful error message when `await` is called inside non-`async` method'
updated_at = <Date 2017-12-20.21:01:36.771>
user = 'https://github.com/nchammas'

bugs.python.org fields:

activity = <Date 2017-12-20.21:01:36.771>
actor = 'asvetlov'
assignee = 'none'
closed = True
closed_date = <Date 2017-12-20.21:01:36.773>
closer = 'asvetlov'
components = ['Interpreter Core', 'asyncio']
creation = <Date 2016-01-24.02:06:03.383>
creator = 'nchammas'
dependencies = []
files = []
hgrepos = []
issue_num = 26188
keywords = []
message_count = 5.0
messages = ['258879', '258989', '259386', '259434', '308813']
nosy_count = 9.0
nosy_names = ['gvanrossum', 'pitrou', 'vstinner', 'giampaolo.rodola', 'asvetlov', 'martin.panter', 'yselivanov', 'nchammas', 'aditya gupta']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue26188'
versions = ['Python 3.5', 'Python 3.6']

Activity

  1. nchammas commented on Jan 24, 2016

    nchammasmannequin
    MannequinAuthor

    Here is the user interaction:

    $ python3
    Python 3.5.1 (default, Dec  7 2015, 21:59:10) 
    [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def oh_hai():
    ...     await something()
      File "<stdin>", line 2
        await something()
                      ^
    SyntaxError: invalid syntax

    It would be helpful if Python could tell the user something more specific about _why_ the syntax is invalid. Is that possible?

    For example, in the case above, an error message along the following lines would be much more helpful:

    SyntaxError: Cannot call `await` inside non-`async` method.
    

    Without a hint like this, it's too easy to miss the obvious and waste time eye-balling the code, like I did. :-)

  2. added
    interpreter-core
    on Jan 24, 2016
  3. vadmium commented on Jan 27, 2016

    @vadmium
    Member

    According to PEP-492, “await” is still allowed to be an ordinary identifier in non-async functions until Python 3.7. Which means you are still allowed to write

    def oh_hai():
        await = "something"

    The proposed error message would need to be smart enough to distinguish the two cases.

  4. nchammas commented on Feb 2, 2016

    nchammasmannequin
    MannequinAuthor

    Related discussions about providing more helpful syntax error messages:

    From the discussion on bpo-1634034, it looks like providing better messages in the general case of a syntax error is quite difficult. But perhaps in limited cases like this one we can do better.

    Parsers are a bit over my head. Martin, is it difficult to distinguish between await as a regular name and await as a special token?

  5. vadmium commented on Feb 2, 2016

    @vadmium
    Member

    I’m not that familiar with the parser either. Maybe I remember a flag or something saying “we are inside an async def function”, which changes “await” from a user identifier into a reserved keyword. But I can’t imagine how that flag will help much with this proposal. The problem I think is “await” is never treated as a reserved keyword when the flag is not set.

  6. asvetlov commented on Dec 20, 2017

    @asvetlov
    Contributor

    Nothing to do.
    Python parser always report with such text on syntax error, there is nothing special for async/await

  7. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coretopic-asyncio

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions