Skip to content

Add initial types to base and fun #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
put type imports in guard
  • Loading branch information
Yobmod committed Mar 4, 2021
commit 5df05e11d0d567c408a1e9c0fcf1af2afca6879d
21 changes: 11 additions & 10 deletions git/repo/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

# Typing ----------------------------------------------------------------------

from .base import Repo
from git.db import GitCmdObjectDB
from git.objects import Commit, TagObject, Blob, Tree
from typing import AnyStr, Union, Optional, cast
from typing import AnyStr, Union, Optional, cast, TYPE_CHECKING
from git.types import PathLike
if TYPE_CHECKING:
from .base import Repo
from git.db import GitCmdObjectDB
from git.objects import Commit, TagObject, Blob, Tree

# ----------------------------------------------------------------------------

Expand Down Expand Up @@ -102,7 +103,7 @@ def find_submodule_git_dir(d: PathLike) -> Optional[PathLike]:
return None


def short_to_long(odb: GitCmdObjectDB, hexsha: AnyStr) -> Optional[bytes]:
def short_to_long(odb: 'GitCmdObjectDB', hexsha: AnyStr) -> Optional[bytes]:
""":return: long hexadecimal sha1 from the given less-than-40 byte hexsha
or None if no candidate could be found.
:param hexsha: hexsha with less than 40 byte"""
Expand All @@ -113,8 +114,8 @@ def short_to_long(odb: GitCmdObjectDB, hexsha: AnyStr) -> Optional[bytes]:
# END exception handling


def name_to_object(repo: Repo, name: str, return_ref: bool = False,
) -> Union[SymbolicReference, Commit, TagObject, Blob, Tree]:
def name_to_object(repo: 'Repo', name: str, return_ref: bool = False,
) -> Union[SymbolicReference, 'Commit', 'TagObject', 'Blob', 'Tree']:
"""
:return: object specified by the given name, hexshas ( short and long )
as well as references are supported
Expand Down Expand Up @@ -161,7 +162,7 @@ def name_to_object(repo: Repo, name: str, return_ref: bool = False,
return Object.new_from_sha(repo, hex_to_bin(hexsha))


def deref_tag(tag: Tag) -> TagObject:
def deref_tag(tag: Tag) -> 'TagObject':
"""Recursively dereference a tag and return the resulting object"""
while True:
try:
Expand All @@ -172,7 +173,7 @@ def deref_tag(tag: Tag) -> TagObject:
return tag


def to_commit(obj: Object) -> Union[Commit, TagObject]:
def to_commit(obj: Object) -> Union['Commit', 'TagObject']:
"""Convert the given object to a commit if possible and return it"""
if obj.type == 'tag':
obj = deref_tag(obj)
Expand All @@ -183,7 +184,7 @@ def to_commit(obj: Object) -> Union[Commit, TagObject]:
return obj


def rev_parse(repo: Repo, rev: str) -> Union[Commit, Tag, Tree, Blob]:
def rev_parse(repo: 'Repo', rev: str) -> Union['Commit', 'Tag', 'Tree', 'Blob']:
"""
:return: Object at the given revision, either Commit, Tag, Tree or Blob
:param rev: git-rev-parse compatible revision specification as string, please see
Expand Down