Skip to content

Commit

Permalink
Use PackageMetadata instead of Message
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Oct 12, 2024
1 parent 6bd2684 commit 236b980
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
from enum import Enum, auto
from functools import partial
from importlib import metadata as importlib_metadata
from importlib.metadata import Distribution
from importlib.metadata import Distribution, PackageMetadata
from pathlib import Path
from typing import TYPE_CHECKING, cast
from typing import cast

import tomli
from prettytable import ALL as RULE_ALL
Expand All @@ -50,10 +50,6 @@
from prettytable import NONE as RULE_NONE
from prettytable import PrettyTable

if TYPE_CHECKING: # pragma: no cover
from email.message import Message


open = open # allow monkey patching

__pkgname__ = "pip-licenses"
Expand Down Expand Up @@ -96,7 +92,7 @@
)


def extract_homepage(metadata: Message) -> str | None:
def extract_homepage(metadata: PackageMetadata) -> str | None:
"""Extracts the homepage attribute from the package metadata.
Not all python packages have defined a home-page attribute.
Expand Down Expand Up @@ -151,7 +147,7 @@ def normalize_pkg_name(pkg_name: str) -> str:
return PATTERN_DELIMITER.sub("-", pkg_name).lower()


METADATA_KEYS: dict[str, list[Callable[[Message], str | None]]] = {
METADATA_KEYS: dict[str, list[Callable[[PackageMetadata], str | None]]] = {
"home-page": [extract_homepage],
"author": [
lambda metadata: metadata.get("author"),
Expand Down Expand Up @@ -235,9 +231,7 @@ def get_pkg_info(pkg: Distribution) -> dict[str, str | list[str]]:
for field_name, field_selector_fns in METADATA_KEYS.items():
value = None
for field_selector_fn in field_selector_fns:
# Type hint of `Distribution.metadata` states `PackageMetadata`
# but it's actually of type `email.Message`
value = field_selector_fn(metadata) # type: ignore
value = field_selector_fn(metadata)
if value:
break
pkg_info[field_name] = value or LICENSE_UNKNOWN
Expand Down

0 comments on commit 236b980

Please sign in to comment.