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

Deleting a table creates invalid output #204

Closed
codejedi365 opened this issue Jul 3, 2022 · 0 comments · Fixed by #206
Closed

Deleting a table creates invalid output #204

codejedi365 opened this issue Jul 3, 2022 · 0 comments · Fixed by #206
Labels
bug Something isn't working

Comments

@codejedi365
Copy link

codejedi365 commented Jul 3, 2022

As specified on the quickstart.rst, one can use the del command to remove a table from a TOMLDocument. This has a faulty implementation when dealing with multi-tier tables. The internal data is deleted and the JSON result is as expected but the TOMLDocument will not render correctly afterword. The resulting TOML does not combine keys and will create 2 tables with the same name which causes TOML Parser Errors on re-read. The bug is more obscure than I first thought as it will not render 2 table headings if the immediate parent/previous table header is the same. However, if the previous table is not the same section, it will re-render the header rather than checking the entire TOMLDocument for the parent header.

Example

The example is here to illustrate an incorrect result and not for debate on if this should be done or not.

Imported pyproject.toml

[project]
name = "examplepkg"

[build-system]
requires = [
    "setuptools",
    "wheel"
]
build-backend = "setuptools.build_meta"

[project.optional-dependencies]
dev = [
    "python-semantic-release"
]

Example code which creates invalid output

"""Script to strip optional-dependencies from pyproject configuration."""
import json, tomlkit

with open('pyproject.toml', 'rb') as fd:
    conf = tomlkit.load(fd)

if conf.get('project', {}).get('optional-dependencies', None) is not None:
    del conf['project']['optional-dependencies']

print(json.dumps(conf, indent=2))
tomlstr = tomlkit.dumps(conf)
print(tomlstr)

# attempt reloading
prod_conf = tomlkit.loads(tomlstr)
# Raises tomlkit.exceptions.KeyAlreadyPresent: Key "project" already exists.

Expected Result

JSON output (expected)

{
  "project": {
    "name": "exampleprj"
  },
  "build-system": {
    "requires": [
      "setuptools",
      "wheel"
    ],
    "build-backend": "setuptools.build_meta"
  }
}

TOML output (expected)

[project]
name = "examplepkg"

[build-system]
requires = [
    "setuptools",
    "wheel"
]
build-backend = "setuptools.build_meta"

Actual Result

JSON output (actual)
Result: Matches Expected Result

TOML output (actual)
Result: Failure

[project]
name = "examplepkg"

[build-system]
requires = [
    "setuptools",
    "wheel"
]
build-backend = "setuptools.build_meta"

[project]

As you can see the toml generated actually outputs 2 [project] sections as it dropped the tiered section and the internals but the result causes the pyproject.toml to be invalid because of 2 defined project sections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants