Skip to content

More helpful error message in JSON decoder #113047

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

Closed
wants to merge 2 commits into from
Closed

Conversation

kno10
Copy link

@kno10 kno10 commented Dec 13, 2023

While JavaScript typically parses the following example, it is not strictly valid JSON. But while the current Python error message Expecting property name enclosed in double quotes hints at a missing quote, the problem in fact is a trailing comma:

{
  "key": "value",
}

The new error message becomes:
Expecting a property name enclosed in double quotes (no trailing comma allowed)

I believe this trivial change does neither require a NEWS entry nor a separate issue, but I do not have experience if there are any tools that depend on the exact error message of the exception...

While JavaScript typically parses the following example, it is not strictly valid JSON. But while the current Python error message `Expecting property name enclosed in double quotes` hints at a missing quote, the problem in fact is a trailing comma:
```js
{
  "key": "value",
}
```
@ghost
Copy link

ghost commented Dec 13, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Dec 13, 2023

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@savannahostrowski savannahostrowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for filing this issue! I had a question but I believe @rhettinger may have thoughts here.

@@ -161,7 +161,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
return pairs, end + 1
elif nextchar != '"':
raise JSONDecodeError(
"Expecting property name enclosed in double quotes", s, end)
"Expecting a property name enclosed in double quotes (no trailing comma allowed)", s, end)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not have this be handled in a dedicated case with a more specific error message? It kind of seems like we might be burying the lede with the current error message if the trailing comma is the issue.

@serhiy-storchaka
Copy link
Member

It is not so simple.

  1. Intentional changes in error messages require tests. Otherwise we cannot be sure that they will not regress.
  2. There are two implementations: C and Python. Both should produce the same error messages if possible.
  3. It will have a lot of false positives. It should check that it is the trailing comma (i.e. that it follows a comma and is followed by the correct ] or }), otherwise it can only increase confusion.
  4. Any non-trivial change should have an open issue for discussion.

@bedevere-app
Copy link

bedevere-app bot commented Dec 14, 2023

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@kno10 kno10 closed this Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants