Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions dparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,32 @@ def parse(self) -> None:
if not cfg or "project" not in cfg:
return

project = cfg["project"]
if not isinstance(project, dict):
raise MalformedDependencyFileError(
info=f"'project' must be a table, not "
f"{type(project).__name__}"
)

optional_dependencies = project.get("optional-dependencies", {})
if not isinstance(optional_dependencies, dict):
raise MalformedDependencyFileError(
info="'project.optional-dependencies' must be a table, not "
f"{type(optional_dependencies).__name__}"
)

sections = {
"dependencies": cfg["project"].get("dependencies", []),
**cfg["project"].get("optional-dependencies", {}),
"dependencies": project.get("dependencies", []),
**optional_dependencies,
}

for section, lines in sections.items():
if not isinstance(lines, list) or not all(
isinstance(line, str) for line in lines
):
raise MalformedDependencyFileError(
info=f"'{section}' must be a list of strings"
)
for line in lines:
req = RequirementsTXTLineParser.parse(line)
if req:
Expand Down
65 changes: 65 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,68 @@ def test_pyproject_toml_no_extra() -> None:
assert dep_file.dependencies[0].specs == SpecifierSet("")
assert dep_file.dependencies[0].dependency_type == "pyproject.toml"
assert dep_file.dependencies[0].section == "dependencies"


def test_pyproject_toml_invalid_dependencies_type() -> None:
"""A scalar `dependencies` value must not be iterated character by
character, which silently turned "requests" into 8 bogus packages.
"""
content = """
[project]
name = "my_package"
dependencies = "requests"
"""
throw = None

try:
parse(content, file_type=filetypes.pyproject_toml)
except Exception as e:
throw = e

assert isinstance(throw, MalformedDependencyFileError)


def test_pyproject_toml_invalid_dependencies_entry_type() -> None:
content = """
[project]
name = "my_package"
dependencies = [1, 2]
"""
throw = None

try:
parse(content, file_type=filetypes.pyproject_toml)
except Exception as e:
throw = e

assert isinstance(throw, MalformedDependencyFileError)


def test_pyproject_toml_project_is_not_a_table() -> None:
content = """
project = []
"""
throw = None

try:
parse(content, file_type=filetypes.pyproject_toml)
except Exception as e:
throw = e

assert isinstance(throw, MalformedDependencyFileError)


def test_pyproject_toml_optional_dependencies_is_not_a_table() -> None:
content = """
[project]
name = "my_package"
optional-dependencies = ["ReportLab>=1.2"]
"""
throw = None

try:
parse(content, file_type=filetypes.pyproject_toml)
except Exception as e:
throw = e

assert isinstance(throw, MalformedDependencyFileError)