2
2
LazyMixin ,
3
3
IterableObj ,
4
4
)
5
- from .symbolic import SymbolicReference
5
+ from .symbolic import SymbolicReference , T_References
6
+
7
+
8
+ # typing ------------------------------------------------------------------
9
+
10
+ from typing import Any , Callable , Iterator , List , Match , Optional , Tuple , Type , TypeVar , Union , TYPE_CHECKING # NOQA
11
+ from git .types import Commit_ish , PathLike , TBD , Literal , TypeGuard , _T # NOQA
12
+
13
+ if TYPE_CHECKING :
14
+ from git .repo import Repo
15
+
16
+ # ------------------------------------------------------------------------------
6
17
7
18
8
19
__all__ = ["Reference" ]
9
20
10
21
#{ Utilities
11
22
12
23
13
- def require_remote_ref_path (func ) :
24
+ def require_remote_ref_path (func : Callable [..., _T ]) -> Callable [..., _T ] :
14
25
"""A decorator raising a TypeError if we are not a valid remote, based on the path"""
15
26
16
- def wrapper (self , * args ) :
27
+ def wrapper (self : T_References , * args : Any ) -> _T :
17
28
if not self .is_remote ():
18
29
raise ValueError ("ref path does not point to a remote reference: %s" % self .path )
19
30
return func (self , * args )
@@ -32,7 +43,7 @@ class Reference(SymbolicReference, LazyMixin, IterableObj):
32
43
_resolve_ref_on_create = True
33
44
_common_path_default = "refs"
34
45
35
- def __init__ (self , repo , path , check_path = True ):
46
+ def __init__ (self , repo : 'Repo' , path : PathLike , check_path : bool = True ) -> None :
36
47
"""Initialize this instance
37
48
:param repo: Our parent repository
38
49
@@ -41,16 +52,17 @@ def __init__(self, repo, path, check_path=True):
41
52
refs/heads/master
42
53
:param check_path: if False, you can provide any path. Otherwise the path must start with the
43
54
default path prefix of this type."""
44
- if check_path and not path .startswith (self ._common_path_default + '/' ):
45
- raise ValueError ("Cannot instantiate %r from path %s" % (self .__class__ .__name__ , path ))
55
+ if check_path and not str (path ).startswith (self ._common_path_default + '/' ):
56
+ raise ValueError (f"Cannot instantiate { self .__class__ .__name__ !r} from path { path } " )
57
+ self .path : str # SymbolicReference converts to string atm
46
58
super (Reference , self ).__init__ (repo , path )
47
59
48
- def __str__ (self ):
60
+ def __str__ (self ) -> str :
49
61
return self .name
50
62
51
63
#{ Interface
52
64
53
- def set_object (self , object , logmsg = None ): # @ReservedAssignment
65
+ def set_object (self , object : Commit_ish , logmsg : Union [ str , None ] = None ) -> 'Reference' : # @ReservedAssignment
54
66
"""Special version which checks if the head-log needs an update as well
55
67
:return: self"""
56
68
oldbinsha = None
@@ -84,7 +96,7 @@ def set_object(self, object, logmsg=None): # @ReservedAssignment
84
96
# NOTE: Don't have to overwrite properties as the will only work without a the log
85
97
86
98
@property
87
- def name (self ):
99
+ def name (self ) -> str :
88
100
""":return: (shortest) Name of this reference - it may contain path components"""
89
101
# first two path tokens are can be removed as they are
90
102
# refs/heads or refs/tags or refs/remotes
@@ -94,7 +106,8 @@ def name(self):
94
106
return '/' .join (tokens [2 :])
95
107
96
108
@classmethod
97
- def iter_items (cls , repo , common_path = None ):
109
+ def iter_items (cls : Type [T_References ], repo : 'Repo' , common_path : Union [PathLike , None ] = None ,
110
+ * args : Any , ** kwargs : Any ) -> Iterator [T_References ]:
98
111
"""Equivalent to SymbolicReference.iter_items, but will return non-detached
99
112
references as well."""
100
113
return cls ._iter_items (repo , common_path )
@@ -105,7 +118,7 @@ def iter_items(cls, repo, common_path=None):
105
118
106
119
@property # type: ignore ## mypy cannot deal with properties with an extra decorator (2021-04-21)
107
120
@require_remote_ref_path
108
- def remote_name (self ):
121
+ def remote_name (self ) -> str :
109
122
"""
110
123
:return:
111
124
Name of the remote we are a reference of, such as 'origin' for a reference
@@ -116,7 +129,7 @@ def remote_name(self):
116
129
117
130
@property # type: ignore ## mypy cannot deal with properties with an extra decorator (2021-04-21)
118
131
@require_remote_ref_path
119
- def remote_head (self ):
132
+ def remote_head (self ) -> str :
120
133
""":return: Name of the remote head itself, i.e. master.
121
134
:note: The returned name is usually not qualified enough to uniquely identify
122
135
a branch"""
0 commit comments