from mkosi.log import ARG_DEBUG, ARG_DEBUG_SHELL, Style, die
from mkosi.pager import page
from mkosi.run import run
-from mkosi.types import PathString
+from mkosi.types import PathString, SupportsRead
from mkosi.util import (
InvokingUser,
StrEnum,
return json.dumps(dataclasses.asdict(self), cls=MkosiJsonEncoder, indent=indent, sort_keys=sort_keys)
@classmethod
- def from_json(cls, s: str) -> "MkosiArgs":
- j = json.loads(s)
+ def from_json(cls, s: Union[str, dict[str, Any], SupportsRead[str], SupportsRead[bytes]]) -> "MkosiArgs":
+ if isinstance(s, str):
+ j = json.loads(s)
+ elif isinstance(s, dict):
+ j = s
+ elif hasattr(s, "read"):
+ j = json.load(s)
+ else:
+ raise ValueError(f"{cls.__name__} can only be constructed from JSON from strings, dictionaries and files.")
+
transformer = json_type_transformer(cls)
tj = {k: transformer(k, v) for k, v in j.items()}
return cls(**tj)
return json.dumps(dataclasses.asdict(self), cls=MkosiJsonEncoder, indent=indent, sort_keys=sort_keys)
@classmethod
- def from_json(cls, s: str) -> "MkosiConfig":
- j = json.loads(s)
+ def from_json(cls, s: Union[str, dict[str, Any], SupportsRead[str], SupportsRead[bytes]]) -> "MkosiConfig":
+ if isinstance(s, str):
+ j = json.loads(s)
+ elif isinstance(s, dict):
+ j = s
+ elif hasattr(s, "read"):
+ j = json.load(s)
+ else:
+ raise ValueError(f"{cls.__name__} can only be constructed from JSON from strings, dictionaries and files.")
+
transformer = json_type_transformer(cls)
tj = {k: transformer(k, v) for k, v in j.items()}
return cls(**tj)
import subprocess
from pathlib import Path
-from typing import IO, TYPE_CHECKING, Any, Union
+from typing import IO, TYPE_CHECKING, Any, Protocol, TypeVar, Union
# These types are only generic during type checking and not at runtime, leading
# to a TypeError during compilation.
# Borrowed from https://github.com/python/typeshed/blob/3d14016085aed8bcf0cf67e9e5a70790ce1ad8ea/stdlib/3/subprocess.pyi#L24
_FILE = Union[None, int, IO[Any]]
PathString = Union[Path, str]
+
+# Borrowed from
+# https://github.com/python/typeshed/blob/ec52bf1adde1d3183d0595d2ba982589df48dff1/stdlib/_typeshed/__init__.pyi#L19
+# and
+# https://github.com/python/typeshed/blob/ec52bf1adde1d3183d0595d2ba982589df48dff1/stdlib/_typeshed/__init__.pyi#L224
+_T_co = TypeVar("_T_co", covariant=True)
+
+class SupportsRead(Protocol[_T_co]):
+ def read(self, __length: int = ...) -> _T_co: ...