Repository: pypa/pip
File: src/pip/_internal/commands/show.py (Python)

Code before:

        requires = sorted(
            # Avoid duplicates in requirements (e.g. due to environment markers).
            {req.name for req in dist.iter_dependencies()},
            key=str.lower,
        )
        required_by = sorted(_get_requiring_packages(dist), key=str.lower)

        try:
            entry_points_text = dist.read_text("entry_points.txt")
            entry_points = entry_points_text.splitlines(keepends=False)
        except FileNotFoundError:
            entry_points = []

        files_iter = dist.iter_declared_entries()
        if files_iter is None:
            files: Optional[List[str]] = None
        else:
            files = sorted(files_iter)

        metadata = dist.metadata

        project_urls = {}
        for url in metadata.get_all("Project-URL", []):
            url_label, url = url.split(",", maxsplit=1)
            project_urls[url_label.strip()] = url.strip()

        homepage = metadata.get("Home-page", "")
        if not homepage:
            # It's common that there is a "homepage" Project-URL, but Home-page
            # remains unset (especially as PEP 621 doesn't surface the field).
            #
            # This logic was taken from PyPI's codebase.
            for url_label, url in project_urls.items():
                normalized_label = (
                    url_label.casefold().replace("-", "").replace("_", "")
                )
                if normalized_label == "homepage":
                    homepage = url
                    break

        yield _PackageInfo(
            name=dist.raw_name,
            version=str(dist.version),
            location=dist.location or "",
            editable_project_location=dist.editable_project_location,
            requires=requires,
            required_by=required_by,
            installer=dist.installer,
            metadata_version=dist.metadata_version or "",
            classifiers=metadata.get_all("Classifier", []),

Review comment (suggestion):
This parsing code does not need to be executed if `Home-page` is present. Let’s move it into the conditional block. Also I don’t think the dict really needs to be built.

Code after:

        requires = sorted(
            # Avoid duplicates in requirements (e.g. due to environment markers).
            {req.name for req in dist.iter_dependencies()},
            key=str.lower,
        )
        required_by = sorted(_get_requiring_packages(dist), key=str.lower)

        try:
            entry_points_text = dist.read_text("entry_points.txt")
            entry_points = entry_points_text.splitlines(keepends=False)
        except FileNotFoundError:
            entry_points = []

        files_iter = dist.iter_declared_entries()
        if files_iter is None:
            files: Optional[List[str]] = None
        else:
            files = sorted(files_iter)

        metadata = dist.metadata

        project_urls = metadata.get_all("Project-URL", [])
        homepage = metadata.get("Home-page", "")
        if not homepage:
            # It's common that there is a "homepage" Project-URL, but Home-page
            # remains unset (especially as PEP 621 doesn't surface the field).
            #
            # This logic was taken from PyPI's codebase.
            for url in project_urls:
                url_label, url = url.split(",", maxsplit=1)
                normalized_label = (
                    url_label.casefold().replace("-", "").replace("_", "").strip()
                )
                if normalized_label == "homepage":
                    homepage = url.strip()
                    break

        yield _PackageInfo(
            name=dist.raw_name,
            version=str(dist.version),
            location=dist.location or "",
            editable_project_location=dist.editable_project_location,
            requires=requires,
            required_by=required_by,
            installer=dist.installer,
            metadata_version=dist.metadata_version or "",
            classifiers=metadata.get_all("Classifier", []),
            summary=metadata.get("Summary", ""),
            homepage=homepage,
            project_urls=project_urls,