Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: some touchup based on packaging PR #211

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
henryiii marked this conversation as resolved.
Show resolved Hide resolved
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"Metadata field {field!r} cannot be declared dynamic"
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"Unknown metadata field {field!r} cannot be declared dynamic"
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
12 changes: 6 additions & 6 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="Metadata field 'name' cannot be declared dynamic",
):
metadata.as_rfc822()
metadata.dynamic_metadata = ["version"]
metadata.dynamic_metadata = ["Version"]
with pytest.raises(
pyproject_metadata.ConfigurationError,
match="Field cannot be set as dynamic metadata: version",
match="Metadata field 'Version' cannot be declared dynamic",
):
metadata.as_rfc822()
metadata.dynamic_metadata = ["unknown"]
with pytest.raises(
pyproject_metadata.ConfigurationError,
match="Field is not known: unknown",
match="Unknown metadata field 'unknown' cannot be declared dynamic",
):
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