diff --git a/NOTICE b/NOTICE index a39dd38..198c4ab 100644 --- a/NOTICE +++ b/NOTICE @@ -32,3 +32,8 @@ packaging/utils.py's `parse_wheel_filename`, licensed under the Apache License, Version 2.0; and from Astral's uv (https://github.com/astral-sh/uv), specifically the uv-distribution-filename crate, dual-licensed under the Apache License, Version 2.0, or the MIT license. + +The extras/ package adapts PEP 685/503 name-canonicalization from +pypa/packaging (https://github.com/pypa/packaging), specifically +packaging/utils.py's `canonicalize_name`, licensed under the Apache +License, Version 2.0. diff --git a/README.md b/README.md index 491a06b..f260e79 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Part of [RFD 0001 — Native Go PyPI Dependency Resolution](https://github.com/r | [`version/`](https://github.com/rstudio/go-pep440-version) | PEP 440 versions and specifiers | | `requirement/` | PEP 508 dependency specifiers | | `marker/` | PEP 508 environment markers | -| `extras/` | PEP 685 extra-name normalization | +| `extras/` | PEP 685 extra-name normalization (implemented) | | `reqtxt/` | pip `requirements.txt` format | | [`distribution/`](https://github.com/rstudio/python-distribution-parser) | METADATA / wheel parsing | | `wheelname/` | PEP 427 wheel filename → name/version/build/tags (implemented) | diff --git a/extras/doc.go b/extras/doc.go index 458f546..50b7db6 100644 --- a/extras/doc.go +++ b/extras/doc.go @@ -2,5 +2,8 @@ // Package extras normalizes Python extra names per PEP 685. // -// TODO(#18638): implement PEP 685 normalization. +// Normalize lowercases an extra name and collapses any run of "-", "_", or +// "." into a single "-", matching the PEP 503 name-canonicalization rule +// that PEP 685 mandates for extras. The result is deterministic and +// idempotent: normalizing an already-normalized name is a no-op. package extras diff --git a/extras/extras.go b/extras/extras.go new file mode 100644 index 0000000..fbc2cc4 --- /dev/null +++ b/extras/extras.go @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package extras + +import "strings" + +// Normalize canonicalizes a Python extra name per PEP 685: lowercase the +// name, then collapse any run of "-", "_", or "." into a single "-". +// +// This mirrors pypa/packaging's canonicalize_name (PEP 503/685) without +// using a regular expression, since extra-name normalization sits on a +// per-dependency, per-extra hot path during dependency resolution. +func Normalize(name string) string { + var b strings.Builder + b.Grow(len(name)) + + inSep := false + for _, r := range name { + switch r { + case '-', '_', '.': + if !inSep { + b.WriteByte('-') + inSep = true + } + default: + inSep = false + if r >= 'A' && r <= 'Z' { + r += 'a' - 'A' + } + b.WriteRune(r) + } + } + + return b.String() +} diff --git a/extras/extras_test.go b/extras/extras_test.go new file mode 100644 index 0000000..0e56fd6 --- /dev/null +++ b/extras/extras_test.go @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package extras + +import "testing" + +func TestNormalize(t *testing.T) { + cases := []struct { + name string + in string + want string + }{ + {"mixed case with underscore", "Foo_Bar", "foo-bar"}, + {"dot and double underscore", "a.b__c", "a-b-c"}, + {"uppercase with repeated hyphen", "A---B", "a-b"}, + {"already normal is idempotent", "already-normal", "already-normal"}, + {"empty string", "", ""}, + {"leading separators", "__foo", "-foo"}, + {"trailing separators", "foo__", "foo-"}, + {"leading and trailing separators", "..foo..", "-foo-"}, + {"mixed run of separators", "foo_.-bar", "foo-bar"}, + {"uppercase with dots", "Foo.Bar.Baz", "foo-bar-baz"}, + {"single character", "x", "x"}, + {"only separators", "___", "-"}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got := Normalize(c.in) + if got != c.want { + t.Errorf("Normalize(%q) = %q, want %q", c.in, got, c.want) + } + }) + } +} + +func TestNormalizeIdempotent(t *testing.T) { + inputs := []string{"Foo_Bar", "a.b__c", "A---B", "already-normal", "", "__foo", "foo__"} + for _, in := range inputs { + once := Normalize(in) + twice := Normalize(once) + if once != twice { + t.Errorf("Normalize not idempotent for %q: Normalize(%q) = %q, Normalize(%q) = %q", in, in, once, once, twice) + } + } +}