pmb: Use inspect.get_annotations()

On Python 3.10 and newer, this is the recommended way of accessing
annotations[1]. It also works with Mypyc, unlike directly accessing
__annotations__.

 [1]: https://docs.python.org/3/howto/annotations.html

Part-of: https://gitlab.postmarketos.org/postmarketOS/pmbootstrap/-/merge_requests/2634
This commit is contained in:
Newbyte 2025-07-01 12:47:18 +02:00 committed by Oliver Smith
parent 53f050ef14
commit 0ade6cab4d
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
2 changed files with 6 additions and 4 deletions

View file

@ -3,6 +3,7 @@
from copy import deepcopy
import enum
import inspect
import multiprocessing
from typing import Any, ClassVar, TypedDict
from pathlib import Path
@ -90,14 +91,14 @@ class Config:
def __init__(self) -> None:
# Make sure we aren't modifying the class defaults
for key in Config.__annotations__.keys():
for key in inspect.get_annotations(Config).keys():
setattr(self, key, deepcopy(Config.get_default(key)))
@staticmethod
def keys() -> list[str]:
keys = list(Config.__annotations__.keys())
keys = list(inspect.get_annotations(Config).keys())
keys.remove("mirrors")
keys += [f"mirrors.{k}" for k in Mirrors.__annotations__.keys()]
keys += [f"mirrors.{k}" for k in inspect.get_annotations(Mirrors).keys()]
return sorted(keys)
@staticmethod

View file

@ -5,6 +5,7 @@ from pathlib import Path
from pmb.core.context import get_context
from pmb.core.arch import Arch
from pmb.helpers import logging
import inspect
import os
import pmb.config
import pmb.helpers.other
@ -53,7 +54,7 @@ def _parse_kernel_suffix(info: dict[str, str], device: str, kernel: str | None)
ret = copy.copy(info)
suffix_kernel = kernel.replace("-", "_")
for key in Deviceinfo.__annotations__.keys():
for key in inspect.get_annotations(Deviceinfo).keys():
key_kernel = f"{key}_{suffix_kernel}"
if key_kernel not in ret:
continue