Skip to content

Warn instead of failing on same-major newer Metadata-Version (#1404) - #1405

Closed
katariyaVivek wants to merge 1 commit into
pypa:mainfrom
katariyaVivek:fix/metadata-version-future-minor
Closed

katariyaVivek wants to merge 1 commit into
pypa:mainfrom
katariyaVivek:fix/metadata-version-future-minor

Conversation

@katariyaVivek

Copy link
Copy Markdown

Problem

Metadata.from_raw(data, validate=True) rejects any Metadata-Version outside a fixed allow-list, including same-major newer minors such as 2.7. Per the Core Metadata spec, automated tools consuming metadata SHOULD warn when metadata-version is greater than the highest version they support, and MUST fail only when it has a greater major version. Compliant tools currently cannot be built on this API without reimplementing validation by hand.

Root Cause

_Validator._process_metadata_version in src/packaging/metadata.py raises InvalidMetadata for every value not in _VALID_METADATA_VERSIONS, making the validate=True path fatal for future minors. Metadata.from_raw then collects it as a hard ExceptionGroup error.

Solution

  • _process_metadata_version now returns known versions unchanged; for an unknown version on the same major line with a numerically greater minor (ASCII digits only), it emits UserWarning ("Unknown metadata version 'X'; continuing as version 'Y'.") and accepts the value. Greater majors, older versions, and malformed values still raise InvalidMetadata. Missing (None) still raises the required-field error.
  • Metadata.from_raw(validate=True) clamps metadata_age to the latest known version when the accepted version is absent from the list, keeping field-introduction checks safe.

Testing

Added to tests/test_metadata.py:

  • test_future_minor_metadata_version_warns (2.7, 2.10): warns UserWarning and validates.
  • test_future_major_metadata_version_fails (3.0, 1.3, bogus): still raises ExceptionGroup.

Verified the new warn tests fail pre-fix (no warning emitted; ExceptionGroup raised) and pass post-fix.

Verification

  • python -m pytest tests/test_metadata.py: 347 passed.
  • ruff check src/packaging/metadata.py tests/test_metadata.py: clean.
  • ruff format --check src/packaging/metadata.py tests/test_metadata.py: clean.
  • Adversarial probes: 2.99 warns+accepts; 2.², 2.6.1, 02.7, 2.x, 2., empty, and 2 (no dot) all raise InvalidMetadata; validate=False lazy path warns identically.

Impact

Same-major future minors (e.g. 2.7 against max supported 2.6) now validate with a warning instead of raising; all other versions behave exactly as before.

Compatibility

Backwards compatible: previously-valid versions unchanged; previously-rejected same-major newer minors move from hard error to warning, matching the spec's SHOULD-warn guidance.

Fixes #1404

@brettcannon

Copy link
Copy Markdown
Member

AI prematurely opened this before the issue this claims to fix has even been agreed upon. I'm closing for now until the issue itself is resolved.

@brettcannon brettcannon closed this Sep 8, 2026
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.

Consuming new core metadata versions in packaging.metadata as a spec compliant automated tool is hard

2 participants