Skip to content

Commit c5e6ae2

Browse files
authored
Merge pull request #1298 from Yobmod/main
Revert use of Typeguard and therefore typing-extensions==3.10.0.0
2 parents 0c1446d + be7bb86 commit c5e6ae2

File tree

15 files changed

+140
-149
lines changed

15 files changed

+140
-149
lines changed

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ignore = E265,E266,E731,E704,
2020
W293, W504,
2121
ANN0 ANN1 ANN2,
2222
TC002,
23-
# TC0, TC1, TC2
23+
TC0, TC1, TC2
2424
# B,
2525
A,
2626
D,

git/config.py

+36-33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""Module containing module parser implementation able to properly read and write
77
configuration files"""
88

9+
import sys
910
import abc
1011
from functools import wraps
1112
import inspect
@@ -14,12 +15,10 @@
1415
import os
1516
import re
1617
import fnmatch
17-
from collections import OrderedDict
1818

1919
from git.compat import (
2020
defenc,
2121
force_text,
22-
with_metaclass,
2322
is_win,
2423
)
2524

@@ -31,15 +30,24 @@
3130

3231
# typing-------------------------------------------------------
3332

34-
from typing import (Any, Callable, IO, List, Dict, Sequence,
35-
TYPE_CHECKING, Tuple, Union, cast, overload)
33+
from typing import (Any, Callable, Generic, IO, List, Dict, Sequence,
34+
TYPE_CHECKING, Tuple, TypeVar, Union, cast, overload)
3635

37-
from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, TBD, assert_never, is_config_level
36+
from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, TBD, assert_never, _T
3837

3938
if TYPE_CHECKING:
4039
from git.repo.base import Repo
4140
from io import BytesIO
4241

42+
T_ConfigParser = TypeVar('T_ConfigParser', bound='GitConfigParser')
43+
44+
if sys.version_info[:2] < (3, 7):
45+
from collections import OrderedDict
46+
OrderedDict_OMD = OrderedDict
47+
else:
48+
from typing import OrderedDict
49+
OrderedDict_OMD = OrderedDict[str, List[_T]]
50+
4351
# -------------------------------------------------------------
4452

4553
__all__ = ('GitConfigParser', 'SectionConstraint')
@@ -61,7 +69,6 @@
6169

6270

6371
class MetaParserBuilder(abc.ABCMeta):
64-
6572
"""Utlity class wrapping base-class methods into decorators that assure read-only properties"""
6673
def __new__(cls, name: str, bases: TBD, clsdict: Dict[str, Any]) -> TBD:
6774
"""
@@ -115,7 +122,7 @@ def flush_changes(self, *args: Any, **kwargs: Any) -> Any:
115122
return flush_changes
116123

117124

118-
class SectionConstraint(object):
125+
class SectionConstraint(Generic[T_ConfigParser]):
119126

