Skip to content

Commit 6974a6e

Browse files
committed
pylock: reject str when Sequence is expected
1 parent 2fa5f17 commit 6974a6e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/packaging/pylock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ def _get_sequence(
106106
"""Get a list value from the dictionary and verify it's the expected items type."""
107107
if (value := _get(d, Sequence, key)) is None: # type: ignore[type-abstract]
108108
return None
109+
if isinstance(value, str):
110+
# special case: str is a Sequence, but we want to reject it
111+
raise PylockValidationError(
112+
f"Unexpected type {type(value).__name__} (expected Sequence)",
113+
context=key,
114+
)
109115
for i, item in enumerate(value):
110116
if not isinstance(item, expected_item_type):
111117
raise PylockValidationError(

tests/test_pylock.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,17 @@ def test_validate() -> None:
508508
str(exc_info.value)
509509
== "Name 'example_package' is not normalized in 'packages[0].name'"
510510
)
511+
512+
513+
def test_validate_sequence_of_str() -> None:
514+
pylock = Pylock(
515+
lock_version=Version("1.0"),
516+
created_by="some_tool",
517+
packages=[],
518+
dependency_groups="abc", # should be a sequence of str
519+
)
520+
with pytest.raises(PylockValidationError) as exc_info:
521+
pylock.validate()
522+
assert str(exc_info.value) == (
523+
"Unexpected type str (expected Sequence) in 'dependency-groups'"
524+
)

0 commit comments

Comments
 (0)