2222
2323from typing import Optional , TYPE_CHECKING , Union , cast , overload
2424
25- from git .types import AnyGitObject , Literal , Old_commit_ish , PathLike
25+ from git .types import AnyGitObject , Literal , PathLike
2626
2727if TYPE_CHECKING :
2828 from git .db import GitCmdObjectDB
29- from git .objects import Commit , TagObject , Blob , Tree
29+ from git .objects import Commit , TagObject
3030 from git .refs .reference import Reference
3131 from git .refs .tag import Tag
3232 from .base import Repo
@@ -227,12 +227,18 @@ def to_commit(obj: Object) -> "Commit":
227227 return obj
228228
229229
230- def rev_parse (repo : "Repo" , rev : str ) -> Union ["Commit" , "Tag" , "Tree" , "Blob" ]:
231- """
230+ def rev_parse (repo : "Repo" , rev : str ) -> AnyGitObject :
231+ """Parse a revision string. Like ``git rev-parse``.
232+
232233 :return:
233- `~git.objects.base.Object` at the given revision, either
234- `~git.objects.commit.Commit`, `~git.refs.tag.Tag`, `~git.objects.tree.Tree` or
235- `~git.objects.blob.Blob`.
234+ `~git.objects.base.Object` at the given revision.
235+
236+ This may be any type of git object:
237+
238+ * :class:`Commit <git.objects.commit.Commit>`
239+ * :class:`TagObject <git.objects.tag.TagObject>`
240+ * :class:`Tree <git.objects.tree.Tree>`
241+ * :class:`Blob <git.objects.blob.Blob>`
236242
237243 :param rev:
238244 ``git rev-parse``-compatible revision specification as string. Please see
@@ -253,7 +259,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
253259 raise NotImplementedError ("commit by message search (regex)" )
254260 # END handle search
255261
256- obj : Union [ Old_commit_ish , "Reference" , None ] = None
262+ obj : Optional [ AnyGitObject ] = None
257263 ref = None
258264 output_type = "commit"
259265 start = 0
@@ -275,12 +281,10 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
275281 if token == "@" :
276282 ref = cast ("Reference" , name_to_object (repo , rev [:start ], return_ref = True ))
277283 else :
278- obj = cast ( Old_commit_ish , name_to_object (repo , rev [:start ]) )
284+ obj = name_to_object (repo , rev [:start ])
279285 # END handle token
280286 # END handle refname
281287 else :
282- assert obj is not None
283-
284288 if ref is not None :
285289 obj = cast ("Commit" , ref .commit )
286290 # END handle ref
@@ -300,7 +304,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
300304 pass # Default.
301305 elif output_type == "tree" :
302306 try :
303- obj = cast (Old_commit_ish , obj )
307+ obj = cast (AnyGitObject , obj )
304308 obj = to_commit (obj ).tree
305309 except (AttributeError , ValueError ):
306310 pass # Error raised later.
@@ -373,7 +377,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
373377 parsed_to = start
374378 # Handle hierarchy walk.
375379 try :
376- obj = cast (Old_commit_ish , obj )
380+ obj = cast (AnyGitObject , obj )
377381 if token == "~" :
378382 obj = to_commit (obj )
379383 for _ in range (num ):
@@ -402,7 +406,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
402406
403407 # Still no obj? It's probably a simple name.
404408 if obj is None :
405- obj = cast ( Old_commit_ish , name_to_object (repo , rev ) )
409+ obj = name_to_object (repo , rev )
406410 parsed_to = lr
407411 # END handle simple name
408412
0 commit comments