Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tomlkit 0.12.5 : Encoder contract interferes with external TypeErrors raised in encoders #358

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

## [unreleased]

### Changed

- Expect a tomlkit-specific error instead of `TypeError` from a custom encoder. ([#355](https://github.com/python-poetry/tomlkit/issues/355))

### Fixed

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


## [0.12.5] - 2024-05-08

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions tomlkit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,10 @@ def __init__(self, value: str, invalid_sequences: Collection[str], delimiter: st
f"Invalid string: {delimiter}{repr_}{delimiter}. "
f"The character sequences {invalid_sequences} are invalid."
)


class ConvertError(TypeError, ValueError, TOMLKitError):
"""Raised when item() fails to convert a value.
It should be a TypeError, but due to historical reasons
it needs to subclass ValueError as well.
"""
16 changes: 5 additions & 11 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from tomlkit._types import wrap_method
from tomlkit._utils import CONTROL_CHARS
from tomlkit._utils import escape_string
from tomlkit.exceptions import ConvertError
from tomlkit.exceptions import InvalidStringError


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


class _ConvertError(TypeError, ValueError):
"""An internal error raised when item() fails to convert a value.
It should be a TypeError, but due to historical reasons
it needs to subclass ValueError as well.
"""


@overload
def item(value: bool, _parent: Item | None = ..., _sort_keys: bool = ...) -> Bool: ...

Expand Down Expand Up @@ -206,16 +200,16 @@ def item(value: Any, _parent: Item | None = None, _sort_keys: bool = False) -> I
for encoder in CUSTOM_ENCODERS:
try:
rv = encoder(value)
except TypeError:
except ConvertError:
pass
else:
if not isinstance(rv, Item):
raise _ConvertError(
f"Custom encoder returned {type(rv)}, not a subclass of Item"
raise ConvertError(
f"Custom encoder is expected to return an instance of Item, got {type(rv)}"
)
return rv

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


class StringType(Enum):
Expand Down
Loading