diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2022-10-06 05:12:30 -0400 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2023-01-16 15:30:12 -0500 |
commit | 2d0c9ce5f270306610c502859ebd46fd92162fb1 (patch) | |
tree | 0c21acfcc5c2dbbf6c59c5aeaab17c45c4e8d6e0 /mesonbuild/utils | |
parent | 9b999ddc874cfba34c3d1c67f75aba037389e65f (diff) | |
download | meson-2d0c9ce5f270306610c502859ebd46fd92162fb1.zip meson-2d0c9ce5f270306610c502859ebd46fd92162fb1.tar.gz meson-2d0c9ce5f270306610c502859ebd46fd92162fb1.tar.bz2 |
properly type utils/core.py and add it to mypy
EnvironmentVariables was always broken, it used MutableMapping because
everyone <3 abstract interfaces, especially when they are broken and
don't actually do what you want.
This needs a dict interface, exposing `.copy()`. We either use a dict or
os._Environ, and the latter also supports that.
Also fix a broken import, and the fallout from forgetting to update the
signature of self.envvars in commit b926374205bd761085031755c87152d08bc10e9d.
Diffstat (limited to 'mesonbuild/utils')
-rw-r--r-- | mesonbuild/utils/core.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/mesonbuild/utils/core.py b/mesonbuild/utils/core.py index 5450cdc..310061a 100644 --- a/mesonbuild/utils/core.py +++ b/mesonbuild/utils/core.py @@ -27,9 +27,12 @@ import abc import typing as T if T.TYPE_CHECKING: + from hashlib import _Hash from typing_extensions import Literal from ..mparser import BaseNode - from . import programs + from .. import programs + + EnvironOrDict = T.Union[T.Dict[str, str], os._Environ[str]] __all__ = [ @@ -78,7 +81,7 @@ EnvInitValueType = T.Dict[str, T.Union[str, T.List[str]]] class EnvironmentVariables(HoldableObject): def __init__(self, values: T.Optional[EnvInitValueType] = None, init_method: Literal['set', 'prepend', 'append'] = 'set', separator: str = os.pathsep) -> None: - self.envvars: T.List[T.Tuple[T.Callable[[T.Dict[str, str], str, T.List[str], str], str], str, T.List[str], str]] = [] + self.envvars: T.List[T.Tuple[T.Callable[[T.Dict[str, str], str, T.List[str], str, T.Optional[str]], str], str, T.List[str], str]] = [] # The set of all env vars we have operations for. Only used for self.has_name() self.varnames: T.Set[str] = set() @@ -92,7 +95,7 @@ class EnvironmentVariables(HoldableObject): repr_str = "<{0}: {1}>" return repr_str.format(self.__class__.__name__, self.envvars) - def hash(self, hasher: T.Any): + def hash(self, hasher: _Hash) -> None: myenv = self.get_env({}) for key in sorted(myenv.keys()): hasher.update(bytes(key, encoding='utf-8')) @@ -132,7 +135,7 @@ class EnvironmentVariables(HoldableObject): curr = env.get(name, default_value) return separator.join(values if curr is None else values + [curr]) - def get_env(self, full_env: T.MutableMapping[str, str], dump: bool = False) -> T.Dict[str, str]: + def get_env(self, full_env: EnvironOrDict, dump: bool = False) -> T.Dict[str, str]: env = full_env.copy() for method, name, values, separator in self.envvars: default_value = f'${name}' if dump else None |