-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(python): impove package name normalization #9290
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
Merged
DmitriyLewen
merged 8 commits into
aquasecurity:main
from
DmitriyLewen:fix/python/normilize-pkg-name-by-pep0503
Aug 1, 2025
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
886b1c5
fix: use regexp for replace
DmitriyLewen fcd0f4a
fix: replace `-` to `_` for pkgName in dist-info
DmitriyLewen 1564215
fix: linter errors
DmitriyLewen b28ac24
test(pip): update related test cases
DmitriyLewen 669832e
refactor(python): select lower case for normalize package name
DmitriyLewen f4feb0e
fix(pip): use lowercase and original case for dist-info
DmitriyLewen 19c179e
test: simplify METADATA files
DmitriyLewen 4ce96c7
fix: close metadata file
DmitriyLewen 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| package python | ||
|
|
||
| import "strings" | ||
|
|
||
| // NormalizePkgName normalizes the package name based on pep-0426 | ||
| func NormalizePkgName(name string) string { | ||
| // The package names don't use `_`, `.` or upper case, but dependency names can contain them. | ||
| // We need to normalize those names. | ||
| // cf. https://peps.python.org/pep-0426/#name | ||
| name = strings.ToLower(name) // e.g. https://github.com/python-poetry/poetry/blob/c8945eb110aeda611cc6721565d7ad0c657d453a/poetry.lock#L819 | ||
| name = strings.ReplaceAll(name, "_", "-") // e.g. https://github.com/python-poetry/poetry/blob/c8945eb110aeda611cc6721565d7ad0c657d453a/poetry.lock#L50 | ||
| name = strings.ReplaceAll(name, ".", "-") // e.g. https://github.com/python-poetry/poetry/blob/c8945eb110aeda611cc6721565d7ad0c657d453a/poetry.lock#L816 | ||
| import ( | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| var normalizePkgNameRegexp = regexp.MustCompile(`[-_.]+`) | ||
|
|
||
| // NormalizePkgName normalizes the package name based on pep-0503 (with the option to disable conversion to lowercase). | ||
| // cf. https://peps.python.org/pep-0503/#normalized-names: | ||
| // The name should be lowercased with all runs of the characters ., -, or _ replaced with a single - character. | ||
| func NormalizePkgName(name string, inLowerCase bool) string { | ||
| name = normalizePkgNameRegexp.ReplaceAllString(name, "-") | ||
|
|
||
| // pep-0503 requires that all packages names MUST be lowercase. | ||
| // But there are cases where the original case should be preserved (e.g. dist-info dir names). | ||
| if inLowerCase { | ||
| name = strings.ToLower(name) | ||
| } | ||
| return name | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
1 change: 1 addition & 0 deletions
1
pkg/fanal/analyzer/language/python/pip/testdata/happy/requirements.txt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| annotated-types==0.7.0 | ||
| click==8.0.0 | ||
| Flask==2.0.0 | ||
| itsdangerous==2.0.0 | ||
|
|
||
28 changes: 28 additions & 0 deletions
28
...n/pip/testdata/libs/common-dir/lib/site-packages/annotated_types-0.7.0.dist-info/METADATA
This file contains hidden or 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,28 @@ | ||
| Metadata-Version: 2.3 | ||
| Name: annotated-types | ||
| Version: 0.7.0 | ||
| Summary: Reusable constraint types to use with typing.Annotated | ||
| Project-URL: Homepage, https://github.com/annotated-types/annotated-types | ||
| Project-URL: Source, https://github.com/annotated-types/annotated-types | ||
| Project-URL: Changelog, https://github.com/annotated-types/annotated-types/releases | ||
| Author-email: Adrian Garcia Badaracco <[email protected]>, Samuel Colvin <[email protected]>, Zac Hatfield-Dodds <[email protected]> | ||
| License-File: LICENSE | ||
| Classifier: Development Status :: 4 - Beta | ||
| Classifier: Environment :: Console | ||
| Classifier: Environment :: MacOS X | ||
| Classifier: Intended Audience :: Developers | ||
| Classifier: Intended Audience :: Information Technology | ||
| Classifier: License :: OSI Approved :: MIT License | ||
| Classifier: Operating System :: POSIX :: Linux | ||
| Classifier: Operating System :: Unix | ||
| Classifier: Programming Language :: Python :: 3 :: Only | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Programming Language :: Python :: 3.11 | ||
| Classifier: Programming Language :: Python :: 3.12 | ||
| Classifier: Topic :: Software Development :: Libraries :: Python Modules | ||
| Classifier: Typing :: Typed | ||
| Requires-Python: >=3.8 | ||
| Requires-Dist: typing-extensions>=4.0.0; python_version < '3.9' | ||
| Description-Content-Type: text/markdown |
28 changes: 28 additions & 0 deletions
28
...ata/libs/python-dir/lib/python3.10/site-packages/annotated_types-0.7.0.dist-info/METADATA
This file contains hidden or 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,28 @@ | ||
| Metadata-Version: 2.3 | ||
| Name: annotated-types | ||
| Version: 0.7.0 | ||
| Summary: Reusable constraint types to use with typing.Annotated | ||
| Project-URL: Homepage, https://github.com/annotated-types/annotated-types | ||
| Project-URL: Source, https://github.com/annotated-types/annotated-types | ||
| Project-URL: Changelog, https://github.com/annotated-types/annotated-types/releases | ||
| Author-email: Adrian Garcia Badaracco <[email protected]>, Samuel Colvin <[email protected]>, Zac Hatfield-Dodds <[email protected]> | ||
| License-File: LICENSE | ||
| Classifier: Development Status :: 4 - Beta | ||
| Classifier: Environment :: Console | ||
| Classifier: Environment :: MacOS X | ||
| Classifier: Intended Audience :: Developers | ||
| Classifier: Intended Audience :: Information Technology | ||
| Classifier: License :: OSI Approved :: MIT License | ||
| Classifier: Operating System :: POSIX :: Linux | ||
| Classifier: Operating System :: Unix | ||
| Classifier: Programming Language :: Python :: 3 :: Only | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Programming Language :: Python :: 3.11 | ||
| Classifier: Programming Language :: Python :: 3.12 | ||
| Classifier: Topic :: Software Development :: Libraries :: Python Modules | ||
| Classifier: Typing :: Typed | ||
| Requires-Python: >=3.8 | ||
| Requires-Dist: typing-extensions>=4.0.0; python_version < '3.9' | ||
| Description-Content-Type: text/markdown |
Oops, something went wrong.
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.
Is this file closed somewhere?
metadataFile.Close().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.
Nice catch!
added in 4ce96c7