Skip to content

Docs: str() of a SyntaxError is not only the error message, and SyntaxError.msg is not documented #162603

In review

Description

@Robertboy18

Documentation

The SyntaxError entry (https://docs.python.org/dev/builtins/exceptions.html#SyntaxError) says "The str() of the exception instance returns only the error message." For a SyntaxError with location details, str() adds the file name and the line number:

try:
    compile("1 +", "f.py", "exec")
except SyntaxError as e:
    print(repr(str(e)))
    print(repr(e.msg))

CPython 3.15.2 and main (639c34b):

'invalid syntax (f.py, line 1)'
'invalid syntax'

The message alone is the msg attribute, which the entry doesn't list among the attributes. I found it while writing a Lean spec of Python.

Linked PRs

Activity

  1. added
    AI-needs-explanation
    on Nov 25, 2026
  2. bedevere-app commented on Nov 25, 2026

    @bedevere-app

    Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

    This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.

    Please read our guidelines for using AI tools.

  3. added
    docs
    and removed
    AI-needs-explanation
    on Nov 25, 2026
  4. moved this from Todo to In Progress in docs issueson Nov 26, 2026
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

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions