1- import collections .abc as abc_collections
1+ import collections .abc
22import functools
3+ import typing as t
34
45from requests .compat import urlencode
56from requests .compat import urlparse
67
78from . import exceptions
89from . import models
910
11+ if t .TYPE_CHECKING :
12+ import requests .models
1013
11- class GitHubIterator (models .GitHubCore , abc_collections .Iterator ):
14+ from . import session
15+
16+
17+ T = t .TypeVar ("T" )
18+
19+
20+ class GitHubIterator (models .GitHubCore , collections .abc .Iterator ):
1221 """The :class:`GitHubIterator` class powers all of the iter_* methods."""
1322
1423 def __init__ (
1524 self ,
16- count ,
17- url ,
18- cls ,
19- session ,
20- params = None ,
21- etag = None ,
22- headers = None ,
23- list_key = None ,
24- ):
25+ count : int ,
26+ url : str ,
27+ cls : t . Type [ T ] ,
28+ session : "session.GitHubSession" ,
29+ params : t . Optional [ t . Mapping [ str , t . Optional [ str ]]] = None ,
30+ etag : t . Optional [ str ] = None ,
31+ headers : t . Optional [ t . Mapping [ str , str ]] = None ,
32+ list_key : t . Optional [ str ] = None ,
33+ ) -> None :
2534 models .GitHubCore .__init__ (self , {}, session )
2635 #: Original number of items requested
27- self .original = count
36+ self .original : t . Final [ int ] = count
2837 #: Number of items left in the iterator
29- self .count = count
38+ self .count : int = count
3039 #: URL the class used to make it's first GET
31- self .url = url
40+ self .url : str = url
3241 #: Last URL that was requested
33- self .last_url = None
34- self ._api = self .url
42+ self .last_url : t . Optional [ str ] = None
43+ self ._api : str = self .url
3544 #: Class for constructing an item to return
36- self .cls = cls
45+ self .cls : t . Type [ T ] = cls
3746 #: Parameters of the query string
38- self .params = params or {}
47+ self .params : t . Mapping [ str , t . Optional [ str ]] = params or {}
3948 self ._remove_none (self .params )
4049 # We do not set this from the parameter sent. We want this to
4150 # represent the ETag header returned by GitHub no matter what.
4251 # If this is not None, then it won't be set from the response and
4352 # that's not what we want.
4453 #: The ETag Header value returned by GitHub
45- self .etag = None
54+ self .etag : t . Optional [ str ] = None
4655 #: Headers generated for the GET request
47- self .headers = headers or {}
56+ self .headers : t . Dict [ str , str ] = dict ( headers or {})
4857 #: The last response seen
49- self .last_response = None
58+ self .last_response : "requests.models.Response" = None
5059 #: Last status code received
51- self .last_status = 0
60+ self .last_status : int = 0
5261 #: Key to get the list of items in case a dict is returned
53- self .list_key = list_key
62+ self .list_key : t . Final [ t . Optional [ str ]] = list_key
5463
5564 if etag :
5665 self .headers .update ({"If-None-Match" : etag })
5766
58- self .path = urlparse (self .url ).path
67+ self .path : str = urlparse (self .url ).path
5968
60- def _repr (self ):
69+ def _repr (self ) -> str :
6170 return f"<GitHubIterator [{ self .count } , { self .path } ]>"
6271
63- def __iter__ (self ):
72+ def __iter__ (self ) -> t . Generator [ T , None , None ] :
6473 self .last_url , params = self .url , self .params
6574 headers = self .headers
6675
@@ -127,23 +136,23 @@ def __iter__(self):
127136 rel_next = response .links .get ("next" , {})
128137 self .last_url = rel_next .get ("url" , "" )
129138
130- def __next__ (self ):
139+ def __next__ (self ) -> T :
131140 if not hasattr (self , "__i__" ):
132141 self .__i__ = self .__iter__ ()
133142 return next (self .__i__ )
134143
135- def _get_json (self , response ):
144+ def _get_json (self , response : "requests.models.Response" ):
136145 return self ._json (response , 200 )
137146
138- def refresh (self , conditional = False ):
147+ def refresh (self , conditional : bool = False ) -> "GitHubIterator" :
139148 self .count = self .original
140- if conditional :
149+ if conditional and self . etag :
141150 self .headers ["If-None-Match" ] = self .etag
142151 self .etag = None
143152 self .__i__ = self .__iter__ ()
144153 return self
145154
146- def next (self ):
155+ def next (self ) -> T :
147156 return self .__next__ ()
148157
149158
@@ -160,13 +169,20 @@ class SearchIterator(GitHubIterator):
160169 _ratelimit_resource = "search"
161170
162171 def __init__ (
163- self , count , url , cls , session , params = None , etag = None , headers = None
172+ self ,
173+ count : int ,
174+ url : str ,
175+ cls : t .Type [T ],
176+ session : "session.GitHubSession" ,
177+ params : t .Optional [t .Mapping [str , t .Optional [str ]]] = None ,
178+ etag : t .Optional [str ] = None ,
179+ headers : t .Optional [t .Mapping [str , str ]] = None ,
164180 ):
165181 super ().__init__ (count , url , cls , session , params , etag , headers )
166182 #: Total count returned by GitHub
167- self .total_count = 0
183+ self .total_count : int = 0
168184 #: Items array returned in the last request
169- self .items = []
185+ self .items : t . List [ t . Mapping [ str , t . Any ]] = []
170186
171187 def _repr (self ):
172188 return "<SearchIterator [{}, {}?{}]>" .format (
0 commit comments