commit 416ef7cf872b70c07dd8dd7190ed92d038491bb1 from: Jelmer Vernooij via: GitHub date: Thu Jun 30 17:17:07 2022 UTC Merge pull request #985 from dimbleby/boolean-config Special case boolean config values commit - 9d9b5072223e4276793a7c6005786cb87f96cb08 commit + 416ef7cf872b70c07dd8dd7190ed92d038491bb1 blob - f979d3cdf036e519ffd79209a431c3736611ee0c blob + 85d073258d11da96c0d7330d6a337793aa42aa77 --- dulwich/config.py +++ dulwich/config.py @@ -29,13 +29,12 @@ TODO: import os import sys import warnings - from typing import ( BinaryIO, Iterable, Iterator, - List, KeysView, + List, MutableMapping, Optional, Tuple, @@ -142,8 +141,8 @@ class CaseInsensitiveOrderedMultiDict(MutableMapping): BytesLike = Union[bytes, str] Key = Tuple[bytes, ...] KeyLike = Union[bytes, str, Tuple[BytesLike, ...]] -Value = Union[bytes, bool] -ValueLike = Union[bytes, str, bool] +Value = bytes +ValueLike = Union[bytes, str] class Config(object): @@ -202,7 +201,12 @@ class Config(object): return False raise ValueError("not a valid boolean string: %r" % value) - def set(self, section: KeyLike, name: BytesLike, value: ValueLike) -> None: + def set( + self, + section: KeyLike, + name: BytesLike, + value: Union[ValueLike, bool] + ) -> None: """Set a configuration value. Args: @@ -369,11 +373,14 @@ class ConfigDict(Config, MutableMapping[Key, MutableMa self, section: KeyLike, name: BytesLike, - value: ValueLike, + value: Union[ValueLike, bool], ) -> None: section, name = self._check_section_and_name(section, name) - if not isinstance(value, (bytes, bool)): + if isinstance(value, bool): + value = b"true" if value else b"false" + + if not isinstance(value, bytes): value = value.encode(self.encoding) self._values.setdefault(section)[name] = value @@ -732,7 +739,12 @@ class StackedConfig(Config): except KeyError: pass - def set(self, section: KeyLike, name: BytesLike, value: ValueLike) -> None: + def set( + self, + section: KeyLike, + name: BytesLike, + value: Union[ValueLike, bool] + ) -> None: if self.writable is None: raise NotImplementedError(self.set) return self.writable.set(section, name, value) @@ -759,9 +771,5 @@ def parse_submodules(config: ConfigFile) -> Iterator[T section_kind, section_name = section if section_kind == b"submodule": sm_path = config.get(section, b"path") - assert isinstance(sm_path, bytes) - assert sm_path is not None sm_url = config.get(section, b"url") - assert sm_url is not None - assert isinstance(sm_url, bytes) yield (sm_path, sm_url, section_name) blob - c7ff6544e509efb2a6c4b4fb4382b581726c6243 blob + d34dac7e8a6f3a6113e2b30e97f9f4f27dbe1a1a --- dulwich/porcelain.py +++ dulwich/porcelain.py @@ -1001,10 +1001,7 @@ def get_remote_repo( if config.has_section(section): remote_name = encoded_location.decode() - url = config.get(section, "url") - assert url is not None - assert isinstance(url, bytes) - encoded_location = url + encoded_location = config.get(section, "url") else: remote_name = None blob - 396069b9de456f1bde341145a311b23b46130da8 blob + 80c2ad575723a1452ebcc6bd4b34a2fe267fc7e1 --- dulwich/repo.py +++ dulwich/repo.py @@ -195,16 +195,12 @@ def get_user_identity(config: "StackedConfig", kind: O email = email_uc.encode("utf-8") if user is None: try: - config_value = config.get(("user",), "name") - assert isinstance(config_value, bytes) - user = config_value + user = config.get(("user",), "name") except KeyError: user = None if email is None: try: - config_value = config.get(("user",), "email") - assert isinstance(config_value, bytes) - email = config_value + email = config.get(("user",), "email") except KeyError: email = None default_user, default_email = _get_default_identity()