Skip to content

Detection

License detection for one installed distribution — declared metadata first, a bundled-license-file text-matching fallback second. See How detection works for the full priority order and rationale.

trustedlicenses.DistributionLicence(name, keys, categories, source, suggested=frozenset(), version='') dataclass

The licenses detected for one installed distribution.

Attributes:

Name Type Description
name str

Canonical (PEP 503 normalised) distribution name.

keys frozenset[str]

SPDX license identifiers detected for the distribution (e.g. "MIT", "Apache-2.0").

categories frozenset[str]

Categories of keys.

source str

Where the detection came from, for a failure report.

suggested frozenset[tuple[str, str]]

(declared statement, corrected SPDX id) pairs :func:_correct_license_statement produced for this distribution's declared statements. Populated whenever keys came back empty, regardless of whether anything ends up trusting it -- purely informational at this layer; see the module docstring.

version str

The installed version, so a later, opt-in index check knows what "newer" means. Empty when unknown.

trustedlicenses.canonical_name(raw)

Normalise a distribution name per PEP 503.

Parameters:

Name Type Description Default
raw str

Distribution name as written in metadata or configuration.

required

Returns:

Type Description
str

The lower-cased name with runs of -, _ and . collapsed to -.

Source code in src/trustedlicenses/detection.py
128
129
130
131
132
133
134
135
136
137
def canonical_name(raw: str) -> str:
    """Normalise a distribution name per PEP 503.

    Args:
        raw: Distribution name as written in metadata or configuration.

    Returns:
        The lower-cased name with runs of ``-``, ``_`` and ``.`` collapsed to ``-``.
    """
    return raw.strip().lower().replace("_", "-").replace(".", "-")

trustedlicenses.inspect_distribution(dist, name=None)

Detect the licenses of one installed distribution.

Parameters:

Name Type Description Default
dist Distribution

The installed distribution.

required
name str | None

Its canonical name. Computed from dist's own metadata when omitted.

None

Returns:

Type Description
DistributionLicence

What was detected, and where it was detected from.

Source code in src/trustedlicenses/detection.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
def inspect_distribution(dist: Distribution, name: str | None = None) -> DistributionLicence:
    """Detect the licenses of one installed distribution.

    Args:
        dist: The installed distribution.
        name: Its canonical name. Computed from ``dist``'s own metadata when omitted.

    Returns:
        What was detected, and where it was detected from.
    """
    resolved_name = name or canonical_name(dist.metadata["Name"] or "")

    statements = _declared_statements(dist)
    declared_keys = _keys_from_declared(statements)
    if declared_keys:
        return DistributionLicence(
            name=resolved_name,
            keys=frozenset(declared_keys),
            categories=frozenset(_categories(declared_keys)),
            source="declared metadata",
            version=dist.version,
        )

    suggested = _suggested_corrections(statements)

    files = _licence_files(_dist_info_dir(dist))
    if files:
        keys = _keys_from_licence_files(files)
        source = "license files: " + ", ".join(sorted({path.name for path in files}))
    else:
        keys = set()
        source = "no license information found"
    return DistributionLicence(
        name=resolved_name,
        keys=frozenset(keys),
        categories=frozenset(_categories(keys)),
        source=source,
        suggested=suggested,
        version=dist.version,
    )

trustedlicenses.inspect_installed(name)

Detect the licenses of an installed distribution by name.

Parameters:

Name Type Description Default
name str

The distribution's project name, as installed (any case/separator).

required

Returns:

Type Description
DistributionLicence

What was detected, and where it was detected from.

Raises:

Type Description
PackageNotFoundError

No such distribution is installed.

Source code in src/trustedlicenses/detection.py
442
443
444
445
446
447
448
449
450
451
452
453
454
def inspect_installed(name: str) -> DistributionLicence:
    """Detect the licenses of an installed distribution by name.

    Args:
        name: The distribution's project name, as installed (any case/separator).

    Returns:
        What was detected, and where it was detected from.

    Raises:
        importlib.metadata.PackageNotFoundError: No such distribution is installed.
    """
    return inspect_distribution(distribution(name), name=canonical_name(name))