From 088f0663e37bac29d0f5fa4f1261f1e7a211e28a Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 8 Nov 2023 09:16:57 -0800 Subject: Revise type annotations per PEP 604 --- pyproject.toml | 1 + src/wikiget/parse.py | 5 +++-- src/wikiget/validations.py | 7 ++++--- src/wikiget/wikiget.py | 5 +++-- tests/test_parse.py | 21 +++++++++++---------- tests/test_validations.py | 13 +++++++------ 6 files changed, 29 insertions(+), 23 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9a075ef..e7ecdd8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -127,6 +127,7 @@ select = [ "E", "EM", "F", + "FA", "FBT", "G", "I", diff --git a/src/wikiget/parse.py b/src/wikiget/parse.py index abe1419..99cb01c 100644 --- a/src/wikiget/parse.py +++ b/src/wikiget/parse.py @@ -15,10 +15,11 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from __future__ import annotations + import fileinput import logging from argparse import Namespace -from typing import Dict from urllib.parse import unquote, urlparse import wikiget @@ -59,7 +60,7 @@ def get_dest(dl: str, args: Namespace) -> File: return File(filename, dest, site_name) -def read_batch_file(batch_file: str) -> Dict[int, str]: +def read_batch_file(batch_file: str) -> dict[int, str]: dl_dict = {} if batch_file == "-": diff --git a/src/wikiget/validations.py b/src/wikiget/validations.py index 6926f06..cfbc5ef 100644 --- a/src/wikiget/validations.py +++ b/src/wikiget/validations.py @@ -15,14 +15,15 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from __future__ import annotations + import hashlib import re -from typing import Optional from wikiget import BLOCKSIZE -def valid_file(search_string: str) -> Optional[re.Match]: +def valid_file(search_string: str) -> re.Match | None: """ Determines if the given string contains a valid file name, defined as a string ending with a '.' and at least one character, beginning with 'File:' or 'Image:', @@ -39,7 +40,7 @@ def valid_file(search_string: str) -> Optional[re.Match]: return file_regex.search(search_string) -def valid_site(search_string: str) -> Optional[re.Match]: +def valid_site(search_string: str) -> re.Match | None: """ Determines if the given string contains a valid site name, defined as a string ending with 'wikipedia.org' or 'wikimedia.org'. This covers all subdomains of those diff --git a/src/wikiget/wikiget.py b/src/wikiget/wikiget.py index 690cbe2..06dc458 100644 --- a/src/wikiget/wikiget.py +++ b/src/wikiget/wikiget.py @@ -15,17 +15,18 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from __future__ import annotations + import argparse import logging import sys -from typing import List import wikiget from wikiget.dl import process_download from wikiget.logging import configure_logging -def parse_args(argv: List[str]) -> argparse.Namespace: +def parse_args(argv: list[str]) -> argparse.Namespace: parser = argparse.ArgumentParser( description=""" A tool for downloading files from MediaWiki sites using the file name or diff --git a/tests/test_parse.py b/tests/test_parse.py index d289d1d..e3bef2d 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -15,10 +15,11 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from __future__ import annotations + import io import logging from pathlib import Path -from typing import Dict import pytest @@ -93,7 +94,7 @@ class TestGetDest: class TestReadBatchFile: @pytest.fixture() - def dl_dict(self, tmp_path: Path) -> Dict[int, str]: + def dl_dict(self, tmp_path: Path) -> dict[int, str]: """ Create and process a test batch file with three lines, returning a dictionary. """ @@ -114,14 +115,14 @@ class TestReadBatchFile: _ = read_batch_file(str(tmp_file)) assert f"Using file '{tmp_file}' for batch download" in caplog.text - def test_batch_file_length(self, dl_dict: Dict[int, str]) -> None: + def test_batch_file_length(self, dl_dict: dict[int, str]) -> None: """ The processed batch dict should have the same number of items as lines in the batch file. """ assert len(dl_dict) == 3 - def test_batch_file_contents(self, dl_dict: Dict[int, str]) -> None: + def test_batch_file_contents(self, dl_dict: dict[int, str]) -> None: """ The processed batch dict should have the correct line numbers and filenames as keys and values, respectively. @@ -130,7 +131,7 @@ class TestReadBatchFile: assert dl_dict == expected_list @pytest.fixture() - def dl_dict_stdin(self, monkeypatch: pytest.MonkeyPatch) -> Dict[int, str]: + def dl_dict_stdin(self, monkeypatch: pytest.MonkeyPatch) -> dict[int, str]: """ Pass three lines of filenames from stdin to read_batch_file and return a dict. """ @@ -150,14 +151,14 @@ class TestReadBatchFile: _ = read_batch_file("-") assert "Using stdin for batch download" in caplog.text - def test_batch_stdin_length(self, dl_dict_stdin: Dict[int, str]) -> None: + def test_batch_stdin_length(self, dl_dict_stdin: dict[int, str]) -> None: """ The processed batch dict should have the same number of items as lines in the input. """ assert len(dl_dict_stdin) == 3 - def test_batch_stdin_contents(self, dl_dict_stdin: Dict[int, str]) -> None: + def test_batch_stdin_contents(self, dl_dict_stdin: dict[int, str]) -> None: """ The processed batch dict should have the correct line numbers and filenames as keys and values, respectively. @@ -166,7 +167,7 @@ class TestReadBatchFile: assert dl_dict_stdin == expected_list @pytest.fixture() - def dl_dict_with_comment(self, tmp_path: Path) -> Dict[int, str]: + def dl_dict_with_comment(self, tmp_path: Path) -> dict[int, str]: """ Create and process a test batch file with four lines, one of which is commented out and another of which is blank, and return a dictionary. @@ -176,7 +177,7 @@ class TestReadBatchFile: return read_batch_file(str(tmp_file)) def test_batch_file_with_comment_length( - self, dl_dict_with_comment: Dict[int, str] + self, dl_dict_with_comment: dict[int, str] ) -> None: """ The processed batch dict should contain the same number of items as uncommented @@ -185,7 +186,7 @@ class TestReadBatchFile: assert len(dl_dict_with_comment) == 2 def test_batch_file_with_comment_contents( - self, dl_dict_with_comment: Dict[int, str] + self, dl_dict_with_comment: dict[int, str] ) -> None: """ The processed batch dict should have the correct line numbers and filenames as diff --git a/tests/test_validations.py b/tests/test_validations.py index a1c57b5..a8fbf1b 100644 --- a/tests/test_validations.py +++ b/tests/test_validations.py @@ -15,9 +15,10 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from __future__ import annotations + from pathlib import Path from re import Match -from typing import Optional import pytest @@ -33,7 +34,7 @@ class TestSiteInput: "en.wikimpedia.org", ], ) - def invalid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + def invalid_input(self, request: pytest.FixtureRequest) -> Match | None: return valid_site(request.param) @pytest.fixture( @@ -44,7 +45,7 @@ class TestSiteInput: "meta.wikimedia.org", ], ) - def valid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + def valid_input(self, request: pytest.FixtureRequest) -> Match | None: return valid_site(request.param) def test_invalid_site_input(self, invalid_input: None) -> None: @@ -58,7 +59,7 @@ class TestSiteInput: class TestFileRegex: @pytest.fixture() - def file_match(self) -> Optional[Match]: + def file_match(self) -> Match | None: """ File regex should return a match object with match groups corresponding to the file prefix and name. @@ -87,7 +88,7 @@ class TestFileInput: "Fil:Example.jpg", ], ) - def invalid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + def invalid_input(self, request: pytest.FixtureRequest) -> Match | None: return valid_file(request.param) @pytest.fixture( @@ -100,7 +101,7 @@ class TestFileInput: "File:A (1).jpeg", ], ) - def valid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + def valid_input(self, request: pytest.FixtureRequest) -> Match | None: return valid_file(request.param) def test_invalid_file_input(self, invalid_input: None) -> None: -- cgit v1.2.3