Skip to content

Commit 5f4b4db

Browse files
committed
Fix typing issues with delete_head and Remote.add
delete_head and Head.delete historically accept either Head objects or a str name of a head. Adjust the typing to match. This unfortunately requires suppressing type warnings in the signature of RemoteReference.delete, since it inherits from Head but does not accept str (since it needs access to the richer data of RemoteReference). Using assignment to make add an alias for create unfortunately confuses mypy, since it loses track of the fact that it's a classmethod and starts treating it like a staticmethod. Replace with a stub wrapper instead.
1 parent 2d15c5a commit 5f4b4db

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

Diff for: git/refs/head.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Head(Reference):
129129
k_config_remote_ref = "merge" # branch to merge from remote
130130

131131
@classmethod
132-
def delete(cls, repo: 'Repo', *heads: 'Head', force: bool = False, **kwargs: Any) -> None:
132+
def delete(cls, repo: 'Repo', *heads: 'Union[Head, str]', force: bool = False, **kwargs: Any) -> None:
133133
"""Delete the given heads
134134
135135
:param force:

Diff for: git/refs/remote.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ def iter_items(cls, repo: 'Repo', common_path: Union[PathLike, None] = None,
3737
# super is Reference
3838
return super(RemoteReference, cls).iter_items(repo, common_path)
3939

40+
# The Head implementation of delete also accepts strs, but this
41+
# implementation does not. mypy doesn't have a way of representing
42+
# tightening the types of arguments in subclasses and recommends Any or
43+
# "type: ignore". (See https://github.com/python/typing/issues/241)
4044
@ classmethod
41-
def delete(cls, repo: 'Repo', *refs: 'RemoteReference', **kwargs: Any) -> None:
45+
def delete(cls, repo: 'Repo', *refs: 'RemoteReference', # type: ignore
46+
**kwargs: Any) -> None:
4247
"""Delete the given remote references
4348
4449
:note:

Diff for: git/remote.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,9 @@ def create(cls, repo: 'Repo', name: str, url: str, **kwargs: Any) -> 'Remote':
665665
return cls(repo, name)
666666

667667
# add is an alias
668-
add = create
668+
@ classmethod
669+
def add(cls, repo: 'Repo', name: str, url: str, **kwargs: Any) -> 'Remote':
670+
return cls.create(repo, name, url, **kwargs)
669671

670672
@ classmethod
671673
def remove(cls, repo: 'Repo', name: str) -> str:

Diff for: git/repo/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def create_head(self, path: PathLike, commit: str = 'HEAD',
429429
:return: newly created Head Reference"""
430430
return Head.create(self, path, commit, logmsg, force)
431431

432-
def delete_head(self, *heads: 'Head', **kwargs: Any) -> None:
432+
def delete_head(self, *heads: 'Union[str, Head]', **kwargs: Any) -> None:
433433
"""Delete the given heads
434434
435435
:param kwargs: Additional keyword arguments to be passed to git-branch"""

0 commit comments

Comments
 (0)