Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed NamedTuple to DataClasses. #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
15 changes: 10 additions & 5 deletions src/wheel_filename/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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],
Expand Down
3 changes: 2 additions & 1 deletion src/wheel_filename/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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__":
Expand Down
4 changes: 2 additions & 2 deletions test/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]