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

refactor: switch to *Python*'s official tomllib, as toml cannot parse inline table values properly #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stevenxxiu
Copy link

Thanks for this library.

I use Prospector often, which makes use of this library. In Prospector I often use inline tables, like:

[tool.pdm.scripts]
post_install = {shell = '''
    mv "${PDM_PROJECT_ROOT}/__pypackages__" "${PDM_PROJECT_ROOT}/nushell/";
    ln --symbolic "${PDM_PROJECT_ROOT}/nushell/__pypackages__" "${PDM_PROJECT_ROOT}"
'''}

This cannot be parsed by the toml library, which this library uses. As you can see in https://github.com/uiri/toml/blob/0.10.2/toml/decoder.py#L654:

def load_inline_object(self, line, currentlevel, multikey=False,
                       multibackslash=False):
    candidate_groups = line[1:-1].split(",")
    # ...

The parsing is really basic and is clearly going to tail. It assumes inline tables are all in one line.

This file was last updated 2020/10/30 so seems rather dead.

This bug is also mentioned at uiri/toml#348, quite some time ago.

Python now has a native Toml library, so it makes sense to switch to that. The API seems quite compatible, as I only had to switch the library, and all tests still pass.

@@ -2,7 +2,7 @@
from pathlib import Path
from typing import List, Union

import toml
import tomllib
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I just arrived here from encountering and then wanting to solve the same problem 😄 I see you got here first, but your solution is not quite right.

tomllib is only available in the standard library for python >=3.11.

Since this project declares compatibility with python >=3.7, you also need to add a dependency on tomli. It's exactly the same package as tomlib for python <3.11

You'll need to do something like this for full compatibility: https://github.com/nat-n/poethepoet/blob/903ed9af0dd313a6da32f24ec4357101435976d8/poethepoet/config.py#L5

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 this pull request may close these issues.

2 participants