Repository: psf/black
File: src/black/files.py (Python)

Code before:

def parse_req_python(requires_python: str) -> Optional[TargetVersion]:
    """Parse the requires-python field to determine Black's target version.

    If the field cannot be parsed, returns None.
    """
    try:
        return parse_req_python_version(requires_python)
    except InvalidVersion:
        pass
    try:
        return parse_req_python_specifier(requires_python)
    except InvalidSpecifier:
        pass
    return None


def parse_req_python_version(requires_python: str) -> Optional[TargetVersion]:
    """Parse a version string (i.e. ``"3.7"``) to a TargetVersion.

    If parsing fails, will raise a packaging.version.InvalidVersion error.
    If the parsed version cannot be mapped to a valid TargetVersion, returns None.
    """
    version = Version(requires_python)
    try:
        return TargetVersion(version.release[1])
    except (IndexError, ValueError):
        return None


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

    If parsing fails, will raise a packaging.specifiers.InvalidSpecifier error.
    If the parsed specifier cannot be mapped to a valid TargetVersion, returns None.
    """
    specifier_set = strip_specifier_set(SpecifierSet(requires_python))

    if not specifier_set:
        return None

    target_version_map = {f"3.{v.value}": v for v in TargetVersion}
    compatible_versions = specifier_set.filter(target_version_map)
    target_version_str: Optional[str] = next(iter(compatible_versions), None)
    if target_version_str is not None:
        return target_version_map.get(target_version_str)
    return None


def strip_specifier_set(specifier_set: SpecifierSet) -> SpecifierSet:
    """Strip irrelevant parts of the specifier set.

Review comment (suggestion):
```suggestion
    specifier_set = strip_specifier_set(SpecifierSet(requires_python))
    if not specifier_set:
        return None
```

Code after:


def parse_req_python_version(requires_python: str) -> Optional[List[TargetVersion]]:
    """Parse a version string (i.e. ``"3.7"``) to a list of TargetVersion.

    If parsing fails, will raise a packaging.version.InvalidVersion error.
    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):
        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.

    If parsing fails, will raise a packaging.specifiers.InvalidSpecifier error.
    If the parsed specifier cannot be mapped to a valid TargetVersion, returns None.
    """
    specifier_set = strip_specifier_set(SpecifierSet(requires_python))
    if not specifier_set:
        return None

    target_version_map = {f"3.{v.value}": v for v in TargetVersion}
    compatible_versions: List[str] = list(specifier_set.filter(target_version_map))
    if compatible_versions:
        return [target_version_map[v] for v in compatible_versions]
    return None


def strip_specifier_set(specifier_set: SpecifierSet) -> SpecifierSet:
    """Strip minor versions for some specifiers in the specifier set.

    For background on version specifiers, see PEP 440:
    https://peps.python.org/pep-0440/#version-specifiers
    """
    specifiers = []
    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: