Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
5 changes: 4 additions & 1 deletion extras/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions extras/extras.go
Original file line number Diff line number Diff line change
@@ -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()
}
46 changes: 46 additions & 0 deletions extras/extras_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}