Skip to content

feat(requirement): PEP 508 dependency specifier (#18636) - #7

Merged
jonyoder merged 4 commits into
mainfrom
impl-requirement
Jul 9, 2026
Merged

jonyoder merged 4 commits into
mainfrom
impl-requirement

Conversation

@jonyoder

@jonyoder jonyoder commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Implements requirement/ — PEP 508 dependency specifiers — the third and final PR of the PEP 508 core (extras → marker → requirement) for the native Go PyPI resolver (RFD 0001, epic rstudio/package-manager#18625). Builds on extras/ (#18638) and marker/ (#18637), both merged.

Closes rstudio/package-manager#18636.

What

  • requirement.Parse(s) → Requirement parsing name [extras] [versionspec | @ url] [; marker], with Requirement{Name, Extras, Specifiers, URL, Marker}, String() (round-trippable), and an ErrInvalidRequirement sentinel.
  • One-pass parse over the shared tokenizer. The requirement parser (in internal/pep508) drives the same parser-driven tokenizer the marker package uses and parses the ; marker clause by calling pep508.ParseMarker mid-stream — no ; substring split, no second lexer. This is what makes a URL containing ; (pkg @ https://x/y;z.whl ; python_version > "3") parse correctly: the greedy, whitespace-terminated URL token consumes the URL whole and the marker is still separated.
  • marker.FromExpr seam added to the marker package so the parsed marker expression flows out of the internal package and is wrapped by requirement/ without an import cycle.
  • Specifiers delegate to version.NewSpecifiers (parenthesized (>=1.0) and multi-clause >=1.0,<2.0 supported); extras normalized via extras.Normalize; Name kept as-parsed (caller canonicalizes).
  • Distinguishable error categories: malformed-syntax, bad-version, and bad-marker all satisfy errors.Is(err, ErrInvalidRequirement) while remaining distinguishable via errors.As.

Notes

  • The requirement grammar preserves PEP 508's whitespace asymmetry: name>=1.0;marker (no space) is legal, but a URL requirement needs wsp+/end before ; marker.
  • New tokenizer kinds are appended to the shared tokenizer's enum and are conflict-free with the marker kinds (the tokenizer is parser-driven — each parser asks for the kind valid at its position); the marker parser never touches the new Identifier kind.

Verification

Per commit: go build, go vet, go test, gofmt -l (empty), golangci-lint run ./... (v2, 0 issues). Correctness anchored on packaging's test_requirements.py cases ported as Go table tests, including the URL-with-; case, the grammar asymmetry, empty [] extras, parenthesized specifiers, error categories, and round-trips.

jonyoder added 4 commits July 9, 2026 14:51
Port pypa/packaging's _parse_requirement: name [extras] (versionspec |
"@" url) [";" marker], driving the shared internal/pep508 tokenizer
end-to-end in one pass rather than splitting the string on ";" (which
would mishandle a URL requirement whose URL itself contains ";").

- internal/pep508: add requirement-only token kinds (Identifier,
  LBracket, RBracket, Comma, Semicolon, AT, Specifier, URL) and
  ParseRequirement, which parses the marker clause mid-stream via the
  existing ParseMarker - no substring re-parse, no second lexer.
- marker: add FromExpr, the seam that lets requirement/ wrap an
  already-parsed pep508.Expr into a Marker without re-lexing.
- requirement: public Requirement/Parse/String, delegating the
  specifier to version.NewSpecifiers, extras to extras.Normalize, and
  distinguishing malformed-syntax/bad-version/bad-marker error
  categories under the ErrInvalidRequirement sentinel.
Polish requirement/doc.go to describe the implemented Parse/Requirement
API and ErrInvalidRequirement, add the missing NOTICE attribution for
the requirement/ package's dependency-specifier grammar (pypa/packaging
_parse_requirement + requirements.py, Apache-2.0; Astral uv's uv-pep508
crate, dual Apache-2.0/MIT), and mark the requirement/ row implemented
in README.md.
- Remove the misleading versionParse test helper (no alias collision
  exists); call sites now use version.Parse directly.
- Add a comment on the URL tokenizer rule noting the deliberate
  space+tab divergence from upstream packaging's space-only rule.
- Add a test asserting foo() (empty parenthesized specifier) is
  rejected as ErrInvalidRequirement.
…ace/tab (#18636)

The URL rule (\A[^ \t]+) treated newline/CR as URL characters, so a
defensively-multiline value like "name @ https://x/y\n; python_version > \"3\""
would absorb the marker clause into the URL token and lose it. Change the
rule to \A\S+, the exact complement of the WS rule, so any whitespace
terminates the URL - safe since a valid URL never contains whitespace.

Add a regression test covering a newline between the URL and the marker
clause; the existing URL-with-";" behavior is unaffected since ";" is not
whitespace.
@jonyoder
jonyoder merged commit a9d26cf into main Jul 9, 2026
2 checks passed
@jonyoder
jonyoder deleted the impl-requirement branch July 9, 2026 19:13
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