Skip to content

Commit

Permalink
Improve type definitions and styles (#869)
Browse files Browse the repository at this point in the history
- exclude .reviewdog.yml from source distribution
- Add more type hints
- protective check for self.config in SettingsClass

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr authored Dec 24, 2024
1 parent f914397 commit eb164e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ recursive-include tools *.py
prune .github
exclude .gitignore
exclude .readthedocs.yml
exclude .reviewdog.yml
exclude azure-pipelines.yml
14 changes: 8 additions & 6 deletions aqt/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from dataclasses import dataclass, field
from itertools import islice, zip_longest
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 defusedxml import ElementTree
Expand Down Expand Up @@ -450,11 +450,11 @@ def _get_archives_base(self, name, 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."""
if not Settings.ignore_hash:
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:
if silent:
return None
Expand All @@ -469,7 +469,9 @@ def _download_update_xml(self, update_xml_path, silent=False):
xml_hash = None
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:
target_packages = ModuleToPackage({})
update_xml = Updates.fromstring(self.base, update_xml_text)
Expand Down Expand Up @@ -509,7 +511,7 @@ def _parse_update_xml(self, os_target_folder, update_xml_text, target_packages:
)
)

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:
target_packages = ModuleToPackage({})
for update_xml in update_xmls:
Expand Down Expand Up @@ -700,7 +702,7 @@ def handle_missing_updates_xml(self, e: ArchiveDownloadError):
def _get_archives(self):
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)
self._append_tool_update(os_target_folder, update_xml, self.arch, self.tool_version_str)

Expand Down
6 changes: 4 additions & 2 deletions aqt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
# 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
# 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"


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.should_show_help: bool = should_show_help or False
super(AqtException, self).__init__(*args, **kwargs)
Expand Down
12 changes: 8 additions & 4 deletions aqt/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
# 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.
import binascii
import configparser
import hashlib
import logging.config
import os
import posixpath
import secrets
import sys
import threading
from configparser import ConfigParser
from logging import Handler, getLogger
from logging.handlers import QueueListener
from pathlib import Path
from threading import Lock
from typing import Any, Callable, Dict, Generator, List, Optional, TextIO, Tuple, Union
from urllib.parse import urlparse
from xml.etree.ElementTree import Element
Expand Down Expand Up @@ -309,7 +309,7 @@ def xml_to_modules(
return packages


class MyConfigParser(configparser.ConfigParser):
class MyConfigParser(ConfigParser):
def getlist(self, section: str, option: str, fallback: List[str] = []) -> List[str]:
value = self.get(section, option, fallback=None)
if value is None:
Expand Down Expand Up @@ -339,7 +339,7 @@ class SettingsClass:
"config": None,
"configfile": None,
"loggingconf": None,
"_lock": threading.Lock(),
"_lock": Lock(),
}

def __new__(cls, *p, **k):
Expand All @@ -348,6 +348,8 @@ def __new__(cls, *p, **k):
return self

def __init__(self) -> None:
self.config: Optional[ConfigParser]
self._lock: Lock
if self.config is None:
with self._lock:
if self.config is None:
Expand All @@ -356,6 +358,8 @@ def __init__(self) -> None:
self.loggingconf = os.path.join(os.path.dirname(__file__), "logging.ini")

def load_settings(self, file: Optional[Union[str, TextIO]] = None) -> None:
if self.config is None:
return
if file is not None:
if isinstance(file, str):
result = self.config.read(file)
Expand Down

0 comments on commit eb164e5

Please sign in to comment.