Skip to content

Commit 400057b

Browse files
authored
fix: tomlkit 0.12.5 : Encoder contract interferes with external TypeErrors raised in encoders (#358)
Fixes #355 Signed-off-by: Frost Ming <[email protected]>
1 parent 22676f9 commit 400057b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
## [unreleased]
44

5+
### Changed
6+
7+
- Expect a tomlkit-specific error instead of `TypeError` from a custom encoder. ([#355](https://github.com/python-poetry/tomlkit/issues/355))
8+
59
### Fixed
610

711
- Fix the incompatiblity with 3.13 because of the `datetime.replace()` change. ([#333](https://github.com/python-poetry/tomlkit/issues/333))
812
- Revert the change of parsing out-of-order tables. ([#347](https://github.com/python-poetry/tomlkit/issues/347))
913

14+
1015
## [0.12.5] - 2024-05-08
1116

1217
### Fixed

tomlkit/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,10 @@ def __init__(self, value: str, invalid_sequences: Collection[str], delimiter: st
225225
f"Invalid string: {delimiter}{repr_}{delimiter}. "
226226
f"The character sequences {invalid_sequences} are invalid."
227227
)
228+
229+
230+
class ConvertError(TypeError, ValueError, TOMLKitError):
231+
"""Raised when item() fails to convert a value.
232+
It should be a TypeError, but due to historical reasons
233+
it needs to subclass ValueError as well.
234+
"""

tomlkit/items.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from tomlkit._types import wrap_method
3434
from tomlkit._utils import CONTROL_CHARS
3535
from tomlkit._utils import escape_string
36+
from tomlkit.exceptions import ConvertError
3637
from tomlkit.exceptions import InvalidStringError
3738

3839

@@ -46,13 +47,6 @@
4647
AT = TypeVar("AT", bound="AbstractTable")
4748

4849

49-
class _ConvertError(TypeError, ValueError):
50-
"""An internal error raised when item() fails to convert a value.
51-
It should be a TypeError, but due to historical reasons
52-
it needs to subclass ValueError as well.
53-
"""
54-
55-
5650
@overload
5751
def item(value: bool, _parent: Item | None = ..., _sort_keys: bool = ...) -> Bool: ...
5852

@@ -206,16 +200,16 @@ def item(value: Any, _parent: Item | None = None, _sort_keys: bool = False) -> I
206200
for encoder in CUSTOM_ENCODERS:
207201
try:
208202
rv = encoder(value)
209-
except TypeError:
203+
except ConvertError:
210204
pass
211205
else:
212206
if not isinstance(rv, Item):
213-
raise _ConvertError(
214-
f"Custom encoder returned {type(rv)}, not a subclass of Item"
207+
raise ConvertError(
208+
f"Custom encoder is expected to return an instance of Item, got {type(rv)}"
215209
)
216210
return rv
217211

218-
raise _ConvertError(f"Invalid type {type(value)}")
212+
raise ConvertError(f"Unable to convert an object of {type(value)} to a TOML item")
219213

220214

221215
class StringType(Enum):

0 commit comments

Comments
 (0)