From d74372b71f1dac754cea57da77052928c49ed81c Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Fri, 31 May 2024 21:23:57 +0800 Subject: [PATCH 1/2] fix: mypy errors when using tomlkit to modify toml document --- tomlkit/items.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tomlkit/items.py b/tomlkit/items.py index 661e09c..0ce6b3c 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -521,6 +521,15 @@ def __reduce__(self): def __reduce_ex__(self, protocol): return self.__class__, self._getstate(protocol) + # Leave it here to improve type hint for `tomlkit.parse` + # For example: `tomlkit.parse('pyproject.toml')['tool']['poetry']` + def __getitem__(self, key) -> Any: + raise NotImplementedError() + + # Improve type hint for document modify, ex: `doc['tool']['poetry']={}` + def __setitem__(self, key, value) -> None: + raise NotImplementedError() + class Whitespace(Item): """ From 69ea60629bbc320209a30471b6b55c78bc5d8b1e Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Fri, 31 May 2024 22:15:31 +0800 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 1 + tomlkit/items.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 833c2dc..2e1a922 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixed +- Fix mypy errors when using tomlkit to modify toml document. ([#326](https://github.com/python-poetry/tomlkit/issues/326)) - 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)) diff --git a/tomlkit/items.py b/tomlkit/items.py index 0ce6b3c..452aec7 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -522,7 +522,9 @@ def __reduce_ex__(self, protocol): return self.__class__, self._getstate(protocol) # Leave it here to improve type hint for `tomlkit.parse` - # For example: `tomlkit.parse('pyproject.toml')['tool']['poetry']` + # For example: + # doc = tomlkit.parse(Path('pyproject.toml').read_text()) + # print(doc['tool']['poetry']) def __getitem__(self, key) -> Any: raise NotImplementedError()