diff --git a/README.rst b/README.rst index e237dfb..ba7c0d7 100644 --- a/README.rst +++ b/README.rst @@ -79,7 +79,7 @@ API filename is not a valid wheel filename, raises an ``InvalidFilenameError``. ``ParsedWheelFilename`` - A namedtuple representing the components of a wheel filename. It has the + A dataclass representing the components of a wheel filename. It has the following attributes and methods: ``project: str`` diff --git a/src/wheel_filename/__init__.py b/src/wheel_filename/__init__.py index 3a24026..fdd7687 100644 --- a/src/wheel_filename/__init__.py +++ b/src/wheel_filename/__init__.py @@ -23,6 +23,7 @@ import os.path import re from typing import NamedTuple, Optional +from dataclasses import dataclass, field, asdict __version__ = "1.5.0.dev1" __author__ = "John Thorvald Wodder II" @@ -52,14 +53,14 @@ r"\.[Ww][Hh][Ll]".format(PYTHON_TAG_RGX, ABI_TAG_RGX, PLATFORM_TAG_RGX) ) - -class ParsedWheelFilename(NamedTuple): +@dataclass +class ParsedWheelFilename: project: str version: str build: Optional[str] - python_tags: list[str] - abi_tags: list[str] - platform_tags: list[str] + python_tags: list[str] = field(default_factory=list) + abi_tags: list[str] = field(default_factory=list) + platform_tags: list[str] = field(default_factory=list) def __str__(self) -> str: if self.build: @@ -83,6 +84,10 @@ def tag_triples(self) -> Iterator[str]: for plat in self.platform_tags: yield "-".join([py, abi, plat]) + def __iter__(self) -> Iterator: + fields = [self.project, self.version, self.build, self.python_tags, self.abi_tags, self.platform_tags] + return iter(fields) + def parse_wheel_filename( filename: str | bytes | os.PathLike[str] | os.PathLike[bytes], diff --git a/src/wheel_filename/__main__.py b/src/wheel_filename/__main__.py index cab7875..428e9f4 100644 --- a/src/wheel_filename/__main__.py +++ b/src/wheel_filename/__main__.py @@ -4,6 +4,7 @@ import sys from typing import Optional from . import InvalidFilenameError, __version__, parse_wheel_filename +from dataclasses import asdict def main(argv: Optional[list[str]] = None) -> None: @@ -17,7 +18,7 @@ def main(argv: Optional[list[str]] = None) -> None: pwf = parse_wheel_filename(args.filename) except InvalidFilenameError as e: sys.exit(f"wheel-filename: {e}") - print(json.dumps(pwf._asdict(), indent=4)) + print(json.dumps(asdict(pwf), indent=4)) if __name__ == "__main__": diff --git a/test/test_class.py b/test/test_class.py index bf3e6f1..aa50b64 100644 --- a/test/test_class.py +++ b/test/test_class.py @@ -10,10 +10,10 @@ def test_pwf_iterable() -> None: abi_tags=["none"], platform_tags=["any"], ) - project, version, build, pytags, abi_tags, platform_tags = pwf + project, version, build, python_tags, abi_tags, platform_tags = pwf assert project == "project" assert version == "1.2.3" assert build == "1" - assert pytags == ["py2", "py3"] + assert python_tags == ["py2", "py3"] assert abi_tags == ["none"] assert platform_tags == ["any"]