From 1ab2b2e1906529d78d59ae49db41e0922441af5e Mon Sep 17 00:00:00 2001 From: Taylor Madore Date: Wed, 16 Oct 2024 15:51:06 -0400 Subject: [PATCH] add missing typing hints for the Package class Signed-off-by: Taylor Madore --- pyarn/lockfile.py | 22 +++++++++++++++++----- tests/test_lockfile.py | 9 +++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/pyarn/lockfile.py b/pyarn/lockfile.py index 9f86abe..4845fb0 100644 --- a/pyarn/lockfile.py +++ b/pyarn/lockfile.py @@ -19,7 +19,7 @@ import logging import re from pathlib import Path -from typing import Optional, Pattern +from typing import Any, Optional, Pattern from ply import lex, yacc @@ -35,8 +35,15 @@ class Package: def __init__( - self, name, version, url=None, checksum=None, path=None, dependencies=None, alias=None - ): + self, + name: str, + version: str, + url: Optional[str] = None, + checksum: Optional[str] = None, + path: Optional[str] = None, + dependencies: Optional[dict[str, str]] = None, + alias: Optional[str] = None, + ) -> None: if not name: raise ValueError("Package name was not provided") @@ -52,7 +59,7 @@ def __init__( self.alias = alias @classmethod - def from_dict(cls, raw_name, data): + def from_dict(cls, raw_name: str, data: dict[str, Any]) -> "Package": name_at_version = re.compile(r"(?P@?[^@]+)(?:@(?P[^,]*))?") # _version is the version as declared in package.json, not the resolved version @@ -67,9 +74,14 @@ def from_dict(cls, raw_name, data): if _version: path = cls.get_path_from_version_specifier(_version) + # Ensure the resolved version key exists to appease mypy + version = data.get("version") + if not version: + raise ValueError("Package version was not provided") + return cls( name=name, - version=data.get("version"), + version=version, url=data.get("resolved"), checksum=data.get("integrity"), path=path, diff --git a/tests/test_lockfile.py b/tests/test_lockfile.py index 7f79d7c..084969a 100644 --- a/tests/test_lockfile.py +++ b/tests/test_lockfile.py @@ -123,16 +123,21 @@ def test_packages(): assert packages[0].path is None -def test_packages_no_version(): +def test_lock_packages_no_version(): data = "breakfast@^1.1.1:\n eggs bacon" lock = lockfile.Lockfile.from_str(data) with pytest.raises(ValueError, match="Package version was not provided"): lock.packages() +def test_package_no_version(): + with pytest.raises(ValueError, match="Package version was not provided"): + lockfile.Package("breakfast", None) # type: ignore[arg-type] + + def test_packages_no_name(): with pytest.raises(ValueError, match="Package name was not provided"): - lockfile.Package(None, "1.0.0") + lockfile.Package(None, "1.0.0") # type: ignore[arg-type] def test_packages_url():