Repository navigation
docs(utils): properly document is_normalized_name vs canonicalize_name - #1291
Conversation
|
Won't this have a performance impact? Checking something is cheaper than modifying a string, I'd expect? I'll trigger the CI and see if we have a perf test for this. |
|
Pushed Local verification: python -m pytest tests/test_utils.py -q
uvx nox -s lint -- --show-diff-on-failure |
|
Good point. I pushed The implementation now checks the same roundtrip conditions directly: already lowercase, no Local verification:
|
There was a problem hiding this comment.
Though this worries me: the standard clearly indicate the current regex is correct, leading and trailing hyphens are not allowed - that's from https://peps.python.org/pep-0508/#names. The normalization spec was a different PEP: https://peps.python.org/pep-0503/#normalized-names and it does allow leading/trailing hyphens, though I think it's more likely that's an oversight. There are now consolidated into a single place:
https://packaging.python.org/en/latest/specifications/name-normalization (edited to be the correct link now!)
Where you can see the "contradicting" rules next to teach other. Packaging currently implements exactly those rules, though.
(And, in practice, it means you can't put any name in to normalize_name and get a normalized version - leading/trailing symbols break, though the stated guarantee that these are related is currently wrong)
|
Good point — you're right, and I think it reframes this PR. Since PEP 508 names must start and end with an alphanumeric, So I'd rather drop the behavior change: keep the original regex, and instead just fix the docstring, which currently states the roundtrip relationship without noting it only holds for valid names. Happy to reduce this PR to that (revert the regex + the edge-hyphen test, tighten the docstring), or to close it if you'd prefer to touch the docstring separately. Which would you like? |
|
I think fixing the docstring is the right path forward for now. We should look into changing the standards first if we do want the round trip property. |
…d names is_normalized_name documented itself as 'canonicalize_name roundtrips to the same value', but canonicalize_name does not enforce that a name starts and ends with an alphanumeric, so a name like '-not-legal' roundtrips without being a normalized (valid) name. Note the valid-name caveat and add doctests showing the leading-hyphen case.
04edeff to
2d7c2fa
Compare
|
Done — reduced to the docstring change. >>> canonicalize_name("_not_legal")
'-not-legal'
>>> is_normalized_name("-not-legal") # roundtrips, but not a valid name
FalseDiff is |
What does packaging.python.org say, as that's the official spec. |
|
Ahh, sorry, I copied and pasted incorrectly. It was supposed to be:
Instead I linked to https://peps.python.org/pep-0508/#names again, sorry! They sit next to each other there. |
Summary
is_normalized_name()is documented to returnTrueiffcanonicalize_name()would roundtrip to the same value. However, the previous regex-based implementation ([a-z0-9]+(?:-[a-z0-9]+)*) rejected names with leading or trailing hyphens — exactly the stringscanonicalize_name()can produce for inputs like"_not_legal"or"a_".The fix replaces the regex check with the definitive roundtrip check the docs describe.
Fixes #1231.
This pull request was prepared with the assistance of AI, under my direction and review.