Fix typing issues with delete_head and Remote.add · gitpython-developers/GitPython@5f4b4db · GitHub
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

4 files changed

Lines changed: 11 additions & 4 deletions

File tree

git/refs/head.py

Lines changed: 1 addition & 1 deletion

git/refs/remote.py

Lines changed: 6 additions & 1 deletion
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:

git/remote.py

Lines changed: 3 additions & 1 deletion
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:

git/repo/base.py

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)