| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ ignore = E265,E266,E731,E704, | |||
| 20 | 20 | W293, W504, | |
| 21 | 21 | ANN0 ANN1 ANN2, | |
| 22 | 22 | TC002, | |
| 23 | - # TC0, TC1, TC2 | ||
| 23 | + TC0, TC1, TC2 | ||
| 24 | 24 | # B, | |
| 25 | 25 | A, | |
| 26 | 26 | D, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,19 +97,3 @@ def win_encode(s: Optional[AnyStr]) -> Optional[bytes]: | |||
| 97 | 97 | elif s is not None: | |
| 98 | 98 | raise TypeError('Expected bytes or text, but got %r' % (s,)) | |
| 99 | 99 | return None | |
| 100 | - | ||
| 101 | - | ||
| 102 | - # type: ignore ## mypy cannot understand dynamic class creation | ||
| 103 | - def with_metaclass(meta: Type[Any], *bases: Any) -> TBD: | ||
| 104 | - """copied from https://github.com/Byron/bcore/blob/master/src/python/butility/future.py#L15""" | ||
| 105 | - | ||
| 106 | - class metaclass(meta): # type: ignore | ||
| 107 | - __call__ = type.__call__ | ||
| 108 | - __init__ = type.__init__ # type: ignore | ||
| 109 | - | ||
| 110 | - def __new__(cls, name: str, nbases: Optional[Tuple[int, ...]], d: Dict[str, Any]) -> TBD: | ||
| 111 | - if nbases is None: | ||
| 112 | - return type.__new__(cls, name, (), d) | ||
| 113 | - return meta(name, bases, d) | ||
| 114 | - | ||
| 115 | - return metaclass(meta.__name__ + 'Helper', None, {}) # type: ignore | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,12 +14,10 @@ | |||
| 14 | 14 | import os | |
| 15 | 15 | import re | |
| 16 | 16 | import fnmatch | |
| 17 | - from collections import OrderedDict | ||
| 18 | 17 | ||
| 19 | 18 | from git.compat import ( | |
| 20 | 19 | defenc, | |
| 21 | 20 | force_text, | |
| 22 | - with_metaclass, | ||
| 23 | 21 | is_win, | |
| 24 | 22 | ) | |
| 25 | 23 | ||
@@ -31,15 +29,16 @@ | |||
| 31 | 29 | ||
| 32 | 30 | # typing------------------------------------------------------- | |
| 33 | 31 | ||
| 34 | - from typing import (Any, Callable, IO, List, Dict, Sequence, | ||
| 35 | - TYPE_CHECKING, Tuple, Union, cast, overload) | ||
| 32 | + from typing import (Any, Callable, Generic, IO, List, Dict, Sequence, | ||
| 33 | + TYPE_CHECKING, Tuple, TypeVar, Union, cast, overload) | ||
| 36 | 34 | ||
| 37 | - from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, TBD, assert_never | ||
| 35 | + from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, TBD, assert_never, _T | ||
| 38 | 36 | ||
| 39 | 37 | if TYPE_CHECKING: | |
| 40 | 38 | from git.repo.base import Repo | |
| 41 | 39 | from io import BytesIO | |
| 42 | 40 | ||
| 41 | + T_ConfigParser = TypeVar('T_ConfigParser', bound='GitConfigParser') | ||
| 43 | 42 | # ------------------------------------------------------------- | |
| 44 | 43 | ||
| 45 | 44 | __all__ = ('GitConfigParser', 'SectionConstraint') | |
@@ -61,7 +60,6 @@ | |||
| 61 | 60 | ||
| 62 | 61 | ||
| 63 | 62 | class MetaParserBuilder(abc.ABCMeta): | |
| 64 | - | ||
| 65 | 63 | """Utlity class wrapping base-class methods into decorators that assure read-only properties""" | |
| 66 | 64 | def __new__(cls, name: str, bases: TBD, clsdict: Dict[str, Any]) -> TBD: | |
| 67 | 65 | """ | |
@@ -115,7 +113,7 @@ def flush_changes(self, *args: Any, **kwargs: Any) -> Any: | |||
| 115 | 113 | return flush_changes | |
| 116 | 114 | ||
| 117 | 115 | ||
| 118 | - class SectionConstraint(object): | ||
| 116 | + class SectionConstraint(Generic[T_ConfigParser]): | ||
| 119 | 117 | ||
| 120 | 118 | """Constrains a ConfigParser to only option commands which are constrained to | |
| 121 | 119 | always use the section we have been initialized with. | |
@@ -128,7 +126,7 @@ class SectionConstraint(object): | |||
| 128 | 126 | _valid_attrs_ = ("get_value", "set_value", "get", "set", "getint", "getfloat", "getboolean", "has_option", | |
| 129 | 127 | "remove_section", "remove_option", "options") | |
| 130 | 128 | ||
| 131 | - def __init__(self, config: 'GitConfigParser', section: str) -> None: | ||
| 129 | + def __init__(self, config: T_ConfigParser, section: str) -> None: | ||
| 132 | 130 | self._config = config | |
| 133 | 131 | self._section_name = section | |
| 134 | 132 | ||
@@ -149,26 +147,26 @@ def _call_config(self, method: str, *args: Any, **kwargs: Any) -> Any: | |||
| 149 | 147 | return getattr(self._config, method)(self._section_name, *args, **kwargs) | |
| 150 | 148 | ||
| 151 | 149 | @property | |
| 152 | - def config(self) -> 'GitConfigParser': | ||
| 150 | + def config(self) -> T_ConfigParser: | ||
| 153 | 151 | """return: Configparser instance we constrain""" | |
| 154 | 152 | return self._config | |
| 155 | 153 | ||
| 156 | 154 | def release(self) -> None: | |
| 157 | 155 | """Equivalent to GitConfigParser.release(), which is called on our underlying parser instance""" | |
| 158 | 156 | return self._config.release() | |
| 159 | 157 | ||
| 160 | - def __enter__(self) -> 'SectionConstraint': | ||
| 158 | + def __enter__(self) -> 'SectionConstraint[T_ConfigParser]': | ||
| 161 | 159 | self._config.__enter__() | |
| 162 | 160 | return self | |
| 163 | 161 | ||
| 164 | 162 | def __exit__(self, exception_type: str, exception_value: str, traceback: str) -> None: | |
| 165 | 163 | self._config.__exit__(exception_type, exception_value, traceback) | |
| 166 | 164 | ||
| 167 | 165 | ||
| 168 | - class _OMD(OrderedDict): | ||
| 166 | + class _OMD(Dict[str, List[_T]]): | ||
| 169 | 167 | """Ordered multi-dict.""" | |
| 170 | 168 | ||
| 171 | - def __setitem__(self, key: str, value: Any) -> None: | ||
| 169 | + def __setitem__(self, key: str, value: _T) -> None: # type: ignore[override] | ||
| 172 | 170 | super(_OMD, self).__setitem__(key, [value]) | |
| 173 | 171 | ||
| 174 | 172 | def add(self, key: str, value: Any) -> None: | |
@@ -177,7 +175,7 @@ def add(self, key: str, value: Any) -> None: | |||
| 177 | 175 | return None | |
| 178 | 176 | super(_OMD, self).__getitem__(key).append(value) | |
| 179 | 177 | ||
| 180 | - def setall(self, key: str, values: Any) -> None: | ||
| 178 | + def setall(self, key: str, values: List[_T]) -> None: | ||
| 181 | 179 | super(_OMD, self).__setitem__(key, values) | |
| 182 | 180 | ||
| 183 | 181 | def __getitem__(self, key: str) -> Any: | |
@@ -194,25 +192,17 @@ def setlast(self, key: str, value: Any) -> None: | |||
| 194 | 192 | prior = super(_OMD, self).__getitem__(key) | |
| 195 | 193 | prior[-1] = value | |
| 196 | 194 | ||
| 197 | - @overload | ||
| 198 | - def get(self, key: str, default: None = ...) -> None: | ||
| 199 | - ... | ||
| 200 | - | ||
| 201 | - @overload | ||
| 202 | - def get(self, key: str, default: Any = ...) -> Any: | ||
| 203 | - ... | ||
| 204 | - | ||
| 205 | - def get(self, key: str, default: Union[Any, None] = None) -> Union[Any, None]: | ||
| 206 | - return super(_OMD, self).get(key, [default])[-1] | ||
| 195 | + def get(self, key: str, default: Union[_T, None] = None) -> Union[_T, None]: # type: ignore | ||
| 196 | + return super(_OMD, self).get(key, [default])[-1] # type: ignore | ||
| 207 | 197 | ||
| 208 | - def getall(self, key: str) -> Any: | ||
| 198 | + def getall(self, key: str) -> List[_T]: | ||
| 209 | 199 | return super(_OMD, self).__getitem__(key) | |
| 210 | 200 | ||
| 211 | - def items(self) -> List[Tuple[str, Any]]: # type: ignore[override] | ||
| 201 | + def items(self) -> List[Tuple[str, _T]]: # type: ignore[override] | ||
| 212 | 202 | """List of (key, last value for key).""" | |
| 213 | 203 | return [(k, self[k]) for k in self] | |
| 214 | 204 | ||
| 215 | - def items_all(self) -> List[Tuple[str, List[Any]]]: | ||
| 205 | + def items_all(self) -> List[Tuple[str, List[_T]]]: | ||
| 216 | 206 | """List of (key, list of values for key).""" | |
| 217 | 207 | return [(k, self.getall(k)) for k in self] | |
| 218 | 208 | ||
@@ -238,7 +228,7 @@ def get_config_path(config_level: Lit_config_levels) -> str: | |||
| 238 | 228 | assert_never(config_level, ValueError(f"Invalid configuration level: {config_level!r}")) | |
| 239 | 229 | ||
| 240 | 230 | ||
| 241 | - class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser)): # type: ignore ## mypy does not understand dynamic class creation # noqa: E501 | ||
| 231 | + class GitConfigParser(cp.RawConfigParser, metaclass=MetaParserBuilder): | ||
| 242 | 232 | ||
| 243 | 233 | """Implements specifics required to read git style configuration files. | |
| 244 | 234 | ||
@@ -298,7 +288,10 @@ def __init__(self, file_or_files: Union[None, PathLike, 'BytesIO', Sequence[Unio | |||
| 298 | 288 | :param repo: Reference to repository to use if [includeIf] sections are found in configuration files. | |
| 299 | 289 | ||
| 300 | 290 | """ | |
| 301 | - cp.RawConfigParser.__init__(self, dict_type=_OMD) | ||
| 291 | + cp.RawConfigParser.__init__(self, dict_type=_OMD) # type: ignore[arg-type] | ||
| 292 | + self._dict: Callable[..., _OMD] # type: ignore[assignment] # mypy/typeshed bug | ||
| 293 | + self._defaults: _OMD # type: ignore[assignment] # mypy/typeshed bug | ||
| 294 | + self._sections: _OMD # type: ignore[assignment] # mypy/typeshed bug | ||
| 302 | 295 | ||
| 303 | 296 | # Used in python 3, needs to stay in sync with sections for underlying implementation to work | |
| 304 | 297 | if not hasattr(self, '_proxies'): | |
@@ -424,7 +417,7 @@ def string_decode(v: str) -> str: | |||
| 424 | 417 | # is it a section header? | |
| 425 | 418 | mo = self.SECTCRE.match(line.strip()) | |
| 426 | 419 | if not is_multi_line and mo: | |
| 427 | - sectname = mo.group('header').strip() | ||
| 420 | + sectname: str = mo.group('header').strip() | ||
| 428 | 421 | if sectname in self._sections: | |
| 429 | 422 | cursect = self._sections[sectname] | |
| 430 | 423 | elif sectname == cp.DEFAULTSECT: | |
@@ -535,7 +528,7 @@ def _included_paths(self) -> List[Tuple[str, str]]: | |||
| 535 | 528 | ||
| 536 | 529 | return paths | |
| 537 | 530 | ||
| 538 | - def read(self) -> None: | ||
| 531 | + def read(self) -> None: # type: ignore[override] | ||
| 539 | 532 | """Reads the data stored in the files we have been initialized with. It will | |
| 540 | 533 | ignore files that cannot be read, possibly leaving an empty configuration | |
| 541 | 534 | ||
@@ -626,7 +619,7 @@ def write_section(name, section_dict): | |||
| 626 | 619 | for name, value in self._sections.items(): | |
| 627 | 620 | write_section(name, value) | |
| 628 | 621 | ||
| 629 | - def items(self, section_name: str) -> List[Tuple[str, str]]: | ||
| 622 | + def items(self, section_name: str) -> List[Tuple[str, str]]: # type: ignore[override] | ||
| 630 | 623 | """:return: list((option, value), ...) pairs of all items in the given section""" | |
| 631 | 624 | return [(k, v) for k, v in super(GitConfigParser, self).items(section_name) if k != '__name__'] | |
| 632 | 625 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,7 +82,8 @@ class BaseIndexEntry(BaseIndexEntryHelper): | |||
| 82 | 82 | ||
| 83 | 83 | As the first 4 data members match exactly to the IndexEntry type, methods | |
| 84 | 84 | expecting a BaseIndexEntry can also handle full IndexEntries even if they | |
| 85 | - use numeric indices for performance reasons. """ | ||
| 85 | + use numeric indices for performance reasons. | ||
| 86 | + """ | ||
| 86 | 87 | ||
| 87 | 88 | def __new__(cls, inp_tuple: Union[Tuple[int, bytes, int, PathLike], | |
| 88 | 89 | Tuple[int, bytes, int, PathLike, bytes, bytes, int, int, int, int, int]] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -965,13 +965,12 @@ def remove(self, module: bool = True, force: bool = False, | |||
| 965 | 965 | ||
| 966 | 966 | # now git config - need the config intact, otherwise we can't query | |
| 967 | 967 | # information anymore | |
| 968 | - writer: Union[GitConfigParser, SectionConstraint] | ||
| 969 | 968 | ||
| 970 | - with self.repo.config_writer() as writer: | ||
| 971 | - writer.remove_section(sm_section(self.name)) | ||
| 969 | + with self.repo.config_writer() as gcp_writer: | ||
| 970 | + gcp_writer.remove_section(sm_section(self.name)) | ||
| 972 | 971 | ||
| 973 | - with self.config_writer() as writer: | ||
| 974 | - writer.remove_section() | ||
| 972 | + with self.config_writer() as sc_writer: | ||
| 973 | + sc_writer.remove_section() | ||
| 975 | 974 | # END delete configuration | |
| 976 | 975 | ||
| 977 | 976 | return self | |
@@ -1024,7 +1023,8 @@ def set_parent_commit(self, commit: Union[Commit_ish, None], check: bool = True) | |||
| 1024 | 1023 | return self | |
| 1025 | 1024 | ||
| 1026 | 1025 | @unbare_repo | |
| 1027 | - def config_writer(self, index: Union['IndexFile', None] = None, write: bool = True) -> SectionConstraint: | ||
| 1026 | + def config_writer(self, index: Union['IndexFile', None] = None, write: bool = True | ||
| 1027 | + ) -> SectionConstraint['SubmoduleConfigParser']: | ||
| 1028 | 1028 | """:return: a config writer instance allowing you to read and write the data | |
| 1029 | 1029 | belonging to this submodule into the .gitmodules file. | |
| 1030 | 1030 | ||
@@ -1201,7 +1201,7 @@ def name(self) -> str: | |||
| 1201 | 1201 | """ | |
| 1202 | 1202 | return self._name | |
| 1203 | 1203 | ||
| 1204 | - def config_reader(self) -> SectionConstraint: | ||
| 1204 | + def config_reader(self) -> SectionConstraint[SubmoduleConfigParser]: | ||
| 1205 | 1205 | """ | |
| 1206 | 1206 | :return: ConfigReader instance which allows you to qurey the configuration values | |
| 1207 | 1207 | of this submodule, as provided by the .gitmodules file | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -100,7 +100,7 @@ def flush_to_index(self) -> None: | |||
| 100 | 100 | #} END interface | |
| 101 | 101 | ||
| 102 | 102 | #{ Overridden Methods | |
| 103 | - def write(self) -> None: | ||
| 103 | + def write(self) -> None: # type: ignore[override] | ||
| 104 | 104 | rval: None = super(SubmoduleConfigParser, self).write() | |
| 105 | 105 | self.flush_to_index() | |
| 106 | 106 | return rval | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,7 @@ | |||
| 23 | 23 | ) | |
| 24 | 24 | ||
| 25 | 25 | from .config import ( | |
| 26 | + GitConfigParser, | ||
| 26 | 27 | SectionConstraint, | |
| 27 | 28 | cp, | |
| 28 | 29 | ) | |
@@ -911,7 +912,7 @@ def push(self, refspec: Union[str, List[str], None] = None, | |||
| 911 | 912 | return self._get_push_info(proc, progress) | |
| 912 | 913 | ||
| 913 | 914 | @ property | |
| 914 | - def config_reader(self) -> SectionConstraint: | ||
| 915 | + def config_reader(self) -> SectionConstraint[GitConfigParser]: | ||
| 915 | 916 | """ | |
| 916 | 917 | :return: | |
| 917 | 918 | GitConfigParser compatible object able to read options for only our remote. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -482,7 +482,8 @@ def _get_config_path(self, config_level: Lit_config_levels) -> str: | |||
| 482 | 482 | ||
| 483 | 483 | raise ValueError("Invalid configuration level: %r" % config_level) | |
| 484 | 484 | ||
| 485 | - def config_reader(self, config_level: Optional[Lit_config_levels] = None) -> GitConfigParser: | ||
| 485 | + def config_reader(self, config_level: Optional[Lit_config_levels] = None | ||
| 486 | + ) -> GitConfigParser: | ||
| 486 | 487 | """ | |
| 487 | 488 | :return: | |
| 488 | 489 | GitConfigParser allowing to read the full git configuration, but not to write it | |
@@ -504,7 +505,8 @@ def config_reader(self, config_level: Optional[Lit_config_levels] = None) -> Git | |||
| 504 | 505 | files = [self._get_config_path(config_level)] | |
| 505 | 506 | return GitConfigParser(files, read_only=True, repo=self) | |
| 506 | 507 | ||
| 507 | - def config_writer(self, config_level: Lit_config_levels = "repository") -> GitConfigParser: | ||
| 508 | + def config_writer(self, config_level: Lit_config_levels = "repository" | ||
| 509 | + ) -> GitConfigParser: | ||
| 508 | 510 | """ | |
| 509 | 511 | :return: | |
| 510 | 512 | GitConfigParser allowing to write values of the specified configuration file level. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments