Skip to content

Commit f89e18c

Browse files
authored
Merge pull request #2174 from gitpython-developers/basedpyright
typing: introduce sensible basedpyright defaults
2 parents e87854b + 6d0b6a4 commit f89e18c

3 files changed

Lines changed: 40 additions & 19 deletions

File tree

git/cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ def execute(
13581358

13591359
# Allow the user to have the command executed in their working dir.
13601360
try:
1361-
cwd = self._working_dir or os.getcwd() # type: Union[None, str]
1361+
cwd = self._working_dir or os.getcwd() # type: Optional[PathLike]
13621362
if not os.access(str(cwd), os.X_OK):
13631363
cwd = None
13641364
except FileNotFoundError:

git/util.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def rmtree(path: PathLike) -> None:
217217
couldn't be deleted are read-only. Windows will not remove them in that case.
218218
"""
219219

220-
def handler(function: Callable, path: PathLike, _excinfo: Any) -> None:
220+
def handler(function: Callable[[str], Any], path: str, _excinfo: Any) -> None:
221221
"""Callback for :func:`shutil.rmtree`.
222222
223223
This works as either a ``onexc`` or ``onerror`` style callback.
@@ -401,7 +401,7 @@ def _cygexpath(drive: Optional[str], path: str, expand_vars: bool = True) -> str
401401
return p_str.replace("\\", "/")
402402

403403

404-
_cygpath_parsers: Tuple[Tuple[Pattern[str], Callable, bool], ...] = (
404+
_cygpath_parsers: Tuple[Tuple[Pattern[str], Callable[..., str], bool], ...] = (
405405
# See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
406406
# and: https://www.cygwin.com/cygwin-ug-net/using.html#unc-paths
407407
(
@@ -508,7 +508,7 @@ def get_user_id() -> str:
508508
return "%s@%s" % (getpass.getuser(), platform.node())
509509

510510

511-
def finalize_process(proc: Union[subprocess.Popen, "Git.AutoInterrupt"], **kwargs: Any) -> None:
511+
def finalize_process(proc: Union["subprocess.Popen[Any]", "Git.AutoInterrupt"], **kwargs: Any) -> None:
512512
"""Wait for the process (clone, fetch, pull or push) and handle its errors
513513
accordingly."""
514514
# TODO: No close proc-streams??
@@ -520,19 +520,21 @@ def expand_path(p: None, expand_vars: bool = ...) -> None: ...
520520

521521

522522
@overload
523-
def expand_path(p: PathLike, expand_vars: bool = ...) -> str:
523+
def expand_path(p: PathLike, expand_vars: bool = ...) -> Optional[PathLike]:
524524
# TODO: Support for Python 3.5 has been dropped, so these overloads can be improved.
525525
...
526526

527527

528528
def expand_path(p: Union[None, PathLike], expand_vars: bool = True) -> Optional[PathLike]:
529-
if isinstance(p, Path):
530-
return p.resolve()
529+
if p is None:
530+
return None
531531
try:
532-
p = osp.expanduser(p) # type: ignore[arg-type]
532+
if isinstance(p, Path):
533+
return p.resolve()
534+
expanded_path = osp.expanduser(os.fspath(p))
533535
if expand_vars:
534-
p = osp.expandvars(p)
535-
return osp.normpath(osp.abspath(p))
536+
expanded_path = osp.expandvars(expanded_path)
537+
return osp.normpath(osp.abspath(expanded_path))
536538
except Exception:
537539
return None
538540

@@ -767,7 +769,7 @@ class CallableRemoteProgress(RemoteProgress):
767769

768770
__slots__ = ("_callable",)
769771

770-
def __init__(self, fn: Callable) -> None:
772+
def __init__(self, fn: Callable[..., Any]) -> None:
771773
self._callable = fn
772774
super().__init__()
773775

@@ -846,7 +848,7 @@ def _main_actor(
846848
cls,
847849
env_name: str,
848850
env_email: str,
849-
config_reader: Union[None, "GitConfigParser", "SectionConstraint"] = None,
851+
config_reader: Union[None, "GitConfigParser", "SectionConstraint[GitConfigParser]"] = None,
850852
) -> "Actor":
851853
actor = Actor("", "")
852854
user_id = None # We use this to avoid multiple calls to getpass.getuser().
@@ -882,7 +884,9 @@ def default_name() -> str:
882884
return actor
883885

884886
@classmethod
885-
def committer(cls, config_reader: Union[None, "GitConfigParser", "SectionConstraint"] = None) -> "Actor":
887+
def committer(
888+
cls, config_reader: Union[None, "GitConfigParser", "SectionConstraint[GitConfigParser]"] = None
889+
) -> "Actor":
886890
"""
887891
:return:
888892
:class:`Actor` instance corresponding to the configured committer. It
@@ -897,7 +901,9 @@ def committer(cls, config_reader: Union[None, "GitConfigParser", "SectionConstra
897901
return cls._main_actor(cls.env_committer_name, cls.env_committer_email, config_reader)
898902

899903
@classmethod
900-
def author(cls, config_reader: Union[None, "GitConfigParser", "SectionConstraint"] = None) -> "Actor":
904+
def author(
905+
cls, config_reader: Union[None, "GitConfigParser", "SectionConstraint[GitConfigParser]"] = None
906+
) -> "Actor":
901907
"""Same as :meth:`committer`, but defines the main author. It may be specified
902908
in the environment, but defaults to the committer."""
903909
return cls._main_actor(cls.env_author_name, cls.env_author_email, config_reader)
@@ -980,11 +986,11 @@ class IndexFileSHA1Writer:
980986

981987
__slots__ = ("f", "sha1")
982988

983-
def __init__(self, f: IO) -> None:
989+
def __init__(self, f: IO[bytes]) -> None:
984990
self.f = f
985991
self.sha1 = make_sha(b"")
986992

987-
def write(self, data: AnyStr) -> int:
993+
def write(self, data: bytes) -> int:
988994
self.sha1.update(data)
989995
return self.f.write(data)
990996

@@ -1181,6 +1187,7 @@ def __new__(cls, id_attr: str, prefix: str = "") -> "IterableList[T_IterableObj]
11811187
return super().__new__(cls)
11821188

11831189
def __init__(self, id_attr: str, prefix: str = "") -> None:
1190+
super().__init__()
11841191
self._id_attr = id_attr
11851192
self._prefix = prefix
11861193

@@ -1210,7 +1217,9 @@ def __getattr__(self, attr: str) -> T_IterableObj:
12101217
# END for each item
12111218
return list.__getattribute__(self, attr)
12121219

1213-
def __getitem__(self, index: Union[SupportsIndex, int, slice, str]) -> T_IterableObj: # type: ignore[override]
1220+
def __getitem__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
1221+
self, index: Union[SupportsIndex, int, slice, str]
1222+
) -> T_IterableObj:
12141223
if isinstance(index, int):
12151224
return list.__getitem__(self, index)
12161225
elif isinstance(index, slice):
@@ -1288,7 +1297,7 @@ def list_items(cls, repo: "Repo", *args: Any, **kwargs: Any) -> IterableList[T_I
12881297
:return:
12891298
list(Item,...) list of item instances
12901299
"""
1291-
out_list: IterableList = IterableList(cls._id_attribute_)
1300+
out_list: IterableList[T_IterableObj] = IterableList(cls._id_attribute_)
12921301
out_list.extend(cls.iter_items(repo, *args, **kwargs))
12931302
return out_list
12941303

@@ -1297,7 +1306,8 @@ class IterableClassWatcher(type):
12971306
"""Metaclass that issues :exc:`DeprecationWarning` when :class:`git.util.Iterable`
12981307
is subclassed."""
12991308

1300-
def __init__(cls, name: str, bases: Tuple, clsdict: Dict) -> None:
1309+
def __init__(cls, name: str, bases: Tuple[type, ...], clsdict: Dict[str, Any]) -> None:
1310+
super().__init__(name, bases, clsdict)
13011311
for base in bases:
13021312
if type(base) is IterableClassWatcher:
13031313
warnings.warn(

pyproject.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ exclude = ["^git/ext/gitdb"]
3333
module = "gitdb.*"
3434
ignore_missing_imports = true
3535

36+
[tool.basedpyright]
37+
typeCheckingMode = "standard"
38+
pythonVersion = "3.7"
39+
extraPaths = [
40+
"git/ext/gitdb",
41+
"git/ext/gitdb/gitdb/ext/smmap",
42+
]
43+
exclude = [
44+
"git/ext/gitdb",
45+
]
46+
3647
[tool.coverage.run]
3748
source = ["git"]
3849

0 commit comments

Comments
 (0)