Skip to content

Commit 472b118

Browse files
Naofal-Helaloz123
authored andcommitted
Fix inline table conversion
the function responsible for converting outline tables to inline was faulty.
1 parent d983c19 commit 472b118

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

pipenv/project.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from urllib.parse import unquote, urljoin
1616

1717
from pipenv.utils.constants import VCS_LIST
18+
from pipenv.vendor.tomlkit.items import SingleKey, Table
1819

1920
try:
2021
import tomllib as toml
@@ -1107,12 +1108,23 @@ def get_package_name_in_pipfile(self, package_name, category):
11071108
return name
11081109
return None
11091110

1110-
def _sort_category(self, category):
1111-
# toml tables won't maintain sorted dictionary order
1112-
# so construct the table in the order that we need
1111+
def _sort_category(self, category) -> Table:
1112+
# copy table or create table from dict-like object
11131113
table = tomlkit.table()
1114-
for key, value in sorted(category.items()):
1115-
table.add(key, value)
1114+
if isinstance(category, Table):
1115+
table.update(category.value)
1116+
else:
1117+
table.update(category)
1118+
1119+
# sort the table internally
1120+
table._value._body.sort(key=lambda t: t[0] and t[0].key or "")
1121+
for index, (key, _) in enumerate(table._value._body):
1122+
assert isinstance(key, SingleKey)
1123+
indices = table._value._map[key]
1124+
if isinstance(indices, tuple):
1125+
table._value._map[key] = (index,) + indices[1:]
1126+
else:
1127+
table._value._map[key] = index
11161128

11171129
return table
11181130

pipenv/utils/toml.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from typing import Union
22

33
from pipenv.vendor.plette.models import Package, PackageCollection
4-
from pipenv.vendor.tomlkit.container import Container
4+
from pipenv.vendor.tomlkit.container import Container, OutOfOrderTableProxy
55
from pipenv.vendor.tomlkit.items import AoT, Array, Bool, InlineTable, Item, String, Table
6+
from pipenv.vendor.tomlkit.toml_document import TOMLDocument
67

78
try:
89
import tomllib as toml
@@ -33,26 +34,32 @@ def cleanup_toml(tml):
3334
return toml
3435

3536

36-
def convert_toml_outline_tables(parsed, project):
37+
def convert_toml_outline_tables(parsed: TOMLDocument, project) -> TOMLDocument:
3738
"""Converts all outline tables to inline tables."""
3839

3940
def convert_tomlkit_table(section):
40-
result = section.copy()
41-
if isinstance(section, tomlkit.items.Table):
41+
result: Table = tomlkit.table()
42+
if isinstance(section, Table):
4243
body = section.value._body
43-
elif isinstance(section, tomlkit.container.OutOfOrderTableProxy):
44+
elif isinstance(section, OutOfOrderTableProxy):
4445
body = section._internal_container._body
4546
else:
46-
body = section._body
47-
for index, (key, value) in enumerate(body):
47+
assert not hasattr(section, "_body")
48+
body = section
49+
50+
index: int = 0
51+
for key, value in body:
4852
if not key:
4953
continue
50-
if hasattr(value, "keys") and not isinstance(
51-
value, tomlkit.items.InlineTable
52-
):
54+
if hasattr(value, "keys") and not isinstance(value, InlineTable):
5355
table = tomlkit.inline_table()
5456
table.update(value.value)
55-
result.value._body[index] = (key, table.value)
57+
key.sep = " = " # add separator because it did not exist before
58+
result.append(key, table)
59+
else:
60+
result.append(key, value)
61+
index += 1
62+
5663
return result
5764

5865
def convert_toml_table(section):
@@ -66,10 +73,10 @@ def convert_toml_table(section):
6673
result[package] = table
6774
return result
6875

69-
is_tomlkit_parsed = isinstance(parsed, tomlkit.container.Container)
76+
is_tomlkit_parsed = isinstance(parsed, Container)
7077
for section in project.get_package_categories():
7178
table_data = parsed.get(section, {})
72-
if not table_data:
79+
if table_data is None:
7380
continue
7481
if is_tomlkit_parsed:
7582
result = convert_tomlkit_table(table_data)

0 commit comments

Comments
 (0)