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 parse_req_python_version by 8% in src/black/files.py #48

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
11 changes: 6 additions & 5 deletions src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ def parse_req_python_version(requires_python: str) -> Optional[list[TargetVersio
If the parsed version cannot be mapped to a valid TargetVersion, returns None.
"""
version = Version(requires_python)
if version.release[0] != 3:
return None
try:
return [TargetVersion(version.release[1])]
except (IndexError, ValueError):
if version.major != 3 or len(version.release) < 2:
return None

minor_version = version.release[1]
if 3 <= minor_version <= 13:
return [TargetVersion(minor_version)]
return None


def parse_req_python_specifier(requires_python: str) -> Optional[list[TargetVersion]]:
"""Parse a specifier string (i.e. ``">=3.7,<3.10"``) to a list of TargetVersion.
Expand Down