Skip to content

Commit

Permalink
Fix custom repr for ParserError
Browse files Browse the repository at this point in the history
This was originally dead code, because an indentation error had `__repr__`
defined after the `return` statement in `__str__`. The definition of the
`__repr__` is also changed to be more in line with the conception of a
repr as "what you would have to evalue to get this object".

Even though this is not guaranteed behavior, this commit also adds a
regression test to avoid simple errors like this in the future.
  • Loading branch information
pganssle committed Jan 2, 2020
1 parent f6c6d5c commit 2a36bd6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/991.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the custom ``repr`` for ``dateutil.parser.ParserError``, which was not defined due to an indentation error. (gh issue #991, gh pr #993)
5 changes: 3 additions & 2 deletions dateutil/parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,8 +1600,9 @@ def __str__(self):
except (TypeError, IndexError):
return super(ParserError, self).__str__()

def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, str(self))
def __repr__(self):
args = ", ".join("'%s'" % arg for arg in self.args)
return "%s(%s)" % (self.__class__.__name__, args)


class UnknownTimezoneWarning(RuntimeWarning):
Expand Down
9 changes: 9 additions & 0 deletions dateutil/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,12 @@ def test_decimal_error(value):
# when constructed with an invalid value
with pytest.raises(ParserError):
parse(value)

def test_parsererror_repr():
# GH 991 - the __repr__ was not properly indented and so was never defined;
# This tests the current behavior of the ParserError __repr__, but the
# precise future form should not be guaranteed and may change even in
# minor versions. This test exists to avoid regressions.
s = repr(ParserError("Problem with string: %s", "2019-01-01"))

assert s == "ParserError('Problem with string: %s', '2019-01-01')"

0 comments on commit 2a36bd6

Please sign in to comment.