Skip to content

Commit 29b5323

Browse files
authored
gh-126175: Add attributes to TOMLDecodeError. Deprecate free-form __init__ args (GH-126428)
1 parent a12690e commit 29b5323

File tree

4 files changed

+163
-52
lines changed

4 files changed

+163
-52
lines changed

Doc/library/tomllib.rst

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,36 @@ This module defines the following functions:
6060

6161
The following exceptions are available:
6262

63-
.. exception:: TOMLDecodeError
63+
.. exception:: TOMLDecodeError(msg, doc, pos)
6464

65-
Subclass of :exc:`ValueError`.
65+
Subclass of :exc:`ValueError` with the following additional attributes:
66+
67+
.. attribute:: msg
68+
69+
The unformatted error message.
70+
71+
.. attribute:: doc
72+
73+
The TOML document being parsed.
74+
75+
.. attribute:: pos
76+
77+
The index of *doc* where parsing failed.
78+
79+
.. attribute:: lineno
80+
81+
The line corresponding to *pos*.
82+
83+
.. attribute:: colno
84+
85+
The column corresponding to *pos*.
86+
87+
.. versionchanged:: next
88+
Added the *msg*, *doc* and *pos* parameters.
89+
Added the :attr:`msg`, :attr:`doc`, :attr:`pos`, :attr:`lineno` and :attr:`colno` attributes.
90+
91+
.. deprecated:: next
92+
Passing free-form positional arguments is deprecated.
6693

6794

6895
Examples

Lib/test/test_tomllib/test_error.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def test_type_error(self):
4949
self.assertEqual(str(exc_info.exception), "Expected str object, not 'bool'")
5050

5151
def test_module_name(self):
52-
self.assertEqual(tomllib.TOMLDecodeError().__module__, tomllib.__name__)
52+
self.assertEqual(
53+
tomllib.TOMLDecodeError("", "", 0).__module__, tomllib.__name__
54+
)
5355

5456
def test_invalid_parse_float(self):
5557
def dict_returner(s: str) -> dict:
@@ -64,3 +66,33 @@ def list_returner(s: str) -> list:
6466
self.assertEqual(
6567
str(exc_info.exception), "parse_float must not return dicts or lists"
6668
)
69+
70+
def test_deprecated_tomldecodeerror(self):
71+
for args in [
72+
(),
73+
("err msg",),
74+
(None,),
75+
(None, "doc"),
76+
("err msg", None),
77+
(None, "doc", None),
78+
("err msg", "doc", None),
79+
("one", "two", "three", "four"),
80+
("one", "two", 3, "four", "five"),
81+
]:
82+
with self.assertWarns(DeprecationWarning):
83+
e = tomllib.TOMLDecodeError(*args) # type: ignore[arg-type]
84+
self.assertEqual(e.args, args)
85+
86+
def test_tomldecodeerror(self):
87+
msg = "error parsing"
88+
doc = "v=1\n[table]\nv='val'"
89+
pos = 13
90+
formatted_msg = "error parsing (at line 3, column 2)"
91+
e = tomllib.TOMLDecodeError(msg, doc, pos)
92+
self.assertEqual(e.args, (formatted_msg,))
93+
self.assertEqual(str(e), formatted_msg)
94+
self.assertEqual(e.msg, msg)
95+
self.assertEqual(e.doc, doc)
96+
self.assertEqual(e.pos, pos)
97+
self.assertEqual(e.lineno, 3)
98+
self.assertEqual(e.colno, 2)

0 commit comments

Comments
 (0)