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

Switch from pkginfo to packaging for parsing distribution metadata #1180

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dnicolodi
Copy link

The packaging package is maintained by the PyPA and it is the de-facto reference implementation for the packaging standards. Using packaging for parsing metadata guarantees support for the latest metadata versions.

warehouse, the Python package index implementation used by PyPI, also uses packaging for parsing metadata. This guarantees that metadata parsing is the same on the client and server side, for the most prominent index.

@dnicolodi
Copy link
Author

This passes the unit tests, except for this one

twine/tests/test_package.py

Lines 383 to 437 in dd61356

@pytest.mark.parametrize(
"read_data, missing_fields",
[
pytest.param(
b"Metadata-Version: 102.3\nName: test-package\nVersion: 1.0.0\n",
"Name, Version",
id="unsupported Metadata-Version",
),
pytest.param(
b"Metadata-Version: 2.3\nName: UNKNOWN\nVersion: UNKNOWN\n",
"Name, Version",
id="missing Name and Version",
),
pytest.param(
b"Metadata-Version: 2.2\nName: UNKNOWN\nVersion: UNKNOWN\n",
"Name, Version",
id="missing Name and Version",
),
pytest.param(
b"Metadata-Version: 2.3\nName: UNKNOWN\nVersion: 1.0.0\n",
"Name",
id="missing Name",
),
pytest.param(
b"Metadata-Version: 2.2\nName: UNKNOWN\nVersion: 1.0.0\n",
"Name",
id="missing Name",
),
pytest.param(
b"Metadata-Version: 2.3\nName: test-package\nVersion: UNKNOWN\n",
"Version",
id="missing Version",
),
pytest.param(
b"Metadata-Version: 2.2\nName: test-package\nVersion: UNKNOWN\n",
"Version",
id="missing Version",
),
],
)
def test_pkginfo_returns_no_metadata(read_data, missing_fields, monkeypatch):
"""Raise an exception when pkginfo can't interpret the metadata.
This could be caused by a version number or format it doesn't support yet.
"""
monkeypatch.setattr(package_file.wheel.Wheel, "read", lambda _: read_data)
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"
with pytest.raises(exceptions.InvalidDistribution) as err:
package_file.PackageFile.from_filename(filename, comment=None)
assert (
f"Metadata is missing required fields: {missing_fields}." in err.value.args[0]
)
assert "1.0, 1.1, 1.2, 2.0, 2.1, 2.2" in err.value.args[0]
which is not surprising as the metadata validation raises different exceptions now. I don't know whether the new code should be adapted to raise the same exceptions or it is acceptable to adjust the test.

@dnicolodi dnicolodi force-pushed the packaging branch 2 times, most recently from aad1a9a to 60ba024 Compare November 24, 2024 00:01
The packaging package is maintained by the PyPA and it is the de-facto
reference implementation for the packaging standards. Using packaging
for parsing metadata guarantees support for the latest metadata
versions.

warehouse, the Python package index implementation used by PyPI, also
uses packaging for parsing metadata. This guarantees that metadata
parsing is the same on the client and server side, for the most
prominent index.
@dnicolodi
Copy link
Author

dnicolodi commented Nov 24, 2024

I've adjusted the test mentioned above. The only remaining issue is that packaging and pkginfo and thus twine disagree on the name of two metadata fields: pkginfo uses platform and supported_platform, while packaging uses platforms and supported_platforms. I don't know who is right here. There is a test failing because of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant