Skip to content

Commit

Permalink
fix: some touchup based on packaging PR
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Oct 25, 2024
1 parent cb74500 commit 7ff3b96
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
27 changes: 14 additions & 13 deletions pyproject_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import os
import os.path
import pathlib
import re
import sys
import typing
import warnings
Expand Down Expand Up @@ -474,11 +473,9 @@ def validate(self, *, warn: bool = True) -> None: # noqa: C901
msg = "The metadata_version must be one of {versions} or None (default)"
errors.config_error(msg, versions=constants.KNOWN_METADATA_VERSIONS)

# See https://packaging.python.org/en/latest/specifications/core-metadata/#name and
# https://packaging.python.org/en/latest/specifications/name-normalization/#name-format
if not re.match(
r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", self.name, re.IGNORECASE
):
try:
packaging.utils.canonicalize_name(self.name, validate=True)
except packaging.utils.InvalidName:
msg = (
"Invalid project name {name!r}. A valid name consists only of ASCII letters and "
"numbers, period, underscore and hyphen. It must start and end with a letter or number"
Expand Down Expand Up @@ -543,13 +540,15 @@ def _write_metadata( # noqa: C901
"""
Write the metadata to the message. Handles JSON or Message.
"""
self.validate(warn=False)
errors = ErrorCollector(collect_errors=self.all_errors)
with errors.collect():
self.validate(warn=False)

smart_message["Metadata-Version"] = self.auto_metadata_version
smart_message["Name"] = self.name
if not self.version:
msg = "Missing version field"
raise ConfigurationError(msg)
msg = "Field {key} missing"
errors.config_error(msg, key="project.version")
smart_message["Version"] = str(self.version)
# skip 'Platform'
# skip 'Supported-Platform'
Expand Down Expand Up @@ -604,13 +603,15 @@ def _write_metadata( # noqa: C901
if self.auto_metadata_version != "2.1":
for field in self.dynamic_metadata:
if field.lower() in {"name", "version", "dynamic"}:
msg = f"Field cannot be set as dynamic metadata: {field}"
raise ConfigurationError(msg)
msg = f"Field cannot be set as dynamic metadata: {field!r}"
errors.config_error(msg)
if field.lower() not in constants.KNOWN_METADATA_FIELDS:
msg = f"Field is not known: {field}"
raise ConfigurationError(msg)
msg = f"Field is not known: {field!r}"
errors.config_error(msg)
smart_message["Dynamic"] = field

errors.finalize("Failed to write metadata")


def _name_list(people: list[tuple[str, str | None]]) -> str | None:
"""
Expand Down
10 changes: 5 additions & 5 deletions tests/test_standard_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,19 +1385,19 @@ def test_as_rfc822_invalid_dynamic() -> None:
)
with pytest.raises(
pyproject_metadata.ConfigurationError,
match="Field cannot be set as dynamic metadata: name",
match="Field cannot be set as dynamic metadata: 'name'",
):
metadata.as_rfc822()
metadata.dynamic_metadata = ["version"]
with pytest.raises(
pyproject_metadata.ConfigurationError,
match="Field cannot be set as dynamic metadata: version",
match="Field cannot be set as dynamic metadata: 'version'",
):
metadata.as_rfc822()
metadata.dynamic_metadata = ["unknown"]
with pytest.raises(
pyproject_metadata.ConfigurationError,
match="Field is not known: unknown",
match="Field is not known: 'unknown'",
):
metadata.as_rfc822()

Expand All @@ -1417,12 +1417,12 @@ def test_as_rfc822_mapped_dynamic() -> None:
def test_as_rfc822_missing_version() -> None:
metadata = pyproject_metadata.StandardMetadata(name="something")
with pytest.raises(
pyproject_metadata.ConfigurationError, match="Missing version field"
pyproject_metadata.ConfigurationError, match='Field "project.version" missing'
):
metadata.as_rfc822()


def test_stically_defined_dynamic_field() -> None:
def test_statically_defined_dynamic_field() -> None:
with pytest.raises(
pyproject_metadata.ConfigurationError,
match='Field "project.version" declared as dynamic in "project.dynamic" but is defined',
Expand Down

0 comments on commit 7ff3b96

Please sign in to comment.