Skip to content

Commit b0aaaa9

Browse files
committed
Add parser for [dependency-groups]
1 parent a1136d4 commit b0aaaa9

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

pyproject_parser/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@
6363

6464
# this package
6565
from pyproject_parser.classes import License, Readme, _NormalisedName
66-
from pyproject_parser.parsers import BuildSystemParser, PEP621Parser
66+
from pyproject_parser.parsers import BuildSystemParser, DependencyGroupsParser, PEP621Parser
6767
from pyproject_parser.type_hints import ( # noqa: F401
6868
Author,
6969
BuildSystemDict,
7070
ContentTypes,
71+
DependencyGroupsDict,
7172
ProjectDict,
7273
_PyProjectAsTomlDict
7374
)
@@ -256,6 +257,9 @@ class PyProject:
256257
#: Represents the :pep:`build-system table <518#build-system-table>` defined in :pep:`517` and :pep:`518`.
257258
build_system: Optional[BuildSystemDict] = attr.ib(default=None)
258259

260+
#: Represents the :pep:`dependency groups table <735#specification>` defined in :pep:`735`.
261+
dependency_groups: Optional[DependencyGroupsDict] = attr.ib(default=None)
262+
259263
#: Represents the :pep621:`project table <table-name>` defined in :pep:`621`.
260264
project: Optional[ProjectDict] = attr.ib(default=None)
261265

@@ -268,6 +272,12 @@ class PyProject:
268272
to parse the :pep:`build-system table <518#build-system-table>` with.
269273
"""
270274

275+
dependency_groups_table_parser: ClassVar[DependencyGroupsParser] = DependencyGroupsParser()
276+
"""
277+
The :class:`~dom_toml.parser.AbstractConfigParser`
278+
to parse the :pep:`dependency groups table <735#specification>` with.
279+
"""
280+
271281
project_table_parser: ClassVar[PEP621Parser] = PEP621Parser()
272282
"""
273283
The :class:`~dom_toml.parser.AbstractConfigParser`
@@ -323,6 +333,12 @@ def load(
323333
)
324334
keys.remove("build-system")
325335

336+
if "dependency-groups" in config:
337+
dependency_groups_table = cls.dependency_groups_table_parser.parse(
338+
config["dependency-groups"], set_defaults=set_defaults
339+
)
340+
keys.remove("dependency-groups")
341+
326342
if "project" in config:
327343
project_table = cls.project_table_parser.parse(config["project"], set_defaults=set_defaults)
328344
keys.remove("project")
@@ -336,7 +352,7 @@ def load(
336352
tool_table[tool_name] = cls.tool_parsers[tool_name].parse(tool_subtable)
337353

338354
if keys:
339-
allowed_top_level = ("build-system", "project", "tool")
355+
allowed_top_level = ("build-system", "dependency-groups", "project", "tool")
340356

341357
for top_level_key in sorted(keys):
342358
if top_level_key in allowed_top_level:
@@ -355,6 +371,7 @@ def load(
355371

356372
return cls(
357373
build_system=build_system_table,
374+
dependency_groups=dependency_groups_table,
358375
project=project_table,
359376
tool=tool_table,
360377
)

pyproject_parser/parsers.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
# this package
5151
from pyproject_parser.classes import License, Readme, _NormalisedName
52-
from pyproject_parser.type_hints import Author, BuildSystemDict, ProjectDict
52+
from pyproject_parser.type_hints import Author, BuildSystemDict, DependencyGroupsDict, ProjectDict
5353
from pyproject_parser.utils import PyProjectDeprecationWarning, content_type_from_filename, render_readme
5454

5555
__all__ = [
@@ -258,6 +258,60 @@ def parse( # type: ignore[override]
258258
return cast(BuildSystemDict, parsed_config)
259259

260260

261+
class DependencyGroupsParser(AbstractConfigParser):
262+
"""
263+
Parser for the :pep:`dependency groups table <735#specification>` table from ``pyproject.toml``.
264+
265+
.. autosummary-widths:: 17/32
266+
""" # noqa: RST399
267+
268+
table_name: ClassVar[str] = "dependency-groups"
269+
270+
@property
271+
def keys(self) -> List[str]:
272+
"""
273+
The keys to parse from the TOML file.
274+
"""
275+
276+
return []
277+
278+
@staticmethod
279+
def parse_group(config: TOML_TYPES) -> List[Union[str, Dict[str, str]]]:
280+
"""
281+
Parse a group from the TOML configuration.
282+
283+
:param config:
284+
"""
285+
286+
if isinstance(config, list):
287+
return config
288+
289+
raise BadConfigError("A group must be a list.")
290+
291+
def parse( # type: ignore[override]
292+
self,
293+
config: Dict[str, TOML_TYPES],
294+
set_defaults: bool = False,
295+
) -> BuildSystemDict:
296+
"""
297+
Parse the TOML configuration.
298+
299+
:param config:
300+
:param set_defaults: If :py:obj:`True`, the values in
301+
:attr:`self.defaults <dom_toml.parser.AbstractConfigParser.defaults>` and
302+
:attr:`self.factories <dom_toml.parser.AbstractConfigParser.factories>`
303+
will be set as defaults for the returned mapping.
304+
305+
:rtype:
306+
307+
.. latex:clearpage::
308+
"""
309+
310+
parsed_config = {key: self.parse_group(value) for key, value in config.items()}
311+
312+
return cast(DependencyGroupsDict, parsed_config)
313+
314+
261315
class PEP621Parser(RequiredKeysConfigParser):
262316
"""
263317
Parser for :pep:`621` metadata from ``pyproject.toml``.

pyproject_parser/type_hints.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#
2828

2929
# stdlib
30-
from typing import Any, Dict, List, Optional
30+
from typing import Any, Dict, List, Optional, Union
3131

3232
# 3rd party
3333
from packaging.markers import Marker
@@ -57,6 +57,16 @@
5757
}
5858
)
5959

60+
IncludeGroupDict = TypedDict(
61+
"IncludeGroupDict",
62+
{
63+
"include-group": str
64+
}
65+
)
66+
67+
#: :class:`typing.TypedDict` representing the output from the :class:`~.DependencyGroupsParser` class.
68+
DependencyGroupsDict = Dict[str, List[Union[str, IncludeGroupDict]]]
69+
6070
#: Type hint for the :pep621:`dynamic` field defined in :pep:`621`.
6171
Dynamic = Literal[
6272
"name",

0 commit comments

Comments
 (0)