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

out of order sections causing issues #196

Open
abn opened this issue May 25, 2022 · 5 comments
Open

out of order sections causing issues #196

abn opened this issue May 25, 2022 · 5 comments

Comments

@abn
Copy link
Member

abn commented May 25, 2022

When there are sections defined out of order, it seems tomlkit does not like it. Original issue was reported at python-poetry/poetry#4718.

The following code runs without issues, however does not modify the toml. Using content.value_insert_after() causes AttributeError: 'dict' object has no attribute '_insert_after'.

If we remove build-system from the toml, the code below raises AttributeError: 'Table' object has no attribute '_insert_after'. However using content.value succeeds with the right results.

reproducer.py

import tomlkit


TOML = tomlkit.parse("""\
[tool.poetry]
name = "foobar"
version = "0.1.0"
description = ""
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.plugins]
""")


if __name__ == '__main__':
    content = TOML["tool"]["poetry"]
    content._insert_after("dependencies", "group", tomlkit.table(is_super_table=True))
    content["group"]["one"] = tomlkit.table(is_super_table=True)
    content["group"]["one"]["dependencies"] = tomlkit.table()
    print(TOML.as_string())

@abn
Copy link
Member Author

abn commented May 25, 2022

Here is a test case that reproduces the issue.

def test_string_output_order_is_preserved_for_out_of_order_tables_with_insert_after():
    content_upper = """
[tool.poetry]
name = "foo"

[tool.poetry.dependencies]
python = "^3.6"
"""
    content_lower = """
[build-system]
requires = ["poetry-core"]
backend = "poetry.core.masonry.api"

[tool.poetry.build]
"""

    content_added = """
[tool.poetry.group.bar.dependencies]"""

    doc = parse(content_upper + content_lower)
    doc["tool"]["poetry"].value._insert_after(
        "dependencies", "group", tomlkit.table(True)
    )

    groups = doc["tool"]["poetry"]["group"]
    groups["bar"] = tomlkit.table(True)
    groups["bar"]["dependencies"] = tomlkit.table()

    assert doc.as_string() == content_upper + content_added + content_lower

@abn
Copy link
Member Author

abn commented May 27, 2022

@frostming any chance I can get a pointer on where to fix this? Happy to do the leg work.

@frostming
Copy link
Contributor

frostming commented May 30, 2022

You are trying to get the DOM element(A Container in tomlkit) of the table via .value while for a OutOfOrderTableProxy, the .value returns an intermediate helper Container that is created when building the proxy, hence any modification won't reflect to the original document.

To fix it, you need to make the modification on one of the underlying tables that are combined by the OutOfOrderTableProxy. See the example here. However, that needs to add those underscored methods to both OutOfOrderTableProxy, Table, and perhaps InlineTable.

@Kilo59
Copy link

Kilo59 commented Mar 31, 2024

I'm having trouble following the example here.
Is there any more documentation or examples on how to resolve these out of order problems?

I'm trying to update a tool.<TOOL_NAME> section but I can't seem to get it right.

Desired

[tool.ruff]
target-version = "py38"
line-length = 120
lint.select = ["F", "ASYNC", "RUFF", "B", "S", "PTH", "W", "E"]
lint.ignore = ["W191", "E111"]

Actual

[tool.ruff]
target-version = "py38"
line-length = 120
[tool.ruff.lint]
tool.ruff.lint.select = ["F", "ASYNC", "RUFF", "B", "S", "PTH", "W", "E"]
tool.ruff.lint.ignore = ["W191", "E111"]

@frostming
Copy link
Contributor

I'm having trouble following the example here.

Sorry, what code you are trying?

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

No branches or pull requests

3 participants