Skip to content

Commit

Permalink
Merge pull request #189 from alexpdev/0.9.2
Browse files Browse the repository at this point in the history
Type Hints and Fixing broken unit tests
  • Loading branch information
alexpdev authored Apr 22, 2024
2 parents 4059abf + 56a31bd commit d50d942
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 53 deletions.
10 changes: 6 additions & 4 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from argparse import Namespace
from hashlib import sha1, sha256 # nosec
from pathlib import Path
from urllib.parse import quote_plus
from urllib.parse import quote

import pyben
import pytest
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_magnet_uri(metafile1):
magnet_link = magnet(metafile1)
meta = pyben.load(metafile1)
announce = meta["announce"]
assert quote_plus(announce) in magnet_link
assert quote(announce) in magnet_link


def test_magnet_hex(metafile1):
Expand Down Expand Up @@ -105,16 +105,18 @@ def test_magnet_no_announce(metafile2):
magnet_link = magnet(metafile2)
assert magnet_link.startswith("magnet")


def test_magnet_web_seed(metafile2):
"""
Test create magnet function scheme.
"""
fake_web_seed = "fake-web-seed"
fake_web_seed = ["fake-web-seed"]
meta = pyben.load(metafile2)
meta["url-list"] = fake_web_seed
pyben.dump(meta, metafile2)
magnet_link = magnet(metafile2)
assert quote_plus(fake_web_seed) in magnet_link
assert quote(fake_web_seed[0]) in magnet_link


def test_magnet_empty():
"""
Expand Down
5 changes: 3 additions & 2 deletions torrentfile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import sys
import logging
from argparse import ArgumentParser, HelpFormatter
from typing import List

from torrentfile import commands
from torrentfile.utils import toggle_debug_mode
Expand Down Expand Up @@ -98,7 +99,7 @@ class TorrentFileHelpFormatter(HelpFormatter):
def __init__(self,
prog: str,
width: int = 45,
max_help_positions: int = 45):
max_help_positions: int = 45) -> None:
"""
Construct HelpFormat class for usage output.
Expand Down Expand Up @@ -194,7 +195,7 @@ def _format_headers(parts: list) -> list:
return parts


def execute(args: list = None) -> list:
def execute(args: List[str] = None) -> List[str]:
"""
Execute program with provided list of arguments.
Expand Down
3 changes: 1 addition & 2 deletions torrentfile/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ def magnet(metafile: str, version: int = 0) -> str:
]
elif "announce" in meta:
announce_args = ["&tr=" + quote_plus(meta["announce"])]


trackers = "".join(announce_args)

Expand All @@ -422,7 +421,7 @@ def magnet(metafile: str, version: int = 0) -> str:
if "url-list" in meta:
web_sources = [
"&ws=" + quote_plus(urllist) for urllist in meta["url-list"]
]
]

web_seed = "".join(web_sources)

Expand Down
3 changes: 1 addition & 2 deletions torrentfile/recheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ def extract(self, path: str, partial: bytearray) -> bytearray:
yield partial
partial = bytearray(0)
if length != read:
for pad in self._gen_padding(partial, length, read):
yield pad
yield from self._gen_padding(partial, length, read)

def _gen_padding(self, partial: bytes, length: int, read=0) -> bytes:
"""
Expand Down
11 changes: 6 additions & 5 deletions torrentfile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ctypes
import shutil
import platform
from typing import Callable, Any, Tuple, List
from pathlib import Path

if platform.system() == "Windows": # pragma: nocover
Expand All @@ -42,15 +43,15 @@ class Memo:
The results of this callable will be cached.
"""

def __init__(self, func):
def __init__(self, func: Callable) -> None:
"""
Construct cache.
"""
self.func = func
self.counter = 0
self.cache = {}

def __call__(self, path: str):
def __call__(self, path: str) -> Any:
"""
Invoke each time memo function is executed.
Expand Down Expand Up @@ -239,7 +240,7 @@ def filelist_total(pathstring: str) -> os.PathLike:
raise MissingPathError


def _filelist_total(path: os.PathLike) -> tuple:
def _filelist_total(path: os.PathLike) -> Tuple[int, List[str]]:
"""
Recursively search directory tree for files.
Expand Down Expand Up @@ -303,7 +304,7 @@ def get_file_list(path: str) -> list:
return filelist


def path_stat(path: str) -> tuple:
def path_stat(path: str) -> Tuple:
"""
Calculate directory statistics.
Expand Down Expand Up @@ -397,7 +398,7 @@ def copypath(source: str, dest: str) -> None:
shutil.copy(source, dest)


def toggle_debug_mode(switch_on: bool):
def toggle_debug_mode(switch_on: bool) -> None:
"""
Switch the environment variable debug indicator on or off.
Expand Down
46 changes: 8 additions & 38 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@

[tox]
envlist =
pylint
pycodestyle
pydocstyle
flake8
pyroma
security
linting
format
twinecheck
py
Expand Down Expand Up @@ -47,48 +42,23 @@ commands =
isort torrentfile tests
autopep8 -r torrentfile tests

[testenv:security]
basepython = python3
deps =
bandit[toml]
commands =
bandit -r -c pyproject.toml torrentfile tests

[testenv:flake8]
[testenv:linting]
basepython = python3
deps =
flake8
mccabe
commands =
flake8 torrentfile tests

[testenv:pyroma]
basepython = python3
deps =
bandit[toml]
pyroma
commands =
pyroma .

[testenv:pycodestyle]
basepython = python3
deps =
pycodestyle
commands =
pycodestyle torrentfile tests

[testenv:pydocstyle]
basepython = python3
deps =
pydocstyle
commands =
pydocstyle torrentfile tests

[testenv:pylint]
basepython = python3
deps =
pylint
pytest
commands =
flake8 torrentfile tests
bandit -r -c pyproject.toml torrentfile tests
pyroma .
pycodestyle torrentfile tests
pydocstyle torrentfile tests
pylint torrentfile tests

[testenv:twinecheck]
Expand Down

0 comments on commit d50d942

Please sign in to comment.