Repository navigation
feat(marker): PEP 508 environment markers (#18637) - #6
Merged
Merged
Conversation
Port pypa/packaging's _parser.py marker grammar (_parse_marker, _parse_marker_atom/_item/_var/_op) as a recursive-descent parser over the shared pep508 tokenizer, producing a typed, exported AST (BoolExpr/CompareExpr/EnvVar/Literal) that a later evaluator can walk directly instead of re-deriving and/or precedence at evaluation time. Also adds the public marker.Marker wrapper (Parse/IsEmpty/String), whose zero value is the always-true "no marker clause" marker.
Add the concrete-target marker environment (11 PEP 508 variables) and EnvironmentFromTarget, which fills platform fields from a tags.Target (linux/macos/windows, per uv's TargetTriple) and interpreter-identity fields from a caller-supplied InterpreterIdentity. Repairs a bare trailing "+" in python_full_version per packaging's _pep440_python_full_version so a later version.Parse does not fail.
Implement Marker.Evaluate with the unified per-comparison dispatch ported from packaging's _eval_op: extra set-membership, an attempted PEP 440 version comparison on the four version-typed variables (python_version, python_full_version, implementation_version, platform_release) that falls back to a generic string-operator table on any parse failure, and and/or tree evaluation.
Polish marker/doc.go to describe the implemented API (Parse, Marker, Environment/InterpreterIdentity, EnvironmentFromTarget, Evaluate) and drop the stale TODO. Add the missing NOTICE attributions for pypa/packaging's marker grammar (packaging/_parser.py) and evaluation logic (packaging/markers.py's _eval_op), and mark marker/ implemented in the README package table.
… (#18637) majorMinor previously derived python_version from python_full_version via a naive strings.SplitN(full, ".", 3), which breaks when the version has only two release segments followed by a local/pre/post/dev suffix. The common trigger is repairPythonFullVersion turning a bare trailing "+" into "+local" (e.g. "3.11+" -> "3.11+local"): majorMinor then returned "3.11+local" instead of "3.11", making python_version == "3.11" marker checks fail and selecting the wrong dependencies. majorMinor now validates full via version.Parse, then derives major.minor from the leading digit/"." run only (the PEP 440 release segment), discarding any local/pre/post/dev suffix before splitting on ".".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
marker/— PEP 508 environment markers — the second of three PRs building the PEP 508 core (extras → marker → requirement) for the native Go PyPI resolver (RFD 0001, epic rstudio/package-manager#18625). Builds on extras/ (#18638, merged).Closes rstudio/package-manager#18637.
What
internal/pep508— a shared, parser-driven tokenizer (ported from packaging's_tokenizer.py) plus the marker AST + recursive-descent parser (ported from_parser.py). Kept internal because it will be reused by therequirement/PR, which drives the same tokenizer end-to-end and reusespep508.ParseMarkermid-stream (so marker and requirement parsing never drift).marker.Marker—Parse,IsEmpty,String(round-trippable). Zero value is the always-true marker. Unknown variables are a parse error;andbinds tighter thanor; no comparison chaining; dotted/legacy aliases (andpython_implementation) fold to canonical names; theextraliteral is normalized once at parse.marker.Environment(11 PEP 508 vars) +InterpreterIdentity+EnvironmentFromTarget— platform fields mapped from atags.Target(following uv'sTargetTriple), interpreter identity supplied by the caller. Windowsplatform_machineuses Python's realplatform.machine()values (AMD64/ARM64/x86);python_full_versiongets packaging's trailing-+repair.marker.Evaluate(env, extras) bool— single-concrete-environment evaluation with a unified per-comparison dispatch (ported frommarkers.py _eval_op): a version comparison is attempted on the four version-typed keys and falls through to a generic string operator table otherwise.extrauses a set-membership model.Deliberate, documented divergences from packaging
</>→ false,<=/>=→==) rather than packaging's lexicographic operators.extrais a set (active-extras), soextra == "a" and extra == "b"is true when both are active (packaging's singularextramakes it unsatisfiable). Correct for the extras-as-virtual-packages architecture.~=/===applied where no version comparison is possible returnsfalse(bool signature; packaging raises).platform_machinematches Python's reported value, not uv's raw target string.platform_release), not uv's three.Verification
Per commit:
go build,go vet,go test,gofmt -l(empty),golangci-lint run ./...(v2, 0 issues). Correctness anchored on packaging's marker truth-table cases ported as Go table tests.