Skip to content

Commit

Permalink
Add complete type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
bbhtt committed Aug 7, 2024
1 parent 2105efa commit 291c2fe
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 38 deletions.
38 changes: 21 additions & 17 deletions flatpak_builder_lint/appstream.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import os
import subprocess
from typing import Optional
from typing import List, Optional, TypedDict, cast

from lxml import etree

# for mypy
Element = etree._Element
ElementTree = etree._ElementTree

class SubprocessResult(TypedDict):
stdout: str
stderr: str
returncode: int

def validate(path: str, *args: str) -> dict:

def validate(path: str, *args: str) -> SubprocessResult:
if not os.path.isfile(path):
raise FileNotFoundError("AppStream file not found")

Expand Down Expand Up @@ -45,7 +47,7 @@ def validate(path: str, *args: str) -> dict:
capture_output=True,
)

ret = {
ret: SubprocessResult = {
"stdout": cmd.stdout.decode("utf-8"),
"stderr": cmd.stderr.decode("utf-8"),
"returncode": cmd.returncode,
Expand All @@ -54,26 +56,24 @@ def validate(path: str, *args: str) -> dict:
return ret


def parse_xml(path: str) -> ElementTree:
def parse_xml(path: str) -> etree._ElementTree:
return etree.parse(path)


def components(path: str) -> list:
components = parse_xml(path).xpath("/components/component")
return list(components)
def components(path: str) -> List[etree._Element]:
return cast(List[etree._Element], parse_xml(path).xpath("/components/component"))


def metainfo_components(path: str) -> list:
components = parse_xml(path).xpath("/component")
return list(components)
def metainfo_components(path: str) -> List[etree._Element]:
return cast(List[etree._Element], parse_xml(path).xpath("/component"))


def appstream_id(path: str) -> Optional[str]:
aps_cid = components(path)[0].xpath("id/text()")[0]
return str(aps_cid)


def get_launchable(path: str) -> list:
def get_launchable(path: str) -> List[str]:
launchable = components(path)[0].xpath("launchable[@type='desktop-id']/text()")
return list(launchable)

Expand Down Expand Up @@ -133,9 +133,13 @@ def has_icon_key(path: str) -> bool:


def icon_no_type(path: str) -> bool:
for icon in parse_xml(path).findall("component/icon"):
if icon.attrib.get("type") is None:
return True
icon_types = set(
[icon.attrib.get("type") for icon in components(path)[0].xpath("icon")]
)

if None in icon_types:
return True

return False


Expand Down
12 changes: 6 additions & 6 deletions flatpak_builder_lint/builddir.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os
from collections import defaultdict
from typing import Optional
from typing import List, Optional

from gi.repository import GLib

Expand Down Expand Up @@ -83,12 +83,12 @@ def infer_appid(path: str) -> Optional[str]:
return None


def get_flathub_json(path: str) -> Optional[dict]:
def get_flathub_json(path: str) -> dict[str, str | bool | List[str]]:
flathub_json_path = f"{path}/files/flathub.json"
if not os.path.exists(flathub_json_path):
return None
flathub_json: dict = {}

with open(flathub_json_path, "r") as f:
flathub_json: dict = json.load(f)
if os.path.exists(flathub_json_path):
with open(flathub_json_path, "r") as f:
flathub_json = json.load(f)

return flathub_json
4 changes: 2 additions & 2 deletions flatpak_builder_lint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _filter(info: set, excepts: set) -> list:
return list(final)


def get_local_exceptions(appid: str) -> set:
def get_local_exceptions(appid: str) -> set[str]:
with importlib.resources.open_text(staticfiles, "exceptions.json") as f:
exceptions = json.load(f)
ret = exceptions.get(appid)
Expand All @@ -53,7 +53,7 @@ def get_local_exceptions(appid: str) -> set:

def run_checks(
kind: str, path: str, enable_exceptions: bool = False, appid: Optional[str] = None
) -> dict:
) -> Dict[str, Union[str, List[Optional[str]]]]:
match kind:
case "manifest":
check_method_name = "check_manifest"
Expand Down
4 changes: 2 additions & 2 deletions flatpak_builder_lint/domainutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def fetch_summary_bytes(url: str) -> bytes:


@cache
def get_appids_from_summary(url: str) -> set:
def get_appids_from_summary(url: str) -> set[str]:

appids = set()
summary = GLib.Bytes.new(fetch_summary_bytes(url))
Expand All @@ -82,7 +82,7 @@ def get_appids_from_summary(url: str) -> set:


@cache
def get_all_apps_on_flathub() -> set:
def get_all_apps_on_flathub() -> set[str]:
return get_appids_from_summary(
f"{FLATHUB_STABLE_REPO_URL}/summary"
) | get_appids_from_summary(f"{FLATHUB_BETA_REPO_URL}/summary")
Expand Down
6 changes: 2 additions & 4 deletions flatpak_builder_lint/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def show_manifest(filename: str) -> dict:
raise Exception(ret.stderr.decode("utf-8"))

manifest = ret.stdout.decode("utf-8")
manifest_json = json.loads(manifest)
manifest_json: dict = json.loads(manifest)
manifest_json["x-manifest-filename"] = filename

manifest_basedir = os.path.dirname(filename)
Expand All @@ -31,9 +31,7 @@ def show_manifest(filename: str) -> dict:
flathub_json = json.load(f)
manifest_json["x-flathub"] = flathub_json

# mypy does not support circular types
# https://github.com/python/typing/issues/182
return manifest_json # type: ignore
return manifest_json


def infer_appid(path: str) -> Optional[str]:
Expand Down
24 changes: 18 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ black = "^24.3.0"
Flake8-pyproject = "^1.1.0"
types-requests = "^2.28.11"
types-jsonschema = "^4.16.0"
types-lxml = "^2023.10.21"
types-lxml = "^2024.4.14"
PyGObject-stubs = "^2.10.0"

[tool.poetry.scripts]
Expand Down

0 comments on commit 291c2fe

Please sign in to comment.