Skip to content

Commit

Permalink
Make NamedTuple names more descriptive
Browse files Browse the repository at this point in the history
- _Package to _OutdatedPackageInfo
- _Column to _ColumnSpec
  • Loading branch information
realshouzy committed Mar 5, 2024
1 parent 7b3c130 commit 05080a2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
38 changes: 22 additions & 16 deletions pip_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def ask(self, prompt: str) -> str:
_ask_to_install: partial[str] = partial(_InteractiveAsker().ask, prompt="Upgrade now?")


class _Package(NamedTuple):
class _OutdatedPackageInfo(NamedTuple):
name: str
version: str
latest_version: str
Expand All @@ -265,13 +265,13 @@ def from_dct(cls, dct: dict[str, str]) -> Self:
)


def freeze_outdated_packages(file: Path, packages: list[_Package]) -> None:
def freeze_outdated_packages(file: Path, packages: list[_OutdatedPackageInfo]) -> None:
outdated_packages: str = "\n".join(f"{pkg.name}=={pkg.version}" for pkg in packages)
file.write_text(f"{outdated_packages}\n", encoding="utf-8")


def update_packages(
packages: list[_Package],
packages: list[_OutdatedPackageInfo],
forwarded: list[str],
*,
continue_on_fail: bool,
Expand All @@ -292,7 +292,7 @@ def update_packages(

def _get_outdated_packages(
forwarded: list[str],
) -> list[_Package]:
) -> list[_OutdatedPackageInfo]:
command: list[str] = [
*_PIP_CMD,
"list",
Expand All @@ -302,34 +302,40 @@ def _get_outdated_packages(
*forwarded,
]
output: str = subprocess.check_output(command).decode("utf-8") # nosec
packages: list[_Package] = [_Package.from_dct(pkg) for pkg in json.loads(output)]
packages: list[_OutdatedPackageInfo] = [
_OutdatedPackageInfo.from_dct(pkg) for pkg in json.loads(output)
]
return packages


class _Column(NamedTuple):
class _ColumnSpec(NamedTuple):
title: str
field: str


# nicer headings for the columns in the oudated package table
_DEFAULT_COLUMNS: Final[tuple[_Column, ...]] = (
_Column("Package", "name"),
_Column("Version", "version"),
_Column("Latest", "latest_version"),
_Column("Type", "latest_filetype"),
_DEFAULT_COLUMNS: Final[tuple[_ColumnSpec, ...]] = (
_ColumnSpec("Package", "name"),
_ColumnSpec("Version", "version"),
_ColumnSpec("Latest", "latest_version"),
_ColumnSpec("Type", "latest_filetype"),
)

# Next two functions describe how to collect data for the table.
# Note how they are not concerned with columns widths.


def _extract_column(data: list[_Package], field: str, title: str) -> list[str]:
def _extract_column(
data: list[_OutdatedPackageInfo],
field: str,
title: str,
) -> list[str]:
return [title, *[getattr(item, field) for item in data]]


def _extract_table(
outdated: list[_Package],
columns: tuple[_Column, ...] = _DEFAULT_COLUMNS,
outdated: list[_OutdatedPackageInfo],
columns: tuple[_ColumnSpec, ...] = _DEFAULT_COLUMNS,
) -> list[list[str]]:
return [_extract_column(outdated, field, title) for title, field in columns]

Expand Down Expand Up @@ -379,7 +385,7 @@ def main(argv: Sequence[str] | None = None) -> int:
logger.error("--auto and --interactive cannot be used together")
return 1

outdated: list[_Package] = _get_outdated_packages(list_args)
outdated: list[_OutdatedPackageInfo] = _get_outdated_packages(list_args)
logger.debug("Outdated packages: %s", outdated)

if not outdated and not args.raw:
Expand All @@ -406,7 +412,7 @@ def main(argv: Sequence[str] | None = None) -> int:
)
return 0

selected: list[_Package] = []
selected: list[_OutdatedPackageInfo] = []
for pkg in outdated:
logger.info(
"%s==%s is available (you have %s)",
Expand Down
47 changes: 27 additions & 20 deletions tests/pip_review_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@


@pytest.fixture()
def test_packages() -> list[pip_review._Package]:
def test_packages() -> list[pip_review._OutdatedPackageInfo]:
return [
pip_review._Package("test1", "1.0.0", "1.1.0", "wheel"),
pip_review._Package("test2", "1.9.9", "2.0.0", "wheel"),
pip_review._OutdatedPackageInfo("test1", "1.0.0", "1.1.0", "wheel"),
pip_review._OutdatedPackageInfo("test2", "1.9.9", "2.0.0", "wheel"),
]


Expand Down Expand Up @@ -118,18 +118,22 @@ def test_subprocess_output() -> bytes:
pytest.param(
pip_review._DEFAULT_COLUMNS,
(
pip_review._Column("Package", "name"),
pip_review._Column("Version", "version"),
pip_review._Column("Latest", "latest_version"),
pip_review._Column("Type", "latest_filetype"),
pip_review._ColumnSpec("Package", "name"),
pip_review._ColumnSpec("Version", "version"),
pip_review._ColumnSpec("Latest", "latest_version"),
pip_review._ColumnSpec("Type", "latest_filetype"),
),
id="_DEFAULT_COLUMNS",
),
],
)
def test_constants(
constant: str | frozenset[str] | tuple[str, ...] | tuple[pip_review._Column, ...],
expected: str | frozenset[str] | tuple[str, ...] | tuple[pip_review._Column, ...],
constant: (
str | frozenset[str] | tuple[str, ...] | tuple[pip_review._ColumnSpec, ...]
),
expected: (
str | frozenset[str] | tuple[str, ...] | tuple[pip_review._ColumnSpec, ...]
),
) -> None:
assert constant == expected

Expand Down Expand Up @@ -425,11 +429,11 @@ def test_ask_to_install_with_last_answer_and_invalid_input(last_answer: str) ->


def test_package_is_tuple() -> None:
assert issubclass(pip_review._Package, tuple)
assert issubclass(pip_review._OutdatedPackageInfo, tuple)


def test_package_fields() -> None:
assert pip_review._Package._fields == (
assert pip_review._OutdatedPackageInfo._fields == (
"name",
"version",
"latest_version",
Expand Down Expand Up @@ -489,12 +493,12 @@ def test_package_fields() -> None:
],
)
def test_package_from_dct(dct: dict[str, str], expected: tuple[str, ...]) -> None:
assert pip_review._Package.from_dct(dct) == expected
assert pip_review._OutdatedPackageInfo.from_dct(dct) == expected


def test_freeze_outdated_packages(
tmp_path: Path,
test_packages: list[pip_review._Package],
test_packages: list[pip_review._OutdatedPackageInfo],
) -> None:
tmp_file: Path = tmp_path / "outdated.txt"
pip_review.freeze_outdated_packages(tmp_file, test_packages)
Expand All @@ -507,7 +511,7 @@ def test_freeze_outdated_packages(
)
def test_update_packages_continue_on_fail_set_to_false(
forwarded: list[str],
test_packages: list[pip_review._Package],
test_packages: list[pip_review._OutdatedPackageInfo],
) -> None:
with mock.patch("subprocess.call") as mock_subprocess_call:
pip_review.update_packages(
Expand Down Expand Up @@ -537,7 +541,7 @@ def test_update_packages_continue_on_fail_set_to_false(
)
def test_update_packages_continue_on_fail_set_to_true(
forwarded: list[str],
test_packages: list[pip_review._Package],
test_packages: list[pip_review._OutdatedPackageInfo],
) -> None:
with mock.patch("subprocess.call") as mock_subprocess_call:
pip_review.update_packages(
Expand Down Expand Up @@ -574,21 +578,21 @@ def test_update_packages_continue_on_fail_set_to_true(


def test_get_outdated_packages(
test_packages: list[pip_review._Package],
test_packages: list[pip_review._OutdatedPackageInfo],
test_subprocess_output: bytes,
) -> None:
with mock.patch(
"subprocess.check_output",
return_value=test_subprocess_output,
):
outdated_packages: list[pip_review._Package] = (
outdated_packages: list[pip_review._OutdatedPackageInfo] = (
pip_review._get_outdated_packages([])
)
assert outdated_packages == test_packages


def test_column_fields() -> None:
assert pip_review._Column._fields == (
assert pip_review._ColumnSpec._fields == (
"title",
"field",
)
Expand All @@ -603,15 +607,18 @@ def test_column_fields() -> None:
"latest_filetype",
],
)
def test_extract_column(test_packages: list[pip_review._Package], field: str) -> None:
def test_extract_column(
test_packages: list[pip_review._OutdatedPackageInfo],
field: str,
) -> None:
assert pip_review._extract_column(test_packages, field, "TEST") == [
"TEST",
getattr(test_packages[0], field),
getattr(test_packages[1], field),
]


def test_extract_table(test_packages: list[pip_review._Package]) -> None:
def test_extract_table(test_packages: list[pip_review._OutdatedPackageInfo]) -> None:
expected_result: list[list[str]] = [
["Package", "test1", "test2"],
["Version", "1.0.0", "1.9.9"],
Expand Down

0 comments on commit 05080a2

Please sign in to comment.