Skip to content

Commit 390efbf

Browse files
committed
Fix more missing types in Symbolic.py, cos GuthubActions pytest stuck
1 parent b8b07b9 commit 390efbf

File tree

6 files changed

+48
-25
lines changed

6 files changed

+48
-25
lines changed

‎git/objects/submodule/base.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
563563
progress.update(op, i, len_rmts, prefix + "Done fetching remote of submodule %r" % self.name)
564564
# END fetch new data
565565
except InvalidGitRepositoryError:
566+
mrepo = None
566567
if not init:
567568
return self
568569
# END early abort if init is not allowed
@@ -603,7 +604,7 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
603604

604605
# make sure HEAD is not detached
605606
mrepo.head.set_reference(local_branch, logmsg="submodule: attaching head to %s" % local_branch)
606-
mrepo.head.ref.set_tracking_branch(remote_branch)
607+
mrepo.head.reference.set_tracking_branch(remote_branch)
607608
except (IndexError, InvalidGitRepositoryError):
608609
log.warning("Failed to checkout tracking branch %s", self.branch_path)
609610
# END handle tracking branch
@@ -629,13 +630,14 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
629630
if mrepo is not None and to_latest_revision:
630631
msg_base = "Cannot update to latest revision in repository at %r as " % mrepo.working_dir
631632
if not is_detached:
632-
rref = mrepo.head.ref.tracking_branch()
633+
rref = mrepo.head.reference.tracking_branch()
633634
if rref is not None:
634635
rcommit = rref.commit
635636
binsha = rcommit.binsha
636637
hexsha = rcommit.hexsha
637638
else:
638-
log.error("%s a tracking branch was not set for local branch '%s'", msg_base, mrepo.head.ref)
639+
log.error("%s a tracking branch was not set for local branch '%s'",
640+
msg_base, mrepo.head.reference)
639641
# END handle remote ref
640642
else:
641643
log.error("%s there was no local tracking branch", msg_base)

‎git/refs/symbolic.py

+25-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
if TYPE_CHECKING:
2727
from git.repo import Repo
28-
from git.refs import Reference, Head, HEAD, TagReference, RemoteReference
28+
from git.refs import Reference, Head, TagReference, RemoteReference
2929

3030
T_References = TypeVar('T_References', bound='SymbolicReference')
3131

@@ -60,6 +60,7 @@ class SymbolicReference(object):
6060
def __init__(self, repo: 'Repo', path: PathLike, check_path: bool = False) -> None:
6161
self.repo = repo
6262
self.path = str(path)
63+
self.ref = self._get_reference()
6364

6465
def __str__(self) -> str:
6566
return self.path
@@ -279,7 +280,7 @@ def set_object(self, object, logmsg=None): # @ReservedAssignment
279280
object = property(_get_object, set_object, doc="Return the object our ref currently refers to")
280281

281282
def _get_reference(self
282-
) -> Union['HEAD', 'Head', 'RemoteReference', 'TagReference', 'Reference', 'SymbolicReference']:
283+
) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
283284
""":return: Reference Object we point to
284285
:raise TypeError: If this symbolic reference is detached, hence it doesn't point
285286
to a reference, but to a commit"""
@@ -288,7 +289,8 @@ def _get_reference(self
288289
raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha))
289290
return self.from_path(self.repo, target_ref_path)
290291

291-
def set_reference(self, ref, logmsg=None):
292+
def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None
293+
) -> None:
292294
"""Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
293295
Otherwise an Object, given as Object instance or refspec, is assumed and if valid,
294296
will be set which effectively detaches the refererence if it was a purely
@@ -355,12 +357,16 @@ def set_reference(self, ref, logmsg=None):
355357
if logmsg is not None:
356358
self.log_append(oldbinsha, logmsg)
357359

358-
return self
360+
return None
359361

360-
# aliased reference
361-
reference: Union[Commit_ish, 'Head', 'Reference'] = property( # type: ignore
362-
_get_reference, set_reference, doc="Returns the Reference we point to")
363-
ref = reference
362+
@ property
363+
def reference(self) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
364+
return self._get_reference()
365+
366+
@ reference.setter
367+
def reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None
368+
) -> None:
369+
return self.set_reference(ref=ref, logmsg=logmsg)
364370

365371
def is_valid(self) -> bool:
366372
"""
@@ -374,7 +380,7 @@ def is_valid(self) -> bool:
374380
else:
375381
return True
376382

377-
@property
383+
@ property
378384
def is_detached(self):
379385
"""
380386
:return:
@@ -424,7 +430,7 @@ def log_entry(self, index):
424430
In that case, it will be faster than the ``log()`` method"""
425431
return RefLog.entry_at(RefLog.path(self), index)
426432

