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

MyPy errors when using tomlkit to modify toml documents #326

Open
florian-cl opened this issue Dec 13, 2023 · 1 comment
Open

MyPy errors when using tomlkit to modify toml documents #326

florian-cl opened this issue Dec 13, 2023 · 1 comment

Comments

@florian-cl
Copy link

When using tomlkit to modify a tomldocument, mypy throws a bunch of errors that I don't know how to fix.
Example:

import tomlkit
from pathlib import Path

path = Path("data.toml")

path.write_text("""
[foo]
bar = 'baz'
[foo.bla]
blub = 'blib'
""")

data = tomlkit.parse(path.read_text(encoding="utf-8"))
data["foo"]["bar"] = "quux"
data["foo"]["bla"]["blub"] = "bleb"
path.write_bytes(tomlkit.dumps(data).encode("utf-8"))

Output

tomlkit_issue.py:14: error: Unsupported target for indexed assignment ("Item | Container")  [index]
tomlkit_issue.py:15: error: Value of type "Item | Container" is not indexable  [index]
tomlkit_issue.py:15: error: Unsupported target for indexed assignment ("Any | Item | Container")  [index]
Found 3 errors in 1 file (checked 1 source file)

Is there any fix for this?
Any help is greatly appreciated :)

@dimbleby
Copy link

dimbleby commented Jan 1, 2024

IMO the types in tomlkit are more or less unusable: per this issue they generate a lot of noise and no help on the code that you want to write.

One approach to working around that goes something like this:

# pretend that we're just dealing with a regular dictionary
data: dict[str, Any] = tomlkit.parse(whatever)
...

# later cast back to the tomlkit type if some API requires it
data = cast("TOMLDocument", data)

which works: but of course it is just ceremony to suppress unhelpful errors, and abandons any potential benefits if the typing were more helpful.

For me, the current state of the typing is such that it would be better not to show it to users (ie remove py.typed).

If anyone cares to make the typing better, then of course that's better! Fixing the warnings and errors that mypy generates on this project and its unit tests would be a sensible way of going about that, though there is probably quite a lot of work - and maybe some of it is not easy - to get that done:

$ mypy --strict tomlkit tests
...
Found 815 errors in 23 files (checked 26 source files)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants