Skip to content

fix(requirements): raise InvalidRequirement for malformed specifier lists - #1332

Merged
henryiii merged 1 commit into
pypa:mainfrom
r266-tech:r266-invalidrequirement-contract
Jul 20, 2026
Merged

henryiii merged 1 commit into
pypa:mainfrom
r266-tech:r266-invalidrequirement-contract

Conversation

@r266-tech

Copy link
Copy Markdown
Contributor

Summary

  • translate SpecifierSet validation failures into InvalidRequirement, matching Requirement's documented exception contract
  • cover both direct construction and stable-pickle restoration with the malformed demo===x,y form

Why

The requirement parser accepts the arbitrary-equality token and comma, but the later SpecifierSet construction rejects the bare y. That currently leaks InvalidSpecifier from Requirement(...), so callers cannot consistently handle malformed requirement strings via InvalidRequirement; the same leak also bypasses Requirement.__setstate__'s invalid-string conversion.

Validation

  • uv run pytest -q — 62,115 passed, 1 platform skip
  • uv run pytest tests/test_requirements.py -q — 5,322 passed
  • uv run ruff check src/packaging/requirements.py tests/test_requirements.py
  • uv run ruff format --check src/packaging/requirements.py tests/test_requirements.py

@henryiii
henryiii merged commit fe18f13 into pypa:main Jul 20, 2026
69 checks passed
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.

2 participants