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

document.pop(key) doesn't actually remove the item from the document #66

Closed
techalchemy opened this issue Dec 5, 2019 · 2 comments · Fixed by #122
Closed

document.pop(key) doesn't actually remove the item from the document #66

techalchemy opened this issue Dec 5, 2019 · 2 comments · Fixed by #122

Comments

@techalchemy
Copy link

I was in the process of updating usage of tomlkit over in requirementslib where I have a need to support compatibility with an old usage of sources keys in a Pipfile (we now just use the source key in an AoT). I was hoping to simply pop the key and reassign it from sources to its proper name, source, if I encounter it. However, pop does not remove the key from the table, so I wind up retaining the original key and also adding the new key. The original key is not valid for the schema so the document fails validation.

Here is an example document:

[[sources]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]
sphinx = "*"
requests = {extras = ["security"], version = "*"}

And for completeness:

>>> toml_data = """ 
... [[sources]] 
... url = "https://pypi.org/simple" 
... verify_ssl = true 
... name = "pypi" 
...  
... [packages] 
...  
... [dev-packages] 
... sphinx = "*" 
... requests = {extras = ["security"], version = "*"} 
... """
>>> data = tomlkit.loads(toml_data)
>>> data["sources"]
<AoT [{'url': 'https://pypi.org/simple', 'verify_ssl': True, 'name': 'pypi'}]>

>>> data["source"] = data.get("source", tomlkit.aot()) + data.pop("sources", tomlkit.aot())
>>> data["source"]
<AoT [{'name': 'pypi', 'url': 'https://pypi.org/simple', 'verify_ssl': True}]>

>>> data["sources"]
<AoT [{'url': 'https://pypi.org/simple', 'verify_ssl': True, 'name': 'pypi'}]>

>>> data._body
[(None, <Whitespace '\n'>), (<Key sources>, <AoT [{'url': 'https://pypi.org/simple', 'verify_ssl': True, 'name': 'pypi'}]>), (<Key packages>, {}), (<Key dev-packages>, {'sphinx': '*', 'requests': {'extras': ['security'], 'version': '*'}}), (<Key source>, <AoT [{'name': 'pypi', 'url': 'https://pypi.org/simple', 'verify_ssl': True}]>)]

So it's just effectively duplicating the key and not removing it. I also noticed that pop seems to just pop the _body value of the item and not the item itself (as compared with __getitem__ which returns the actual AoT in this case), so what I thought was an AoT instance was just a list.

Is this the expected behavior or should someone put together a patch?

@frostming
Copy link
Contributor

It is easy to add pop support, but i am thinking of introducing collections.abc.MutableMapping as base class to reduce overhead. dict can still be kept to not break isinstance() check. That may require a little refactor of Container and Table classes.

What do you think @sdispater

@frostming
Copy link
Contributor

For anyone who comes to this issue, I've created a fork of tomlkit v0.7.0 with the issue fixed, try it:

GitHub: https://github.com/frostming/atoml
PyPI: https://pypi.org/project/atoml

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