Skip to content

Commit 2273e41

Browse files
committed
pylock: refine and test some validation erros
1 parent 901653f commit 2273e41

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

src/pip/_internal/models/pylock.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def _get(d: Dict[str, Any], expected_type: Type[T], key: str) -> Optional[T]:
8585
return None
8686
if not isinstance(value, expected_type):
8787
raise PylockValidationError(
88-
f"{key} has unexpected type {type(value)} (expected {expected_type})"
88+
f"{key!r} has unexpected type {type(value).__name__} "
89+
f"(expected {expected_type.__name__})"
8990
)
9091
return value
9192

@@ -114,7 +115,7 @@ def _get_as(
114115
try:
115116
return target_type(value)
116117
except Exception as e:
117-
raise PylockValidationError(f"Error parsing value of {key!r}: {e}") from e
118+
raise PylockValidationError(f"Error in {key!r}: {e}") from e
118119

119120

120121
def _get_required_as(
@@ -145,15 +146,13 @@ def _get_list_as(
145146
for i, item in enumerate(value):
146147
if not isinstance(item, expected_type):
147148
raise PylockValidationError(
148-
f"Item {i} of {key} has unpexpected type {type(item)} "
149-
f"(expected {expected_type})"
149+
f"Item {i} of {key!r} has unexpected type {type(item).__name__} "
150+
f"(expected {expected_type.__name__})"
150151
)
151152
try:
152153
result.append(target_type(item))
153154
except Exception as e:
154-
raise PylockValidationError(
155-
f"Error parsing item {i} of {key!r}: {e}"
156-
) from e
155+
raise PylockValidationError(f"Error in item {i} of {key!r}: {e}") from e
157156
return result
158157

159158

@@ -167,7 +166,7 @@ def _get_object(
167166
try:
168167
return target_type.from_dict(value)
169168
except Exception as e:
170-
raise PylockValidationError(f"Error parsing value of {key!r}: {e}") from e
169+
raise PylockValidationError(f"Error in {key!r}: {e}") from e
171170

172171

173172
def _get_list_of_objects(
@@ -184,9 +183,7 @@ def _get_list_of_objects(
184183
try:
185184
result.append(target_type.from_dict(item))
186185
except Exception as e:
187-
raise PylockValidationError(
188-
f"Error parsing item {i} of {key!r}: {e}"
189-
) from e
186+
raise PylockValidationError(f"Error in item {i} of {key!r}: {e}") from e
190187
return result
191188

192189

tests/unit/test_pylock.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_pylock_packages_without_dist() -> None:
108108
with pytest.raises(PylockValidationError) as exc_info:
109109
Pylock.from_dict(data)
110110
assert str(exc_info.value) == (
111-
"Error parsing item 0 of 'packages': "
111+
"Error in item 0 of 'packages': "
112112
"Exactly one of vcs, directory, archive must be set "
113113
"if sdist and wheels are not set"
114114
)
@@ -140,3 +140,76 @@ def test_pylock_basic_package() -> None:
140140
assert package.marker == Marker('os_name == "posix"')
141141
assert package.requires_python == SpecifierSet(">=3.10, !=3.10.1")
142142
assert pylock.to_dict() == data
143+
144+
145+
def test_pylock_invalid_archive() -> None:
146+
data = {
147+
"lock-version": "1.0",
148+
"created-by": "pip",
149+
"requires-python": ">=3.10",
150+
"environments": ['os_name == "posix"'],
151+
"packages": [
152+
{
153+
"name": "example",
154+
"archive": {
155+
# "path": "example.tar.gz",
156+
"hashes": {"sha256": "f" * 40},
157+
},
158+
}
159+
],
160+
}
161+
with pytest.raises(PylockValidationError) as exc_info:
162+
Pylock.from_dict(data)
163+
assert str(exc_info.value) == (
164+
"Error in item 0 of 'packages': "
165+
"Error in 'archive': "
166+
"No path nor url set for archive package"
167+
)
168+
169+
170+
def test_pylock_invalid_wheel() -> None:
171+
data = {
172+
"lock-version": "1.0",
173+
"created-by": "pip",
174+
"requires-python": ">=3.10",
175+
"environments": ['os_name == "posix"'],
176+
"packages": [
177+
{
178+
"name": "example",
179+
"wheels": [
180+
{
181+
"name": "example-1.0-py3-none-any.whl",
182+
"path": "./example-1.0-py3-none-any.whl",
183+
# "hashes": {"sha256": "f" * 40},
184+
}
185+
],
186+
}
187+
],
188+
}
189+
with pytest.raises(PylockValidationError) as exc_info:
190+
Pylock.from_dict(data)
191+
assert str(exc_info.value) == (
192+
"Error in item 0 of 'packages': "
193+
"Error in item 0 of 'wheels': "
194+
"Missing required key 'hashes'"
195+
)
196+
197+
198+
def test_pylock_invalid_environments() -> None:
199+
data = {
200+
"lock-version": "1.0",
201+
"created-by": "pip",
202+
"environments": [
203+
'os_name == "posix"',
204+
'invalid_marker == "..."',
205+
],
206+
"packages": [],
207+
}
208+
with pytest.raises(PylockValidationError) as exc_info:
209+
Pylock.from_dict(data)
210+
assert str(exc_info.value) == (
211+
"Error in item 1 of 'environments': "
212+
"Expected a marker variable or quoted string\n"
213+
' invalid_marker == "..."\n'
214+
" ^"
215+
)

0 commit comments

Comments
 (0)