a258eb4547e610899162526f754ce1bf88027112

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
Carlo Sala <carlosalag@protonmail.com>
Date

Message

fix(dependencies): improve typing

Diff

 1diff --git a/.github/workflows/dependencies/updater.py b/.github/workflows/dependencies/updater.py
 2index 765cb26fe1194ff9abf167fbae3da21c6cfab1aa..9754cdf839419f6cbf6781808c293096f5c5faf5 100644
 3--- a/.github/workflows/dependencies/updater.py
 4+++ b/.github/workflows/dependencies/updater.py
 5@@ -4,7 +4,7 @@ import subprocess
 6 import sys
 7 import timeit
 8 from copy import deepcopy
 9-from typing import Optional, TypedDict
10+from typing import Literal, NotRequired, TypedDict
11 
12 import requests
13 import yaml
14@@ -49,20 +49,24 @@ class DependencyDict(TypedDict):
15     repo: str
16     branch: str
17     version: str
18-    precopy: Optional[str]
19-    postcopy: Optional[str]
20+    precopy: NotRequired[str]
21+    postcopy: NotRequired[str]
22 
23 
24 class DependencyYAML(TypedDict):
25     dependencies: dict[str, DependencyDict]
26 
27 
28-class UpdateStatus(TypedDict):
29-    has_updates: bool
30-    version: Optional[str]
31-    compare_url: Optional[str]
32-    head_ref: Optional[str]
33-    head_url: Optional[str]
34+class UpdateStatusFalse(TypedDict):
35+    has_updates: Literal[False]
36+
37+
38+class UpdateStatusTrue(TypedDict):
39+    has_updates: Literal[True]
40+    version: str
41+    compare_url: str
42+    head_ref: str
43+    head_url: str
44 
45 
46 class CommandRunner:
47@@ -105,7 +109,9 @@ class DependencyStore:
48         with CodeTimer(f"store deepcopy: {path}"):
49             store_copy = deepcopy(DependencyStore.store)
50 
51-        dependency = store_copy["dependencies"].get(path, {})
52+        dependency = store_copy["dependencies"].get(path)
53+        if dependency is None:
54+            raise ValueError(f"Dependency {path} {version} not found")
55         dependency["version"] = version
56         store_copy["dependencies"][path] = dependency
57 
58@@ -171,7 +177,7 @@ class Dependency:
59                 else:
60                     status = GitHub.check_updates(repo, remote_branch, version)
61 
62-            if status["has_updates"]:
63+            if status["has_updates"] is True:
64                 short_sha = status["head_ref"][:8]
65                 new_version = status["version"] if is_tag else short_sha
66 
67@@ -212,10 +218,10 @@ Check out the [list of changes]({status['compare_url']}).
68                         case CommandRunner.Exception:
69                             # Print error message
70                             print(
71-                                f"Error running {e.stage} command: {e.returncode}",
72+                                f"Error running {e.stage} command: {e.returncode}",  # pyright: ignore[reportAttributeAccessIssue]
73                                 file=sys.stderr,
74                             )
75-                            print(e.stderr, file=sys.stderr)
76+                            print(e.stderr, file=sys.stderr)  # pyright: ignore[reportAttributeAccessIssue]
77                         case shutil.Error:
78                             print(f"Error copying files: {e}", file=sys.stderr)
79 
80@@ -378,7 +384,7 @@ class Git:
81 
82 class GitHub:
83     @staticmethod
84-    def check_newer_tag(repo, current_tag) -> UpdateStatus:
85+    def check_newer_tag(repo, current_tag) -> UpdateStatusFalse | UpdateStatusTrue:
86         # GET /repos/:owner/:repo/git/refs/tags
87         url = f"https://api.github.com/repos/{repo}/git/refs/tags"
88 
89@@ -417,7 +423,7 @@ class GitHub:
90             )
91 
92     @staticmethod
93-    def check_updates(repo, branch, version) -> UpdateStatus:
94+    def check_updates(repo, branch, version) -> UpdateStatusFalse | UpdateStatusTrue:
95         # TODO: add support for semver updating (based on tags)
96         # Check if upstream github repo has a new version
97         # GitHub API URL for comparing two commits