Improve type definitions and styles (#869)

- exclude .reviewdog.yml from source distribution
- Add more type hints
- protective check for self.config in SettingsClass

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura
2024-12-24 14:45:04 +09:00
committed by GitHub
parent f914397685
commit eb164e552b
4 changed files with 21 additions and 12 deletions

View File

@@ -19,4 +19,5 @@ recursive-include tools *.py
prune .github prune .github
exclude .gitignore exclude .gitignore
exclude .readthedocs.yml exclude .readthedocs.yml
exclude .reviewdog.yml
exclude azure-pipelines.yml exclude azure-pipelines.yml

View File

@@ -23,7 +23,7 @@ import posixpath
from dataclasses import dataclass, field from dataclasses import dataclass, field
from itertools import islice, zip_longest from itertools import islice, zip_longest
from logging import getLogger from logging import getLogger
from typing import Dict, Iterable, List, Optional, Set, Tuple from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
from xml.etree.ElementTree import Element # noqa from xml.etree.ElementTree import Element # noqa
from defusedxml import ElementTree from defusedxml import ElementTree
@@ -450,11 +450,11 @@ class QtArchives:
self._parse_update_xmls(update_xmls, target_packages) self._parse_update_xmls(update_xmls, target_packages)
def _download_update_xml(self, update_xml_path, silent=False): def _download_update_xml(self, update_xml_path: str, silent: bool = False) -> Optional[str]:
"""Hook for unit test.""" """Hook for unit test."""
if not Settings.ignore_hash: if not Settings.ignore_hash:
try: try:
xml_hash = get_hash(update_xml_path, Settings.hash_algorithm, self.timeout) xml_hash: Optional[bytes] = get_hash(update_xml_path, Settings.hash_algorithm, self.timeout)
except ChecksumDownloadFailure: except ChecksumDownloadFailure:
if silent: if silent:
return None return None
@@ -469,7 +469,9 @@ class QtArchives:
xml_hash = None xml_hash = None
return getUrl(posixpath.join(self.base, update_xml_path), self.timeout, xml_hash) return getUrl(posixpath.join(self.base, update_xml_path), self.timeout, xml_hash)
def _parse_update_xml(self, os_target_folder, update_xml_text, target_packages: Optional[ModuleToPackage]): def _parse_update_xml(
self, os_target_folder: str, update_xml_text: str, target_packages: Optional[ModuleToPackage]
) -> None:
if not target_packages: if not target_packages:
target_packages = ModuleToPackage({}) target_packages = ModuleToPackage({})
update_xml = Updates.fromstring(self.base, update_xml_text) update_xml = Updates.fromstring(self.base, update_xml_text)
@@ -509,7 +511,7 @@ class QtArchives:
) )
) )
def _parse_update_xmls(self, update_xmls, target_packages: Optional[ModuleToPackage]): def _parse_update_xmls(self, update_xmls: list[UpdateXmls], target_packages: Optional[ModuleToPackage]) -> None:
if not target_packages: if not target_packages:
target_packages = ModuleToPackage({}) target_packages = ModuleToPackage({})
for update_xml in update_xmls: for update_xml in update_xmls:
@@ -700,7 +702,7 @@ class ToolArchives(QtArchives):
def _get_archives(self): def _get_archives(self):
self._get_archives_base(self.tool_name, None) self._get_archives_base(self.tool_name, None)
def _parse_update_xml(self, os_target_folder, update_xml_text, *ignored): def _parse_update_xml(self, os_target_folder: str, update_xml_text: str, *ignored: Any) -> None:
update_xml = Updates.fromstring(self.base, update_xml_text) update_xml = Updates.fromstring(self.base, update_xml_text)
self._append_tool_update(os_target_folder, update_xml, self.arch, self.tool_version_str) self._append_tool_update(os_target_folder, update_xml, self.arch, self.tool_version_str)

View File

@@ -18,13 +18,15 @@
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typing import List, Optional from typing import List, Optional, Any
DOCS_CONFIG = "https://aqtinstall.readthedocs.io/en/stable/configuration.html#configuration" DOCS_CONFIG = "https://aqtinstall.readthedocs.io/en/stable/configuration.html#configuration"
class AqtException(Exception): class AqtException(Exception):
def __init__(self, *args, suggested_action: Optional[List[str]] = None, should_show_help: bool = False, **kwargs) -> None: def __init__(
self, *args, suggested_action: Optional[List[str]] = None, should_show_help: bool = False, **kwargs: Any
) -> None:
self.suggested_action: List[str] = suggested_action or [] self.suggested_action: List[str] = suggested_action or []
self.should_show_help: bool = should_show_help or False self.should_show_help: bool = should_show_help or False
super(AqtException, self).__init__(*args, **kwargs) super(AqtException, self).__init__(*args, **kwargs)

View File

@@ -19,17 +19,17 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import binascii import binascii
import configparser
import hashlib import hashlib
import logging.config import logging.config
import os import os
import posixpath import posixpath
import secrets import secrets
import sys import sys
import threading from configparser import ConfigParser
from logging import Handler, getLogger from logging import Handler, getLogger
from logging.handlers import QueueListener from logging.handlers import QueueListener
from pathlib import Path from pathlib import Path
from threading import Lock
from typing import Any, Callable, Dict, Generator, List, Optional, TextIO, Tuple, Union from typing import Any, Callable, Dict, Generator, List, Optional, TextIO, Tuple, Union
from urllib.parse import urlparse from urllib.parse import urlparse
from xml.etree.ElementTree import Element from xml.etree.ElementTree import Element
@@ -309,7 +309,7 @@ def xml_to_modules(
return packages return packages
class MyConfigParser(configparser.ConfigParser): class MyConfigParser(ConfigParser):
def getlist(self, section: str, option: str, fallback: List[str] = []) -> List[str]: def getlist(self, section: str, option: str, fallback: List[str] = []) -> List[str]:
value = self.get(section, option, fallback=None) value = self.get(section, option, fallback=None)
if value is None: if value is None:
@@ -339,7 +339,7 @@ class SettingsClass:
"config": None, "config": None,
"configfile": None, "configfile": None,
"loggingconf": None, "loggingconf": None,
"_lock": threading.Lock(), "_lock": Lock(),
} }
def __new__(cls, *p, **k): def __new__(cls, *p, **k):
@@ -348,6 +348,8 @@ class SettingsClass:
return self return self
def __init__(self) -> None: def __init__(self) -> None:
self.config: Optional[ConfigParser]
self._lock: Lock
if self.config is None: if self.config is None:
with self._lock: with self._lock:
if self.config is None: if self.config is None:
@@ -356,6 +358,8 @@ class SettingsClass:
self.loggingconf = os.path.join(os.path.dirname(__file__), "logging.ini") self.loggingconf = os.path.join(os.path.dirname(__file__), "logging.ini")
def load_settings(self, file: Optional[Union[str, TextIO]] = None) -> None: def load_settings(self, file: Optional[Union[str, TextIO]] = None) -> None:
if self.config is None:
return
if file is not None: if file is not None:
if isinstance(file, str): if isinstance(file, str):
result = self.config.read(file) result = self.config.read(file)