Skip to content

feat(marker): PEP 508 environment markers (#18637) - #6

Merged
jonyoder merged 8 commits into
mainfrom
impl-marker
Jul 9, 2026
Merged

jonyoder merged 8 commits into
mainfrom
impl-marker

Conversation

@jonyoder

@jonyoder jonyoder commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

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 the requirement/ PR, which drives the same tokenizer end-to-end and reuses pep508.ParseMarker mid-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; and binds tighter than or; no comparison chaining; dotted/legacy aliases (and python_implementation) fold to canonical names; the extra literal is normalized once at parse.
  • marker.Environment (11 PEP 508 vars) + InterpreterIdentity + EnvironmentFromTarget — platform fields mapped from a tags.Target (following uv's TargetTriple), interpreter identity supplied by the caller. Windows platform_machine uses Python's real platform.machine() values (AMD64/ARM64/x86); python_full_version gets packaging's trailing-+ repair.
  • marker.Evaluate(env, extras) bool — single-concrete-environment evaluation with a unified per-comparison dispatch (ported from markers.py _eval_op): a version comparison is attempted on the four version-typed keys and falls through to a generic string operator table otherwise. extra uses a set-membership model.

Deliberate, documented divergences from packaging

  • String ordered-comparison operators follow the design's rule (</> → false, <=/>= → ==) rather than packaging's lexicographic operators.
  • extra is a set (active-extras), so extra == "a" and extra == "b" is true when both are active (packaging's singular extra makes it unsatisfiable). Correct for the extras-as-virtual-packages architecture.
  • ~=/=== applied where no version comparison is possible returns false (bool signature; packaging raises).
  • Windows platform_machine matches Python's reported value, not uv's raw target string.
  • Version-typed keys follow packaging's set of four (incl. 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.

jonyoder added 8 commits July 9, 2026 12:59
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 ".".
@jonyoder
jonyoder merged commit f567152 into main Jul 9, 2026
2 checks passed
@jonyoder
jonyoder deleted the impl-marker branch July 9, 2026 18:34
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