Skip to content

Commit 978fb55

Browse files
authored
validate extras against dependencies and in schema (#542)
1 parent cd5b624 commit 978fb55

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

src/poetry/core/factory.py

+16
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,22 @@ def validate(
409409
'Use "allow-prereleases" instead.'
410410
)
411411

412+
if "extras" in config:
413+
for extra_name, requirements in config["extras"].items():
414+
extra_name = canonicalize_name(extra_name)
415+
416+
for req in requirements:
417+
req_name = canonicalize_name(req)
418+
for dependency in config.get("dependencies", {}).keys():
419+
dep_name = canonicalize_name(dependency)
420+
if req_name == dep_name:
421+
break
422+
else:
423+
result["errors"].append(
424+
f'Cannot find dependency "{req}" for extra '
425+
f'"{extra_name}" in main dependencies.'
426+
)
427+
412428
# Checking for scripts with extras
413429
if "scripts" in config:
414430
scripts = config["scripts"]

src/poetry/core/json/schemas/poetry-schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@
155155
"^[a-zA-Z-_.0-9]+$": {
156156
"type": "array",
157157
"items": {
158-
"type": "string"
158+
"type": "string",
159+
"pattern": "^[a-zA-Z-_.0-9]+$"
159160
}
160161
}
161162
}

tests/fixtures/project_failing_strict_validation/pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ readme = ["README.rst", "README_WITH_ANOTHER_EXTENSION.md"]
55
python = "*"
66
pathlib2 = { version = "^2.2", python = "3.7", allows-prereleases = true }
77

8+
[tool.poetry.extras]
9+
some_extras = ["missing_extra", "another_missing_extra"]
10+
811
[tool.poetry.scripts]
912
a_script_with_unknown_extra = { reference = "a_script_with_unknown_extra.py", type = "file", extras = ["foo"] }
1013
a_script_without_extras = { reference = "a_script_without_extras.py", type = "file" }

tests/json/test_poetry_schema.py

+12
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ def test_multiline_description(
6565

6666
regex = r"\\A[^\n]*\\Z"
6767
assert errors[0] == f"[description] {bad_description!r} does not match '{regex}'"
68+
69+
70+
def test_bad_extra(base_object: dict[str, Any]) -> None:
71+
bad_extra = "a{[*+"
72+
base_object["extras"] = {}
73+
base_object["extras"]["test"] = [bad_extra]
74+
75+
errors = validate_object(base_object, "poetry-schema")
76+
assert len(errors) == 1
77+
assert (
78+
errors[0] == f"[extras.test.0] {bad_extra!r} does not match '^[a-zA-Z-_.0-9]+$'"
79+
)

tests/test_factory.py

+8
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ def test_validate_strict_fails_strict_and_non_strict() -> None:
232232
"'version' is a required property",
233233
"'description' is a required property",
234234
"'authors' is a required property",
235+
(
236+
'Cannot find dependency "missing_extra" for extra "some-extras" in '
237+
"main dependencies."
238+
),
239+
(
240+
'Cannot find dependency "another_missing_extra" for extra '
241+
'"some-extras" in main dependencies.'
242+
),
235243
(
236244
'Script "a_script_with_unknown_extra" requires extra "foo" which is not'
237245
" defined."

0 commit comments

Comments
 (0)