Skip to content

Commit

Permalink
fix: produce standard Python repr style (#183)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored Sep 30, 2024
1 parent 06bcbae commit c26b406
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 323 deletions.
42 changes: 21 additions & 21 deletions pyproject_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class RFC822Policy(email.policy.EmailPolicy):

def header_store_parse(self, name: str, value: str) -> tuple[str, str]:
if name.lower() not in constants.KNOWN_METADATA_FIELDS:
msg = f'Unknown field "{name}"'
msg = f"Unknown field {name!r}"
raise ConfigurationError(msg, key=name)
size = len(name) + 2
value = value.replace("\n", "\n" + " " * size)
Expand Down Expand Up @@ -225,7 +225,7 @@ def __setattr__(self, name: str, value: Any) -> None:
metadata_name = name.replace("_", "-")
locked_fields = constants.KNOWN_METADATA_FIELDS - set(self.dynamic)
if metadata_name in locked_fields:
msg = f'Field "{name}" is not dynamic'
msg = f"Field {name!r} is not dynamic"
raise AttributeError(msg)
super().__setattr__(name, value)

Expand Down Expand Up @@ -256,38 +256,38 @@ def validate(self, *, warn: bool = True) -> None: # noqa: C901
r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", self.name, re.IGNORECASE
):
msg = (
f'Invalid project name "{self.name}". A valid name consists only of ASCII letters and '
f"Invalid project name {self.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"
)
errors.config_error(msg, key="project.name")

if self.license_files is not None and isinstance(self.license, License):
msg = '"project.license-files" must not be used when "project.license" is not a SPDX license expression'
msg = "'project.license-files' must not be used when 'project.license' is not a SPDX license expression"
errors.config_error(msg, key="project.license-files")

if isinstance(self.license, str) and any(
c.startswith("License ::") for c in self.classifiers
):
msg = 'Setting "project.license" to an SPDX license expression is not compatible with "License ::" classifiers'
msg = "Setting 'project.license' to an SPDX license expression is not compatible with 'License ::' classifiers"
errors.config_error(msg, key="project.license")

if warn:
if self.description and "\n" in self.description:
warnings.warn(
'The one-line summary "project.description" should not contain more than one line. Readers might merge or truncate newlines.',
"The one-line summary 'project.description' should not contain more than one line. Readers might merge or truncate newlines.",
ConfigurationWarning,
stacklevel=2,
)
if self.auto_metadata_version not in constants.PRE_SPDX_METADATA_VERSIONS:
if isinstance(self.license, License):
warnings.warn(
'Set "project.license" to an SPDX license expression for metadata >= 2.4',
"Set 'project.license' to an SPDX license expression for metadata >= 2.4",
ConfigurationWarning,
stacklevel=2,
)
elif any(c.startswith("License ::") for c in self.classifiers):
warnings.warn(
'"License ::" classifiers are deprecated for metadata >= 2.4, use a SPDX license expression for "project.license" instead',
"'License ::' classifiers are deprecated for metadata >= 2.4, use a SPDX license expression for 'project.license' instead",
ConfigurationWarning,
stacklevel=2,
)
Expand All @@ -296,14 +296,14 @@ def validate(self, *, warn: bool = True) -> None: # noqa: C901
isinstance(self.license, str)
and self.auto_metadata_version in constants.PRE_SPDX_METADATA_VERSIONS
):
msg = 'Setting "project.license" to an SPDX license expression is supported only when emitting metadata version >= 2.4'
msg = "Setting 'project.license' to an SPDX license expression is supported only when emitting metadata version >= 2.4"
errors.config_error(msg, key="project.license")

if (
self.license_files is not None
and self.auto_metadata_version in constants.PRE_SPDX_METADATA_VERSIONS
):
msg = '"project.license-files" is supported only when emitting metadata version >= 2.4'
msg = "'project.license-files' is supported only when emitting metadata version >= 2.4"
errors.config_error(msg, key="project.license-files")

errors.finalize("Metadata validation failed")
Expand Down Expand Up @@ -352,7 +352,7 @@ def from_pyproject( # noqa: C901

pyproject_table: PyProjectTable = data # type: ignore[assignment]
if "project" not in pyproject_table:
msg = 'Section "project" missing in pyproject.toml'
msg = "Section 'project' missing in pyproject.toml"
pyproject.config_error(msg, key="project")
pyproject.finalize("Failed to parse pyproject.toml")
msg = "Unreachable code" # pragma: no cover
Expand All @@ -364,8 +364,8 @@ def from_pyproject( # noqa: C901
if not allow_extra_keys:
extra_keys = extras_project(data)
if extra_keys:
extra_keys_str = ", ".join(sorted(f'"{k}"' for k in extra_keys))
msg = f'Extra keys present in "project": {extra_keys_str}'
extra_keys_str = ", ".join(sorted(f"{k!r}" for k in extra_keys))
msg = f"Extra keys present in 'project': {extra_keys_str}"
if allow_extra_keys is None:
warnings.warn(msg, ConfigurationWarning, stacklevel=2)
else:
Expand All @@ -375,13 +375,13 @@ def from_pyproject( # noqa: C901

for field in dynamic:
if field in data["project"]:
msg = f'Field "project.{field}" declared as dynamic in "project.dynamic" but is defined'
msg = f"Field 'project.{field}' declared as dynamic in 'project.dynamic' but is defined"
pyproject.config_error(msg, key=field)

raw_name = project.get("name")
name = "UNKNOWN"
if raw_name is None:
msg = 'Field "project.name" missing'
msg = "Field 'project.name' missing"
pyproject.config_error(msg, key="name")
else:
tmp_name = pyproject.ensure_str(raw_name, "project.name")
Expand All @@ -400,10 +400,10 @@ def from_pyproject( # noqa: C901
else None
)
except packaging.version.InvalidVersion:
msg = f'Invalid "project.version" value, expecting a valid PEP 440 version (got "{version_string}")'
msg = f"Invalid 'project.version' value, expecting a valid PEP 440 version (got {version_string!r})"
pyproject.config_error(msg, key="project.version")
elif "version" not in dynamic:
msg = 'Field "project.version" missing and "version" not specified in "project.dynamic"'
msg = "Field 'project.version' missing and 'version' not specified in 'project.dynamic'"
pyproject.config_error(msg, key="version")

# Description fills Summary, which cannot be multiline
Expand All @@ -428,7 +428,7 @@ def from_pyproject( # noqa: C901
requires_python_string
)
except packaging.specifiers.InvalidSpecifier:
msg = f'Invalid "project.requires-python" value, expecting a valid specifier set (got "{requires_python_string}")'
msg = f"Invalid 'project.requires-python' value, expecting a valid specifier set (got {requires_python_string!r})"
pyproject.config_error(msg, key="project.requires-python")

self = None
Expand Down Expand Up @@ -597,12 +597,12 @@ def _build_extra_req(
if requirement.marker:
if "or" in requirement.marker._markers:
requirement.marker = packaging.markers.Marker(
f'({requirement.marker}) and extra == "{extra}"'
f"({requirement.marker}) and extra == {extra!r}"
)
else:
requirement.marker = packaging.markers.Marker(
f'{requirement.marker} and extra == "{extra}"'
f"{requirement.marker} and extra == {extra!r}"
)
else:
requirement.marker = packaging.markers.Marker(f'extra == "{extra}"')
requirement.marker = packaging.markers.Marker(f"extra == {extra!r}")
return requirement
Loading

0 comments on commit c26b406

Please sign in to comment.