Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.4
hooks:
- id: ruff
- id: ruff-check
args: [--fix]
- id: ruff
- id: ruff-check
args: [--preview, --select=CPY]
- id: ruff-format
- repo: https://github.com/tox-dev/pyproject-fmt
Expand Down
3 changes: 2 additions & 1 deletion src/session_info2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from dataclasses import dataclass, field
from datetime import datetime, timezone
from functools import cached_property
from importlib.metadata import packages_distributions, version
from importlib.metadata import version
from types import MappingProxyType, ModuleType
from typing import TYPE_CHECKING, Any, Literal, TypeAlias

from . import _pu
from ._dists import packages_distributions
from ._repr import repr_mimebundle as _repr_mimebundle
from ._ttl_cache import ttl_cache
from ._widget import widget as _widget
Expand Down
51 changes: 51 additions & 0 deletions src/session_info2/_dists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# SPDX-License-Identifier: MPL-2.0
from __future__ import annotations

import re
from importlib.metadata import Distribution, distributions
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there docs for this distributions function https://docs.python.org/3/library/importlib.metadata.html? Presumably it gets all packages in the current env or?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, right. I’m pretty sure it’s safe given that it’s in __all__ and therefore meant to be exported: python/cpython#110937

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from importlib.metadata import packages_distributions as pkgs_dists
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Generator, Mapping


def packages_distributions() -> Mapping[str, list[str]]:
"""Return a mapping of top-level packages to their distributions.

Unlike :func:`importlib.metadata.packages_distributions`,
this includes editable packages.
"""
pds = dict(pkgs_dists())
for dist in distributions():
for pkg_name in _top_level_editable(dist):
if "." not in pkg_name: # apparently that’s what makes an importable name
pds.setdefault(pkg_name, []).append(dist.name)
return pds


def _top_level_editable(dist: Distribution) -> Generator[str, None, None]:
"""Find top-level packages in an editable distribution."""
for pth_file in dist.files or ():
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow what is in files given https://docs.python.org/3/library/importlib.metadata.html#importlib.metadata.Distribution. It's just all the files in the package or?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah:

image

also a candidate for python/cpython#110937

if len(pth_file.parts) != 1 or pth_file.suffix != ".pth":
continue
for line in pth_file.read_text().splitlines():
if re.match(r"import\s", line):
continue # https://docs.python.org/3/library/site.html
for p in Path(line).iterdir():
yield from _find_top_level(p)


def _find_top_level(root: Path) -> Generator[str, None, None]:
if root.suffix == ".py" and "." not in root.stem and root.is_file():
yield root.stem
return
if "." in root.name or not root.is_dir():
return
if (root / "__init__.py").is_file():
yield root.name
return
for p in root.iterdir():
for pkg in _find_top_level(p):
yield f"{root.name}.{pkg}"
21 changes: 21 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@

from __future__ import annotations

from importlib.metadata import PathDistribution
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from session_info2 import _mods
from session_info2._dists import _top_level_editable

if TYPE_CHECKING:
from pathlib import Path


@pytest.mark.parametrize(
Expand All @@ -18,3 +26,16 @@
)
def test_mods(mod_name: str, expected: list[str]) -> None:
assert list(_mods(mod_name)) == expected


def test_top_level_editable(tmp_path: Path, libdir_test: Path) -> None:
(tmp_path / "fake_editable.pth").write_text(str(libdir_test))
(meta_path := (tmp_path / "fake_editable-0.1.dist-info")).mkdir()
(meta_path / "RECORD").write_text(f"fake_editable.pth,,\n{meta_path}/RECORD,,")

assert set(_top_level_editable(PathDistribution(meta_path))) == {
"basic",
"dep",
"mis_match",
"namespace.package",
}