427-
@classmethod
433+
@ classmethod
428434
def to_full_path(cls, path: Union[PathLike, 'SymbolicReference']) -> str:
429435
"""
430436
:return: string with a full repository-relative path which can be used to initialize
@@ -439,7 +445,7 @@ def to_full_path(cls, path: Union[PathLike, 'SymbolicReference']) -> str:
439445
full_ref_path = '%s/%s' % (cls._common_path_default, path)
440446
return full_ref_path
441447

442-
@classmethod
448+
@ classmethod
443449
def delete(cls, repo: 'Repo', path: PathLike) -> None:
444450
"""Delete the reference at the given path
445451
@@ -497,8 +503,10 @@ def delete(cls, repo: 'Repo', path: PathLike) -> None:
497503
os.remove(reflog_path)
498504
# END remove reflog
499505

500-
@classmethod
501-
def _create(cls, repo, path, resolve, reference, force, logmsg=None):
506+
@ classmethod
507+
def _create(cls: Type[T_References], repo: 'Repo', path: PathLike, resolve: bool,
508+
reference: Union[str, 'SymbolicReference'],
509+
force: bool, logmsg: Union[str, None] = None) -> T_References:
502510
"""internal method used to create a new symbolic reference.
503511
If resolve is False, the reference will be taken as is, creating
504512
a proper symbolic reference. Otherwise it will be resolved to the
@@ -531,8 +539,9 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
531539
return ref
532540

533541
@classmethod
534-
def create(cls, repo: 'Repo', path: PathLike, reference: Union[Commit_ish, str] = 'SymbolicReference',
535-
logmsg: Union[str, None] = None, force: bool = False, **kwargs: Any):
542+
def create(cls: Type[T_References], repo: 'Repo', path: PathLike,
543+
reference: Union[Commit_ish, str, 'SymbolicReference'] = 'SymbolicReference',
544+
logmsg: Union[str, None] = None, force: bool = False, **kwargs: Any) -> T_References:
536545
"""Create a new symbolic reference, hence a reference pointing , to another reference.
537546
538547
:param repo:
@@ -669,7 +678,7 @@ def iter_items(cls, repo: 'Repo', common_path: Union[PathLike, None] = None, *ar
669678
return (r for r in cls._iter_items(repo, common_path) if r.__class__ == SymbolicReference or not r.is_detached)
670679

671680
@classmethod
672-
def from_path(cls, repo, path):
681+
def from_path(cls, repo: 'Repo', path: PathLike) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
673682
"""
674683
:param path: full .git-directory-relative path name to the Reference to instantiate
675684
:note: use to_full_path() if you only have a partial path of a known Reference Type

‎git/refs/tag.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
# typing ------------------------------------------------------------------
66

7-
from typing import Any, Union, TYPE_CHECKING
7+
from typing import Any, Type, Union, TYPE_CHECKING
88
from git.types import Commit_ish, PathLike
99

1010
if TYPE_CHECKING:
1111
from git.repo import Repo
1212
from git.objects import Commit
1313
from git.objects import TagObject
14+
from git.refs import SymbolicReference
1415

1516

1617
# ------------------------------------------------------------------------------
@@ -68,7 +69,8 @@ def object(self) -> Commit_ish: # type: ignore[override]
6869
return Reference._get_object(self)
6970

7071
@classmethod
71-
def create(cls, repo: 'Repo', path: PathLike, reference: Union[Commit_ish, str] = 'HEAD',
72+
def create(cls: Type['TagReference'], repo: 'Repo', path: PathLike,
73+
reference: Union[Commit_ish, str, 'SymbolicReference'] = 'HEAD',
7274
logmsg: Union[str, None] = None,
7375
force: bool = False, **kwargs: Any) -> 'TagReference':
7476
"""Create a new tag reference.

‎git/repo/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,10 @@ def ignored(self, *paths: PathLike) -> List[PathLike]:
788788
return proc.replace("\\\\", "\\").replace('"', "").split("\n")
789789

790790
@property
791-
def active_branch(self) -> 'HEAD':
791+
def active_branch(self) -> Head:
792792
"""The name of the currently active branch.
793793
:return: Head to the active branch"""
794+
# reveal_type(self.head.reference) # => Reference
794795
return self.head.reference
795796

796797
def blame_incremental(self, rev: TBD, file: TBD, **kwargs: Any) -> Optional[Iterator['BlameEntry']]:

‎pyproject.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ filterwarnings = 'ignore::DeprecationWarning'
1919
# filterwarnings ignore::WarningType # ignores those warnings
2020

2121
[tool.mypy]
22-
# disallow_untyped_defs = true
22+
# disallow_untyped_defs = True
2323
no_implicit_optional = true
2424
warn_redundant_casts = true
25-
# warn_unused_ignores = true
26-
# warn_unreachable = true
25+
# warn_unused_ignores = True
26+
# warn_unreachable = True
2727
show_error_codes = true
2828

2929
# TODO: remove when 'gitdb' is fully annotated

‎t.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from git import Repo
2+
3+
4+
def get_active_branch(gitobj: Repo) -> str:
5+
return gitobj.active_branch.name
6+
7+
8+
gitobj = Repo(".")
9+
print(get_active_branch(gitobj))

0 commit comments

Comments
 (0)