Skip to content

Commit

Permalink
MAINT: remove workarounds for bugs fixed in pyproject-metadata 0.8.0
Browse files Browse the repository at this point in the history
Update tests to consider only pyproject-metadata 0.8.0 behavior.
  • Loading branch information
dnicolodi committed Apr 25, 2024
1 parent 78ed330 commit d1da1af
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 37 deletions.
26 changes: 2 additions & 24 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,12 @@ class MesonBuilderError(Error):

class Metadata(pyproject_metadata.StandardMetadata):
def __init__(self, name: str, *args: Any, **kwargs: Any):
super().__init__(name, *args, **kwargs)
# Local fix for https://github.com/FFY00/python-pyproject-metadata/issues/60
self.name = self._validate_name(name)

@staticmethod
def _validate_name(name: str) -> str:
# See https://packaging.python.org/en/latest/specifications/core-metadata/#name
if not re.match(r'^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$', name, re.IGNORECASE):
raise pyproject_metadata.ConfigurationError(
f'Invalid project name "{name}". A valid name consists only of ASCII letters and '
f'numbers, period, underscore and hyphen. It must start and end with a letter or number')
return name
super().__init__(name, *args, **kwargs)

@classmethod
def from_pyproject(
Expand All @@ -254,12 +248,7 @@ def from_pyproject(
project_dir: Path = os.path.curdir,
metadata_version: Optional[str] = None
) -> Self:
metadata = super().from_pyproject(data, project_dir)

# Check for missing version field.
if not metadata.version and 'version' not in metadata.dynamic:
raise pyproject_metadata.ConfigurationError(
'Required "project.version" field is missing and not declared as dynamic')
metadata = super().from_pyproject(data, project_dir, metadata_version)

# Check for unsupported dynamic fields.
unsupported_dynamic = set(metadata.dynamic) - {'version', }
Expand All @@ -269,17 +258,6 @@ def from_pyproject(

return metadata

# Local fix for a bug in pyproject-metadata. See
# https://github.com/mesonbuild/meson-python/issues/454
def _update_dynamic(self, value: Any) -> None:
if value and 'version' in self.dynamic:
self.dynamic.remove('version')

@property
def canonical_name(self) -> str:
# See https://packaging.python.org/en/latest/specifications/name-normalization/#normalization
return packaging.utils.canonicalize_name(self.name)

@property
def distribution_name(self) -> str:
"""Name to be used in wheel and sdist file names."""
Expand Down
2 changes: 1 addition & 1 deletion tests/packages/full-metadata/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description = 'Some package with all of the PEP 621 metadata'
readme = 'README.md'
requires-python = '>=3.7'
license = {file = 'LICENSE'}
keywords = ['full', 'metadata']
keywords = ['full', 'metadata', 'keyword with spaces']
authors = [
{email = 'jhon.doe@example.com'},
{name = 'Jane Doe'}
Expand Down
7 changes: 1 addition & 6 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,5 @@ def test_missing_version(package_missing_version):
pyproject = {'project': {
'name': 'missing-version',
}}
match = '|'.join((
re.escape('Required "project.version" field is missing'),
# pyproject-metatadata 0.8.0 and later
re.escape('Field "project.version" missing and "version" not specified in "project.dynamic"'),
))
with pytest.raises(pyproject_metadata.ConfigurationError, match=match):
with pytest.raises(pyproject_metadata.ConfigurationError, match='Field "project.version" missing'):
Metadata.from_pyproject(pyproject, pathlib.Path())
10 changes: 4 additions & 6 deletions tests/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def test_pep621(sdist_full_metadata):
with tarfile.open(sdist_full_metadata, 'r:gz') as sdist:
sdist_pkg_info = sdist.extractfile('full_metadata-1.2.3/PKG-INFO').read().decode()

metadata = re.escape(textwrap.dedent('''\
metadata = textwrap.dedent('''\
Metadata-Version: 2.1
Name: full-metadata
Version: 1.2.3
Summary: Some package with all of the PEP 621 metadata
Keywords: full metadata
Keywords: full,metadata,keyword with spaces
Home-page: https://example.com
Author: Jane Doe
Author-Email: Unknown <jhon.doe@example.com>
Expand Down Expand Up @@ -66,11 +66,9 @@ def test_pep621(sdist_full_metadata):
# full-metadata
An example package with all of the PEP 621 metadata!
'''))
''')

# pyproject-metadata 0.8.0 and later uses a comma to separate keywords
expr = metadata.replace(r'Keywords:\ full\ metadata', r'Keywords:\ full[ ,]metadata')
assert re.fullmatch(expr, sdist_pkg_info)
assert sdist_pkg_info == metadata


def test_dynamic_version(sdist_dynamic_version):
Expand Down

0 comments on commit d1da1af

Please sign in to comment.