1212from packaging .utils import canonicalize_name
1313from poetry .core .packages .dependency import Dependency
1414from poetry .core .packages .dependency_group import MAIN_GROUP
15- from tomlkit .toml_document import TOMLDocument
1615
1716from poetry .console .commands .init import InitCommand
1817from poetry .console .commands .installer_command import InstallerCommand
@@ -126,10 +125,8 @@ class AddCommand(InstallerCommand, InitCommand):
126125
127126 def handle (self ) -> int :
128127 from poetry .core .constraints .version import parse_constraint
129- from tomlkit import array
130- from tomlkit import inline_table
131- from tomlkit import nl
132- from tomlkit import table
128+ from tomlrt import Array
129+ from tomlrt import Table
133130
134131 from poetry .factory import Factory
135132
@@ -148,11 +145,9 @@ def handle(self) -> int:
148145 if optional and group != MAIN_GROUP :
149146 raise ValueError ("You can only add optional dependencies to the main group" )
150147
151- # tomlkit types are awkward to work with, treat content as a mostly untyped
152- # dictionary.
153- content : dict [str , Any ] = self .poetry .file .read ()
154- project_content = content .get ("project" , table ())
155- poetry_content = content .get ("tool" , {}).get ("poetry" , table ())
148+ content = self .poetry .file .read ()
149+ project_content = content .get ("project" , Table .section ())
150+ poetry_content = content .get ("tool" , {}).get ("poetry" , Table .section ())
156151 groups_content = content .get ("dependency-groups" , {})
157152 project_name = (
158153 canonicalize_name (name )
@@ -174,25 +169,24 @@ def handle(self) -> int:
174169 if optional :
175170 project_section = project_content .get (
176171 "optional-dependencies" , {}
177- ).get (optional , array ())
172+ ).get (optional , Array ())
178173 else :
179- project_section = project_content .get ("dependencies" , array ())
174+ project_section = project_content .get ("dependencies" , Array ())
180175 project_dependency_names = [
181176 Dependency .create_from_pep_508 (dep ).name for dep in project_section
182177 ]
183178 else :
184- project_section = array ()
179+ project_section = Array ()
185180
186- poetry_section = poetry_content .get ("dependencies" , table ())
181+ poetry_section = poetry_content .get ("dependencies" , Table . section ())
187182
188183 # Dependency Groups
189184 else :
190185 if groups_content or "group" not in poetry_content :
191186 use_groups_section = True
192187 if not groups_content :
193- groups_content = table (is_super_table = True )
194- if group not in groups_content :
195- groups_content [group ] = array ("[\n ]" )
188+ groups_content = Table .section ()
189+ groups_content .setdefault (group , Array (multiline = True ))
196190
197191 project_dependency_names = [
198192 Dependency .create_from_pep_508 (dep ).name
@@ -207,7 +201,7 @@ def handle(self) -> int:
207201 poetry_section = (
208202 poetry_content .get ("group" , {})
209203 .get (group , {})
210- .get ("dependencies" , table ())
204+ .get ("dependencies" , Table . section ())
211205 )
212206 project_section = []
213207
@@ -244,7 +238,7 @@ def handle(self) -> int:
244238 assert isinstance (version , str )
245239 parse_constraint (version )
246240
247- constraint : dict [str , Any ] = inline_table ()
241+ constraint : dict [str , Any ] = Table . inline ()
248242 for key , value in _constraint .items ():
249243 if key == "name" :
250244 continue
@@ -331,7 +325,7 @@ def handle(self) -> int:
331325
332326 # create a second constraint for tool.poetry.dependencies with keys
333327 # that cannot be stored in the project section
334- poetry_constraint : dict [str , Any ] = inline_table ()
328+ poetry_constraint : dict [str , Any ] = Table . inline ()
335329 if not isinstance (constraint , str ):
336330 for key in ["allow-prereleases" , "develop" , "source" ]:
337331 if value := constraint .get (key ):
@@ -363,38 +357,23 @@ def handle(self) -> int:
363357 if project_section :
364358 assert group == MAIN_GROUP
365359 if optional :
366- if "optional-dependencies" not in project_content :
367- project_content ["optional-dependencies" ] = table ()
368- if optional not in project_content ["optional-dependencies" ]:
369- project_content ["optional-dependencies" ][optional ] = project_section
360+ opt_deps_table = project_content .ensure_table ("optional-dependencies" )
361+ opt_deps_table .setdefault (optional , project_section )
370362 elif "dependencies" not in project_content :
371363 project_content ["dependencies" ] = project_section
372364
373365 if poetry_section :
374- if "tool" not in content :
375- content ["tool" ] = table ()
376- if "poetry" not in content ["tool" ]:
377- content ["tool" ]["poetry" ] = poetry_content
366+ tool_table = content .ensure_table ("tool" )
367+ tool_table .setdefault ("poetry" , poetry_content )
368+
378369 if group == MAIN_GROUP :
379- if "dependencies" not in poetry_content :
380- poetry_content ["dependencies" ] = poetry_section
370+ poetry_content .setdefault ("dependencies" , poetry_section )
381371 else :
382- if "group" not in poetry_content :
383- poetry_content ["group" ] = table (is_super_table = True )
384-
385- groups = poetry_content ["group" ]
386-
387- if group not in groups :
388- groups [group ] = table ()
389- groups .add (nl ())
390-
391- if "dependencies" not in groups [group ]:
392- groups [group ]["dependencies" ] = poetry_section
372+ group_table = poetry_content .ensure_table (("group" , group ))
373+ group_table .setdefault ("dependencies" , poetry_section )
393374
394375 if groups_content and group != MAIN_GROUP :
395- if "dependency-groups" not in content :
396- content ["dependency-groups" ] = table ()
397- content ["dependency-groups" ][group ] = groups_content [group ]
376+ content .install (("dependency-groups" , group ), groups_content [group ])
398377
399378 self .poetry .locker .set_pyproject_data (content )
400379 self .installer .set_locker (self .poetry .locker )
@@ -413,7 +392,6 @@ def handle(self) -> int:
413392 status = self .installer .run ()
414393
415394 if status == 0 and not self .option ("dry-run" ):
416- assert isinstance (content , TOMLDocument )
417395 self .poetry .file .write (content )
418396
419397 return status
0 commit comments