Skip to content

Commit f121187

Browse files
committed
pylock: validate package name normalization
1 parent 32bbc75 commit f121187

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/pip/_internal/models/pylock.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from pip._vendor.packaging.markers import Marker
2323
from pip._vendor.packaging.specifiers import SpecifierSet
24+
from pip._vendor.packaging.utils import NormalizedName, is_normalized_name
2425
from pip._vendor.packaging.version import Version
2526

2627
if TYPE_CHECKING:
@@ -452,7 +453,7 @@ def from_dict(cls, d: Mapping[str, Any]) -> "Self":
452453

453454
@dataclass(frozen=True)
454455
class Package:
455-
name: str
456+
name: NormalizedName
456457
version: Optional[Version] = None
457458
marker: Optional[Marker] = None
458459
requires_python: Optional[SpecifierSet] = None
@@ -498,6 +499,8 @@ def __init__(
498499
object.__setattr__(self, "attestation_identities", attestation_identities)
499500
object.__setattr__(self, "tool", tool)
500501
# __post_init__ in Python 3.10+
502+
if not is_normalized_name(self.name):
503+
raise PylockValidationError(f"Package name {self.name!r} is not normalized")
501504
if self.sdist or self.wheels:
502505
if any([self.vcs, self.directory, self.archive]):
503506
raise PylockValidationError(

tests/unit/test_pylock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ def test_hash_validation(hashes: Dict[str, Any], expected_error: str) -> None:
294294
assert str(exc_info.value) == expected_error
295295

296296

297+
def test_package_name_validation() -> None:
298+
with pytest.raises(PylockValidationError) as exc_info:
299+
Package(name="Example")
300+
assert str(exc_info.value) == "Package name 'Example' is not normalized"
301+
302+
297303
def test_is_direct() -> None:
298304
direct_package = Package(
299305
name="example",

0 commit comments

Comments
 (0)