-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: have a single function for normalized PyPI package names #1329
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Normalize a PyPI package name to allow consistent label names | ||
|
||
Note we chose `_` instead of `-` as a separator as there are certain | ||
requirements around Bazel labels that we need to consider. | ||
|
||
From the Bazel docs: | ||
> Package names must be composed entirely of characters drawn from the set | ||
> A-Z, a–z, 0–9, '/', '-', '.', and '_', and cannot start with a slash. | ||
|
||
However, due to restrictions on Bazel labels we also cannot allow hyphens. | ||
See https://github.com/bazelbuild/bazel/issues/6841 | ||
|
||
Further, rules_python automatically adds the repository root to the | ||
PYTHONPATH, meaning a package that has the same name as a module is picked | ||
up. We workaround this by prefixing with `<hub_name>_`. | ||
|
||
Alternatively we could require | ||
`--noexperimental_python_import_all_repositories` be set, however this | ||
breaks rules_docker. | ||
See: https://github.com/bazelbuild/bazel/issues/2636 | ||
|
||
Also see Python spec on normalizing package names: | ||
https://packaging.python.org/en/latest/specifications/name-normalization/ | ||
""" | ||
|
||
# Keep in sync with ../pip_install/tools/lib/bazel.py | ||
def normalize_name(name): | ||
"""normalize a PyPI package name and return a valid bazel label. | ||
|
||
Args: | ||
name: str, the PyPI package name. | ||
|
||
Returns: | ||
a normalized name as a string. | ||
""" | ||
name = name.replace("-", "_").replace(".", "_").lower() | ||
if "__" not in name: | ||
return name | ||
|
||
# Handle the edge-case where there are consecutive `-`, `_` or `.` characters, | ||
# which is a valid Python package name. | ||
return "_".join([ | ||
part | ||
for part in name.split("_") | ||
if part | ||
]) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load(":normalize_name_tests.bzl", "normalize_name_test_suite") | ||
|
||
normalize_name_test_suite(name = "normalize_name_tests") |
50 changes: 50 additions & 0 deletions
50
tests/pip_hub_repository/normalize_name/normalize_name_tests.bzl
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"" | ||
|
||
load("@rules_testing//lib:test_suite.bzl", "test_suite") | ||
load("//python/private:normalize_name.bzl", "normalize_name") # buildifier: disable=bzl-visibility | ||
|
||
_tests = [] | ||
|
||
def _test_name_normalization(env): | ||
want = { | ||
input: "friendly_bard" | ||
for input in [ | ||
"friendly-bard", | ||
"Friendly-Bard", | ||
"FRIENDLY-BARD", | ||
"friendly.bard", | ||
"friendly_bard", | ||
"friendly--bard", | ||
"FrIeNdLy-._.-bArD", | ||
] | ||
} | ||
|
||
actual = { | ||
input: normalize_name(input) | ||
for input in want.keys() | ||
} | ||
env.expect.that_dict(actual).contains_exactly(want) | ||
|
||
_tests.append(_test_name_normalization) | ||
|
||
def normalize_name_test_suite(name): | ||
"""Create the test suite. | ||
|
||
Args: | ||
name: the name of the test suite | ||
""" | ||
test_suite(name = name, basic_tests = _tests) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add an empty string here please? And maybe something that is not a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I can do that easily because I am not sure
rules_testing
supports failure testing yet and I am not sure if passing a failure function to thenormalize_name
would be a good idea. Because this is aprivate
function I am not sure how much benefit there is in doing such testing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failure testing a unit test is possible, but annoying to do. You basically have to switch over to treating it like an analysis test (the same as skylib's unittest): implement a rule to call your code that fails, instantiate the target as the target under test, then check for that failure in the target under test.
(Fabian and I tried kicking around ideas for how we could somehow make use whats available today to make it easier, but couldn't come up with anything).
I'm fine with the tests as-is, fwiw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arg. That is all.