Skip to content

Commit 34647c1

Browse files
committed
pylock: factorize path/url validation
The context provided by the field name is sufficient, so we can have the same error message for all package sources
1 parent 706c446 commit 34647c1

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/pip/_internal/models/pylock.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ def _exactly_one(iterable: Iterable[object]) -> bool:
214214
return found
215215

216216

217+
def _validate_path_url(path: Optional[str], url: Optional[str]) -> None:
218+
if not path and not url:
219+
raise PylockValidationError("path or url must be provided")
220+
221+
217222
def _validate_hashes(hashes: Dict[str, Any]) -> None:
218223
if not hashes:
219224
raise PylockValidationError("At least one hash must be provided")
@@ -248,8 +253,7 @@ class PackageVcs:
248253
subdirectory: Optional[str]
249254

250255
def __post_init__(self) -> None:
251-
if not self.path and not self.url:
252-
raise PylockValidationError("No path nor url set for vcs package")
256+
_validate_path_url(self.path, self.url)
253257

254258
@classmethod
255259
def from_dict(cls, d: Dict[str, Any]) -> "Self":
@@ -288,8 +292,7 @@ class PackageArchive:
288292
subdirectory: Optional[str]
289293

290294
def __post_init__(self) -> None:
291-
if not self.path and not self.url:
292-
raise PylockValidationError("No path nor url set for archive package")
295+
_validate_path_url(self.path, self.url)
293296
_validate_hashes(self.hashes)
294297

295298
@classmethod
@@ -314,8 +317,7 @@ class PackageSdist:
314317
hashes: Dict[str, str]
315318

316319
def __post_init__(self) -> None:
317-
if not self.path and not self.url:
318-
raise PylockValidationError("No path nor url set for sdist package")
320+
_validate_path_url(self.path, self.url)
319321
_validate_hashes(self.hashes)
320322

321323
@classmethod
@@ -340,8 +342,7 @@ class PackageWheel:
340342
hashes: Dict[str, str]
341343

342344
def __post_init__(self) -> None:
343-
if not self.path and not self.url:
344-
raise PylockValidationError("No path nor url set for wheel package")
345+
_validate_path_url(self.path, self.url)
345346
_validate_hashes(self.hashes)
346347

347348
@classmethod

tests/unit/test_pylock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_pylock_invalid_archive() -> None:
165165
assert str(exc_info.value) == (
166166
"Error in item 0 of 'packages': "
167167
"Error in 'archive': "
168-
"No path nor url set for archive package"
168+
"path or url must be provided"
169169
)
170170

171171

0 commit comments

Comments
 (0)