1
1
from typing import Union
2
2
3
3
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
5
5
from pipenv .vendor .tomlkit .items import AoT , Array , Bool , InlineTable , Item , String , Table
6
+ from pipenv .vendor .tomlkit .toml_document import TOMLDocument
6
7
7
8
try :
8
9
import tomllib as toml
@@ -33,26 +34,32 @@ def cleanup_toml(tml):
33
34
return toml
34
35
35
36
36
- def convert_toml_outline_tables (parsed , project ):
37
+ def convert_toml_outline_tables (parsed : TOMLDocument , project ) -> TOMLDocument :
37
38
"""Converts all outline tables to inline tables."""
38
39
39
40
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 ):
42
43
body = section .value ._body
43
- elif isinstance (section , tomlkit . container . OutOfOrderTableProxy ):
44
+ elif isinstance (section , OutOfOrderTableProxy ):
44
45
body = section ._internal_container ._body
45
46
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 :
48
52
if not key :
49
53
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 ):
53
55
table = tomlkit .inline_table ()
54
56
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
+
56
63
return result
57
64
58
65
def convert_toml_table (section ):
@@ -66,10 +73,10 @@ def convert_toml_table(section):
66
73
result [package ] = table
67
74
return result
68
75
69
- is_tomlkit_parsed = isinstance (parsed , tomlkit . container . Container )
76
+ is_tomlkit_parsed = isinstance (parsed , Container )
70
77
for section in project .get_package_categories ():
71
78
table_data = parsed .get (section , {})
72
- if not table_data :
79
+ if table_data is None :
73
80
continue
74
81
if is_tomlkit_parsed :
75
82
result = convert_tomlkit_table (table_data )
0 commit comments