From 11306f27d16dc068968a96a0a330f96839be2b09 Mon Sep 17 00:00:00 2001 From: MTR Date: Thu, 1 Aug 2024 16:50:10 +0500 Subject: [PATCH 1/2] Change NamedTuple to DataClasses --- README.rst | 2 +- src/wheel_filename/__init__.py | 11 ++++++----- src/wheel_filename/__main__.py | 3 ++- test/test_class.py | 14 +++++++------- 4 files changed, 16 insertions(+), 14 deletions(-) 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..e39b7b6 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: 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..2e1b9f3 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 - assert project == "project" - assert version == "1.2.3" - assert build == "1" - assert pytags == ["py2", "py3"] - assert abi_tags == ["none"] - assert platform_tags == ["any"] + + assert pwf.project == "project" + assert pwf.version == "1.2.3" + assert pwf.build == "1" + assert pwf.python_tags == ["py2", "py3"] + assert pwf.abi_tags == ["none"] + assert pwf.platform_tags == ["any"] From 1c1e80742e919e48c2ee473d024aee3b74646814 Mon Sep 17 00:00:00 2001 From: MTR Date: Thu, 1 Aug 2024 17:17:36 +0500 Subject: [PATCH 2/2] Add __iter__ --- src/wheel_filename/__init__.py | 4 ++++ test/test_class.py | 14 +++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/wheel_filename/__init__.py b/src/wheel_filename/__init__.py index e39b7b6..fdd7687 100644 --- a/src/wheel_filename/__init__.py +++ b/src/wheel_filename/__init__.py @@ -84,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/test/test_class.py b/test/test_class.py index 2e1b9f3..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"], ) - - assert pwf.project == "project" - assert pwf.version == "1.2.3" - assert pwf.build == "1" - assert pwf.python_tags == ["py2", "py3"] - assert pwf.abi_tags == ["none"] - assert pwf.platform_tags == ["any"] + project, version, build, python_tags, abi_tags, platform_tags = pwf + assert project == "project" + assert version == "1.2.3" + assert build == "1" + assert python_tags == ["py2", "py3"] + assert abi_tags == ["none"] + assert platform_tags == ["any"]