fix(py39): replace PEP 604 union types with Optional for Python 3.9 compatibility
Some checks failed
Mark stable commit / test-unit (push) Has been cancelled
Mark stable commit / test-integration (push) Has been cancelled
Mark stable commit / test-env-virtual (push) Has been cancelled
Mark stable commit / test-env-nix (push) Has been cancelled
Mark stable commit / test-e2e (push) Has been cancelled
Mark stable commit / test-virgin-user (push) Has been cancelled
Mark stable commit / test-virgin-root (push) Has been cancelled
Mark stable commit / mark-stable (push) Has been cancelled

- Replaced all `X | None` type hints with `Optional[X]`
- Adjusted typing imports across modules
- Fixed import order and removed invalid future-import placements
- Ensured code runs correctly on Python 3.9

https://chatgpt.com/share/693c58e1-ce70-800f-9088-5864571e024a
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-12 19:02:54 +01:00
parent 31f7f47fe2
commit ac5ae95369
15 changed files with 21 additions and 19 deletions

View File

@@ -1,5 +1,4 @@
from __future__ import annotations
from typing import Optional
from pkgmgr.core.git import run_git, GitError, get_current_branch
from .utils import _resolve_base_branch

View File

@@ -1,5 +1,4 @@
from __future__ import annotations
from typing import Optional
from pkgmgr.core.git import run_git, GitError, get_current_branch
from .utils import _resolve_base_branch

View File

@@ -1,5 +1,4 @@
from __future__ import annotations
from typing import Optional
from pkgmgr.core.git import run_git, GitError
from .utils import _resolve_base_branch

View File

@@ -15,7 +15,7 @@ Responsibilities:
from __future__ import annotations
import os
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional
from pkgmgr.core.repository.identifier import get_repo_identifier
from pkgmgr.core.repository.dir import get_repo_dir
@@ -63,7 +63,7 @@ def _ensure_repo_dir(
no_verification: bool,
clone_mode: str,
identifier: str,
) -> str | None:
) -> Optional[str]:
"""
Compute and, if necessary, clone the repository directory.

View File

@@ -35,7 +35,7 @@ from __future__ import annotations
import glob
import os
from abc import ABC, abstractmethod
from typing import Iterable, TYPE_CHECKING
from typing import Iterable, TYPE_CHECKING, Optional
if TYPE_CHECKING:
from pkgmgr.actions.install.context import RepoContext
@@ -46,7 +46,7 @@ if TYPE_CHECKING:
# ---------------------------------------------------------------------------
def _read_text_if_exists(path: str) -> str | None:
def _read_text_if_exists(path: str) -> Optional[str]:
"""Read a file as UTF-8 text, returning None if it does not exist or fails."""
if not os.path.exists(path):
return None
@@ -75,7 +75,7 @@ def _scan_files_for_patterns(files: Iterable[str], patterns: Iterable[str]) -> b
return False
def _first_spec_file(repo_dir: str) -> str | None:
def _first_spec_file(repo_dir: str) -> Optional[str]:
"""Return the first *.spec file in repo_dir, if any."""
matches = glob.glob(os.path.join(repo_dir, "*.spec"))
if not matches:
@@ -360,7 +360,7 @@ def detect_capabilities(
def resolve_effective_capabilities(
ctx: "RepoContext",
layers: Iterable[str] | None = None,
layers: Optional[Iterable[str]] = None,
) -> dict[str, set[str]]:
"""
Resolve *effective* capabilities for each layer using a bottom-up strategy.

View File

@@ -6,7 +6,7 @@ Base interface for all installer components in the pkgmgr installation pipeline.
"""
from abc import ABC, abstractmethod
from typing import Set
from typing import Set, Optional
from pkgmgr.actions.install.context import RepoContext
from pkgmgr.actions.install.capabilities import CAPABILITY_MATCHERS
@@ -24,7 +24,7 @@ class BaseInstaller(ABC):
# Examples: "nix", "python", "makefile".
# This is used by capability matchers to decide which patterns to
# search for in the repository.
layer: str | None = None
layer: Optional[str] = None
def discover_capabilities(self, ctx: RepoContext) -> Set[str]:
"""

View File

@@ -17,7 +17,7 @@ apt/dpkg tooling are available.
import glob
import os
import shutil
from typing import List
from typing import List, Optional
from pkgmgr.actions.install.context import RepoContext
from pkgmgr.actions.install.installers.base import BaseInstaller
@@ -67,7 +67,7 @@ class DebianControlInstaller(BaseInstaller):
pattern = os.path.join(parent, "*.deb")
return sorted(glob.glob(pattern))
def _privileged_prefix(self) -> str | None:
def _privileged_prefix(self) -> Optional[str]:
"""
Determine how to run privileged commands:

View File

@@ -1,10 +1,10 @@
from __future__ import annotations
import os
from typing import List, Optional, Set
from pkgmgr.core.command.run import run_command
from pkgmgr.core.git import GitError, run_git
from typing import List, Optional, Set
from .types import MirrorMap, RepoMirrorContext, Repository

View File

@@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Optional
import os
import sys

View File

@@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Optional
import os
import sys

View File

@@ -7,7 +7,7 @@ import os
import sys
import shutil
from pathlib import Path
from typing import Any, Dict
from typing import Any, Dict, Optional
import yaml
@@ -36,7 +36,7 @@ def _load_user_config(user_config_path: str) -> Dict[str, Any]:
return {"repositories": []}
def _find_defaults_source_dir() -> str | None:
def _find_defaults_source_dir() -> Optional[str]:
"""
Find the directory inside the installed pkgmgr package OR the
project root that contains default config files.

View File

@@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Optional
import os
import sys

View File

@@ -1,3 +1,4 @@
from typing import Optional
import os
import shutil
from typing import Optional, List, Dict, Any

View File

@@ -1,3 +1,4 @@
from typing import Optional
# pkgmgr/run_command.py
import subprocess
import sys

View File

@@ -40,7 +40,7 @@ from __future__ import annotations
import os
from pathlib import Path
from typing import Any, Dict, List, Tuple
from typing import Any, Dict, List, Tuple, Optional
import yaml
@@ -83,7 +83,7 @@ def _repo_key(repo: Repo) -> Tuple[str, str, str]:
def _merge_repo_lists(
base_list: List[Repo],
new_list: List[Repo],
category_name: str | None = None,
category_name: Optional[str] = None,
) -> List[Repo]:
"""
Merge two repository lists, matching by (provider, account, repository).
@@ -143,7 +143,7 @@ def _load_yaml_file(path: Path) -> Dict[str, Any]:
def _load_layer_dir(
config_dir: Path,
skip_filename: str | None = None,
skip_filename: Optional[str] = None,
) -> Dict[str, Any]:
"""
Load all *.yml/*.yaml from a directory as layered defaults.