120127
"""Constrains a ConfigParser to only option commands which are constrained to
121128
always use the section we have been initialized with.
@@ -128,7 +135,7 @@ class SectionConstraint(object):
128135
_valid_attrs_ = ("get_value", "set_value", "get", "set", "getint", "getfloat", "getboolean", "has_option",
129136
"remove_section", "remove_option", "options")
130137

131-
def __init__(self, config: 'GitConfigParser', section: str) -> None:
138+
def __init__(self, config: T_ConfigParser, section: str) -> None:
132139
self._config = config
133140
self._section_name = section
134141

@@ -149,26 +156,26 @@ def _call_config(self, method: str, *args: Any, **kwargs: Any) -> Any:
149156
return getattr(self._config, method)(self._section_name, *args, **kwargs)
150157

151158
@property
152-
def config(self) -> 'GitConfigParser':
159+
def config(self) -> T_ConfigParser:
153160
"""return: Configparser instance we constrain"""
154161
return self._config
155162

156163
def release(self) -> None:
157164
"""Equivalent to GitConfigParser.release(), which is called on our underlying parser instance"""
158165
return self._config.release()
159166

160-
def __enter__(self) -> 'SectionConstraint':
167+
def __enter__(self) -> 'SectionConstraint[T_ConfigParser]':
161168
self._config.__enter__()
162169
return self
163170

164171
def __exit__(self, exception_type: str, exception_value: str, traceback: str) -> None:
165172
self._config.__exit__(exception_type, exception_value, traceback)
166173

167174

168-
class _OMD(OrderedDict):
175+
class _OMD(OrderedDict_OMD):
169176
"""Ordered multi-dict."""
170177

171-
def __setitem__(self, key: str, value: Any) -> None:
178+
def __setitem__(self, key: str, value: _T) -> None: # type: ignore[override]
172179
super(_OMD, self).__setitem__(key, [value])
173180

174181
def add(self, key: str, value: Any) -> None:
@@ -177,7 +184,7 @@ def add(self, key: str, value: Any) -> None:
177184
return None
178185
super(_OMD, self).__getitem__(key).append(value)
179186

180-
def setall(self, key: str, values: Any) -> None:
187+
def setall(self, key: str, values: List[_T]) -> None:
181188
super(_OMD, self).__setitem__(key, values)
182189

183190
def __getitem__(self, key: str) -> Any:
@@ -194,25 +201,17 @@ def setlast(self, key: str, value: Any) -> None:
194201
prior = super(_OMD, self).__getitem__(key)
195202
prior[-1] = value
196203

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]
204+
def get(self, key: str, default: Union[_T, None] = None) -> Union[_T, None]: # type: ignore
205+
return super(_OMD, self).get(key, [default])[-1] # type: ignore
207206

208-
def getall(self, key: str) -> Any:
207+
def getall(self, key: str) -> List[_T]:
209208
return super(_OMD, self).__getitem__(key)
210209

211-
def items(self) -> List[Tuple[str, Any]]: # type: ignore[override]
210+
def items(self) -> List[Tuple[str, _T]]: # type: ignore[override]
212211
"""List of (key, last value for key)."""
213212
return [(k, self[k]) for k in self]
214213

215-
def items_all(self) -> List[Tuple[str, List[Any]]]:
214+
def items_all(self) -> List[Tuple[str, List[_T]]]:
216215
"""List of (key, list of values for key)."""
217216
return [(k, self.getall(k)) for k in self]
218217

@@ -238,7 +237,7 @@ def get_config_path(config_level: Lit_config_levels) -> str:
238237
assert_never(config_level, ValueError(f"Invalid configuration level: {config_level!r}"))
239238

240239

241-
class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser)): # type: ignore ## mypy does not understand dynamic class creation # noqa: E501
240+
class GitConfigParser(cp.RawConfigParser, metaclass=MetaParserBuilder):
242241

243242
"""Implements specifics required to read git style configuration files.
244243
@@ -298,7 +297,10 @@ def __init__(self, file_or_files: Union[None, PathLike, 'BytesIO', Sequence[Unio
298297
:param repo: Reference to repository to use if [includeIf] sections are found in configuration files.
299298
300299
"""
301-
cp.RawConfigParser.__init__(self, dict_type=_OMD)
300+
cp.RawConfigParser.__init__(self, dict_type=_OMD) # type: ignore[arg-type]
301+
self._dict: Callable[..., _OMD] # type: ignore[assignment] # mypy/typeshed bug
302+
self._defaults: _OMD # type: ignore[assignment] # mypy/typeshed bug
303+
self._sections: _OMD # type: ignore[assignment] # mypy/typeshed bug
302304

303305
# Used in python 3, needs to stay in sync with sections for underlying implementation to work
304306
if not hasattr(self, '_proxies'):
@@ -309,9 +311,9 @@ def __init__(self, file_or_files: Union[None, PathLike, 'BytesIO', Sequence[Unio
309311
else:
310312
if config_level is None:
311313
if read_only:
312-
self._file_or_files = [get_config_path(f)
314+
self._file_or_files = [get_config_path(cast(Lit_config_levels, f))
313315
for f in CONFIG_LEVELS
314-
if is_config_level(f) and f != 'repository']
316+
if f != 'repository']
315317
else:
316318
raise ValueError("No configuration level or configuration files specified")
317319
else:
@@ -424,7 +426,7 @@ def string_decode(v: str) -> str:
424426
# is it a section header?
425427
mo = self.SECTCRE.match(line.strip())
426428
if not is_multi_line and mo:
427-
sectname = mo.group('header').strip()
429+
sectname: str = mo.group('header').strip()
428430
if sectname in self._sections:
429431
cursect = self._sections[sectname]
430432
elif sectname == cp.DEFAULTSECT:
@@ -535,7 +537,7 @@ def _included_paths(self) -> List[Tuple[str, str]]:
535537

536538
return paths
537539

538-
def read(self) -> None:
540+
def read(self) -> None: # type: ignore[override]
539541
"""Reads the data stored in the files we have been initialized with. It will
540542
ignore files that cannot be read, possibly leaving an empty configuration
541543
@@ -623,10 +625,11 @@ def write_section(name, section_dict):
623625

624626
if self._defaults:
625627
write_section(cp.DEFAULTSECT, self._defaults)
628+
value: TBD
626629
for name, value in self._sections.items():
627630
write_section(name, value)
628631

629-
def items(self, section_name: str) -> List[Tuple[str, str]]:
632+
def items(self, section_name: str) -> List[Tuple[str, str]]: # type: ignore[override]
630633
""":return: list((option, value), ...) pairs of all items in the given section"""
631634
return [(k, v) for k, v in super(GitConfigParser, self).items(section_name) if k != '__name__']
632635

git/diff.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
# typing ------------------------------------------------------------------
1717

18-
from typing import Any, Iterator, List, Match, Optional, Tuple, Type, TypeVar, Union, TYPE_CHECKING
19-
from git.types import PathLike, TBD, Literal, TypeGuard
18+
from typing import Any, Iterator, List, Match, Optional, Tuple, Type, TypeVar, Union, TYPE_CHECKING, cast
19+
from git.types import PathLike, TBD, Literal
2020

2121
if TYPE_CHECKING:
2222
from .objects.tree import Tree
@@ -28,9 +28,9 @@
2828
Lit_change_type = Literal['A', 'D', 'C', 'M', 'R', 'T', 'U']
2929

3030

31-
def is_change_type(inp: str) -> TypeGuard[Lit_change_type]:
32-
# return True
33-
return inp in ['A', 'D', 'C', 'M', 'R', 'T', 'U']
31+
# def is_change_type(inp: str) -> TypeGuard[Lit_change_type]:
32+
# # return True
33+
# return inp in ['A', 'D', 'C', 'M', 'R', 'T', 'U']
3434

3535
# ------------------------------------------------------------------------
3636

@@ -517,8 +517,8 @@ def _handle_diff_line(lines_bytes: bytes, repo: 'Repo', index: DiffIndex) -> Non
517517
# Change type can be R100
518518
# R: status letter
519519
# 100: score (in case of copy and rename)
520-
assert is_change_type(_change_type[0]), f"Unexpected value for change_type received: {_change_type[0]}"
521-
change_type: Lit_change_type = _change_type[0]
520+
# assert is_change_type(_change_type[0]), f"Unexpected value for change_type received: {_change_type[0]}"
521+
change_type: Lit_change_type = cast(Lit_change_type, _change_type[0])
522522
score_str = ''.join(_change_type[1:])
523523
score = int(score_str) if score_str.isdigit() else None
524524
path = path.strip()

git/index/fun.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@
5353

5454
from typing import (Dict, IO, List, Sequence, TYPE_CHECKING, Tuple, Type, Union, cast)
5555

56-
from git.types import PathLike, TypeGuard
56+
from git.types import PathLike
5757

5858
if TYPE_CHECKING:
5959
from .base import IndexFile
60+
from git.db import GitCmdObjectDB
6061
from git.objects.tree import TreeCacheTup
6162
# from git.objects.fun import EntryTupOrNone
6263

@@ -149,15 +150,15 @@ def write_cache(entries: Sequence[Union[BaseIndexEntry, 'IndexEntry']], stream:
149150
# body
150151
for entry in entries:
151152
beginoffset = tell()
152-
write(entry[4]) # ctime
153-
write(entry[5]) # mtime
154-
path_str: str = entry[3]
153+
write(entry.ctime_bytes) # ctime
154+
write(entry.mtime_bytes) # mtime
155+
path_str = str(entry.path)
155156
path: bytes = force_bytes(path_str, encoding=defenc)
156157
plen = len(path) & CE_NAMEMASK # path length
157-
assert plen == len(path), "Path %s too long to fit into index" % entry[3]
158-
flags = plen | (entry[2] & CE_NAMEMASK_INV) # clear possible previous values
159-
write(pack(">LLLLLL20sH", entry[6], entry[7], entry[0],
160-
entry[8], entry[9], entry[10], entry[1], flags))
158+
assert plen == len(path), "Path %s too long to fit into index" % entry.path
159+
flags = plen | (entry.flags & CE_NAMEMASK_INV) # clear possible previous values
160+
write(pack(">LLLLLL20sH", entry.dev, entry.inode, entry.mode,
161+
entry.uid, entry.gid, entry.size, entry.binsha, flags))
161162
write(path)
162163
real_size = ((tell() - beginoffset + 8) & ~7)
163164
write(b"\0" * ((beginoffset + real_size) - tell()))
@@ -188,15 +189,16 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i
188189
""":return: Key suitable to be used for the index.entries dictionary
189190
:param entry: One instance of type BaseIndexEntry or the path and the stage"""
190191

191-
def is_entry_key_tup(entry_key: Tuple) -> TypeGuard[Tuple[PathLike, int]]:
192-
return isinstance(entry_key, tuple) and len(entry_key) == 2
192+
# def is_entry_key_tup(entry_key: Tuple) -> TypeGuard[Tuple[PathLike, int]]:
193+
# return isinstance(entry_key, tuple) and len(entry_key) == 2
193194

194195
if len(entry) == 1:
195196
entry_first = entry[0]
196197
assert isinstance(entry_first, BaseIndexEntry)
197198
return (entry_first.path, entry_first.stage)
198199
else:
199-
assert is_entry_key_tup(entry)
200+
# assert is_entry_key_tup(entry)
201+
entry = cast(Tuple[PathLike, int], entry)
200202
return entry
201203
# END handle entry
202204

@@ -244,7 +246,7 @@ def read_cache(stream: IO[bytes]) -> Tuple[int, Dict[Tuple[PathLike, int], 'Inde
244246
content_sha = extension_data[-20:]
245247

246248
# truncate the sha in the end as we will dynamically create it anyway
247-
extension_data = extension_data[:-20]
249+
extension_data = extension_data[: -20]
248250

249251
return (version, entries, extension_data, content_sha)
250252

@@ -310,7 +312,7 @@ def _tree_entry_to_baseindexentry(tree_entry: 'TreeCacheTup', stage: int) -> Bas
310312
return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2]))
311313

312314

313-
def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]:
315+
def aggressive_tree_merge(odb: 'GitCmdObjectDB', tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]:
314316
"""
315317
:return: list of BaseIndexEntries representing the aggressive merge of the given
316318
trees. All valid entries are on stage 0, whereas the conflicting ones are left

0 commit comments

Comments
 (0)