Skip to content

Commit

Permalink
add missing typing hints for the Package class
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Madore <[email protected]>
  • Loading branch information
taylormadore committed Oct 16, 2024
1 parent 5311bec commit 1ab2b2e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
22 changes: 17 additions & 5 deletions pyarn/lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")

Expand All @@ -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<name>@?[^@]+)(?:@(?P<version>[^,]*))?")

# _version is the version as declared in package.json, not the resolved version
Expand All @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions tests/test_lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 1ab2b2e

Please sign in to comment.