22
33from __future__ import annotations
44
5- import pydantic
6- from packaging .requirements import Requirement
7- from pydantic import TypeAdapter
5+ from typing import TYPE_CHECKING
6+
87from typing_extensions import assert_never
98
109from usethis ._backend .dispatch import get_backend
1716from usethis ._backend .uv .errors import UVDepGroupError
1817from usethis ._config import usethis_config
1918from usethis ._console import instruct_print , tick_print
19+ from usethis ._file .pyproject_toml .deps import (
20+ get_dep_groups as _get_dep_groups ,
21+ )
22+ from usethis ._file .pyproject_toml .deps import (
23+ get_project_deps as _get_project_deps ,
24+ )
25+ from usethis ._file .pyproject_toml .errors import PyprojectTOMLDepsError
2026from usethis ._file .pyproject_toml .io_ import PyprojectTOMLManager
2127from usethis ._types .backend import BackendEnum
22- from usethis ._types .deps import Dependency
2328from usethis .errors import DepGroupError
2429
30+ if TYPE_CHECKING :
31+ from usethis ._types .deps import Dependency
32+
2533
2634def get_project_deps () -> list [Dependency ]:
2735 """Get all project dependencies.
@@ -33,71 +41,16 @@ def get_project_deps() -> list[Dependency]:
3341 of the `pyproject.toml` file.
3442 """
3543 try :
36- pyproject = PyprojectTOMLManager ().get ()
37- except FileNotFoundError :
38- return []
39-
40- try :
41- project_section = pyproject ["project" ]
42- except KeyError :
43- return []
44-
45- if not isinstance (project_section , dict ):
46- return []
47-
48- try :
49- dep_section = project_section ["dependencies" ]
50- except KeyError :
51- return []
52-
53- try :
54- req_strs = TypeAdapter (list [str ]).validate_python (dep_section )
55- except pydantic .ValidationError as err :
56- msg = (
57- "Failed to parse the 'project.dependencies' section in 'pyproject.toml':\n "
58- f"{ err } \n \n "
59- "Please check the section and try again."
60- )
61- raise UVDepGroupError (msg ) from None
62-
63- reqs = [Requirement (req_str ) for req_str in req_strs ]
64- deps = [Dependency (name = req .name , extras = frozenset (req .extras )) for req in reqs ]
65- return deps
44+ return _get_project_deps ()
45+ except PyprojectTOMLDepsError as err :
46+ raise UVDepGroupError (str (err )) from None
6647
6748
6849def get_dep_groups () -> dict [str , list [Dependency ]]:
6950 try :
70- pyproject = PyprojectTOMLManager ().get ()
71- except FileNotFoundError :
72- return {}
73-
74- try :
75- dep_groups_section = pyproject ["dependency-groups" ]
76- except KeyError :
77- # In the past might have been in [tool.uv.dev-dependencies] section when using
78- # uv but this will be deprecated, so we don't support it in usethis.
79- return {}
80-
81- try :
82- req_strs_by_group = TypeAdapter (dict [str , list [str ]]).validate_python (
83- dep_groups_section
84- )
85- except pydantic .ValidationError as err :
86- msg = (
87- "Failed to parse the 'dependency-groups' section in 'pyproject.toml':\n "
88- f"{ err } \n \n "
89- "Please check the section and try again."
90- )
91- raise DepGroupError (msg ) from None
92- reqs_by_group = {
93- group : [Requirement (req_str ) for req_str in req_strs ]
94- for group , req_strs in req_strs_by_group .items ()
95- }
96- deps_by_group = {
97- group : [Dependency (name = req .name , extras = frozenset (req .extras )) for req in reqs ]
98- for group , reqs in reqs_by_group .items ()
99- }
100- return deps_by_group
51+ return _get_dep_groups ()
52+ except PyprojectTOMLDepsError as err :
53+ raise DepGroupError (str (err )) from None
10154
10255
10356def get_deps_from_group (group : str ) -> list [Dependency ]:
0 commit comments