Skip to content
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

⚡️ Speed up function strip_specifier_set by 11% in src/black/files.py #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 18 additions & 14 deletions src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,26 @@ def strip_specifier_set(specifier_set: SpecifierSet) -> SpecifierSet:
https://peps.python.org/pep-0440/#version-specifiers
"""
specifiers = []
specifier_add = specifiers.append

for s in specifier_set:
if "*" in str(s):
specifiers.append(s)
elif s.operator in ["~=", "==", ">=", "==="]:
version = Version(s.version)
stripped = Specifier(f"{s.operator}{version.major}.{version.minor}")
specifiers.append(stripped)
elif s.operator == ">":
version = Version(s.version)
if len(version.release) > 2:
s = Specifier(f">={version.major}.{version.minor}")
specifiers.append(s)
s_operator = s.operator
if "*" in s.version:
specifier_add(s)
elif s_operator in ["~=", "==", ">=", "==="]:
version_parts = s.version.split(".")
major_version = version_parts[0]
minor_version = version_parts[1] if len(version_parts) > 1 else "0"
stripped = Specifier(f"{s_operator}{major_version}.{minor_version}")
specifier_add(stripped)
elif s_operator == ">":
version_parts = s.version.split(".")
if len(version_parts) > 2:
s = Specifier(f">={version_parts[0]}.{version_parts[1]}")
specifier_add(s)
else:
specifiers.append(s)

return SpecifierSet(",".join(str(s) for s in specifiers))
specifier_add(s)
return SpecifierSet(",".join(map(str, specifiers)))


@lru_cache
Expand Down