Skip to content

parse('') raises ValueError although parse() documents it never raises #153

Closed

Description

@rahul05ranjan

parse() documents that it never raises ValueError and always returns errors in the tuple, but the empty-string path raises instead:

Docstring (current main):

Note that this does not raise a ValueError; instead any error is returned as the second value in the tuple.

Repro:

import json5

json5.loads('')  # ValueError — documented and fine

json5.parse('')  # ValueError: Empty strings are not legal JSON5
json5.parse('   ', consume_trailing=False)  # returns (None, '<string>:1 ...', 3) as documented

So callers that follow the docstring and use the tuple result get an exception for one specific input, while every other malformed input ('x', '{', '}', whitespace-only, …) comes back as (None, err, pos) as promised.

Fix would be one line in parse():

if not s:
    return None, 'Empty strings are not legal JSON5', 0

(or keep the raise and adjust the docstring). The behavior snuck past the docstring's "Raises:" section too, which only lists UnicodeDecodeError.

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions