summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Ganssle <paul@ganssle.io>2020-01-02 11:54:27 -0500
committerPaul Ganssle <paul@ganssle.io>2020-01-02 14:08:55 -0500
commitea7f441b455cabbae2bf98f2d10fd11724001155 (patch)
treee773eb33388f57dab41fd697deb517ff67ab47a5
parent21fe6e9669191b413e59cd9abd22610191575c1f (diff)
downloaddateutil-git-ea7f441b455cabbae2bf98f2d10fd11724001155.tar.gz
Fix custom repr for ParserError
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.
-rw-r--r--changelog.d/991.bugfix.rst1
-rw-r--r--dateutil/parser/_parser.py5
-rw-r--r--dateutil/test/test_parser.py9
3 files changed, 13 insertions, 2 deletions
diff --git a/changelog.d/991.bugfix.rst b/changelog.d/991.bugfix.rst
new file mode 100644
index 0000000..473082e
--- /dev/null
+++ b/changelog.d/991.bugfix.rst
@@ -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)
diff --git a/dateutil/parser/_parser.py b/dateutil/parser/_parser.py
index 7fcfa54..8d67584 100644
--- a/dateutil/parser/_parser.py
+++ b/dateutil/parser/_parser.py
@@ -1600,8 +1600,9 @@ class ParserError(ValueError):
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):
diff --git a/dateutil/test/test_parser.py b/dateutil/test/test_parser.py
index 605705e..cfa4bbb 100644
--- a/dateutil/test/test_parser.py
+++ b/dateutil/test/test_parser.py
@@ -943,3 +943,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 format is not guaranteed to be stable 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')"