Skip to content

Commit f23da45

Browse files
authored
Add capability for global laziness (PyGithub#2746)
Adds capability to configure global laziness Github objects through `Github(lazy=…)` and `GithubIntegration(lazy=…)`. Does not add any laziness capability to any particular Github object. Reworks `CompletableGithubObject` to support following features: - It fetches initial complete JSON, which allows to replace ```python headers, data = self.__requester.requestJsonAndCheck("GET", f"/users/{login}") return github.NamedUser.NamedUser(self.__requester, headers, data, completed=True) ``` with ```python return github.NamedUser.NamedUser(self.__requester, url=f"/users/{login}") ``` Which allows that returned `CompletableGithubObject` to be lazy. Removes any code related to `CompletableGithubObject` from `NonCompletableGithubObject`. - Which requires replacing (for `NonCompletableGithubObject` only) ```python return github.Branch.Branch(self._requester, headers, data, completed=True) ``` for `NonCompletableGithubObject` with ```python return github.Branch.Branch(self._requester, headers, data) ``` Fixes PyGithub#2334
1 parent 19ddb9f commit f23da45

23 files changed

+283
-149
lines changed

github/ApplicationOAuth.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ def __init__(
6262
requester: Requester,
6363
headers: dict[str, Any],
6464
attributes: Any,
65-
completed: bool,
6665
) -> None:
6766
# this object requires a request without authentication
6867
requester = requester.withAuth(auth=None)
69-
super().__init__(requester, headers, attributes, completed)
68+
super().__init__(requester, headers, attributes)
7069

7170
def __repr__(self) -> str:
7271
return self.get__repr__({"client_id": self._client_id.value})
@@ -146,7 +145,6 @@ def get_access_token(self, code: str, state: str | None = None) -> AccessToken:
146145
requester=self._requester,
147146
headers=headers,
148147
attributes=data,
149-
completed=False,
150148
)
151149

152150
def get_app_user_auth(self, token: AccessToken) -> AppUserAuth:
@@ -187,7 +185,6 @@ def refresh_access_token(self, refresh_token: str) -> AccessToken:
187185
requester=self._requester,
188186
headers=headers,
189187
attributes=data,
190-
completed=False,
191188
)
192189

193190
@staticmethod

github/Auth.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@ def withRequester(self, requester: Requester) -> "AppUserAuth":
451451
"client_id": self._client_id,
452452
"client_secret": self._client_secret,
453453
},
454-
completed=False,
455454
)
456455

457456
return self

github/Commit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def create_status(
204204
f"{self._parentUrl(self._parentUrl(self.url))}/statuses/{self.sha}",
205205
input=post_parameters,
206206
)
207-
return github.CommitStatus.CommitStatus(self._requester, headers, data, completed=True)
207+
return github.CommitStatus.CommitStatus(self._requester, headers, data)
208208

209209
def get_comments(self) -> PaginatedList[CommitComment]:
210210
"""
@@ -233,7 +233,7 @@ def get_combined_status(self) -> CommitCombinedStatus:
233233
:calls: `GET /repos/{owner}/{repo}/commits/{ref}/status/ <http://docs.github.com/en/rest/reference/repos#statuses>`_
234234
"""
235235
headers, data = self._requester.requestJsonAndCheck("GET", f"{self.url}/status")
236-
return github.CommitCombinedStatus.CommitCombinedStatus(self._requester, headers, data, completed=True)
236+
return github.CommitCombinedStatus.CommitCombinedStatus(self._requester, headers, data)
237237

238238
def get_pulls(self) -> PaginatedList[PullRequest]:
239239
"""

github/Enterprise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(
6161
enterprise: str,
6262
):
6363
enterprise = urllib.parse.quote(enterprise)
64-
super().__init__(requester, {}, {"enterprise": enterprise, "url": f"/enterprises/{enterprise}"}, True)
64+
super().__init__(requester, {}, {"enterprise": enterprise, "url": f"/enterprises/{enterprise}"})
6565

6666
def _initAttributes(self) -> None:
6767
self._enterprise: Attribute[str] = NotSet

github/GithubIntegration.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def __init__(
8383
jwt_issued_at: int = Consts.DEFAULT_JWT_ISSUED_AT,
8484
jwt_algorithm: str = Consts.DEFAULT_JWT_ALGORITHM,
8585
auth: AppAuth | None = None,
86+
# v3: set lazy = True as the default
87+
lazy: bool = False,
8688
) -> None:
8789
"""
8890
:param integration_id: int deprecated, use auth=github.Auth.AppAuth(...) instead
@@ -100,6 +102,8 @@ def __init__(
100102
:param jwt_issued_at: int deprecated, use auth=github.Auth.AppAuth(...) instead
101103
:param jwt_algorithm: string deprecated, use auth=github.Auth.AppAuth(...) instead
102104
:param auth: authentication method
105+
:param lazy: completable objects created from this instance are lazy,
106+
as well as completable objects created from those, and so on
103107
"""
104108
if integration_id is not None:
105109
assert isinstance(integration_id, (int, str)), integration_id
@@ -117,6 +121,7 @@ def __init__(
117121
assert isinstance(jwt_expiry, int), jwt_expiry
118122
assert Consts.MIN_JWT_EXPIRY <= jwt_expiry <= Consts.MAX_JWT_EXPIRY, jwt_expiry
119123
assert isinstance(jwt_issued_at, int)
124+
assert isinstance(lazy, bool), lazy
120125

121126
self.base_url = base_url
122127

@@ -157,8 +162,22 @@ def __init__(
157162
pool_size=pool_size,
158163
seconds_between_requests=seconds_between_requests,
159164
seconds_between_writes=seconds_between_writes,
165+
lazy=lazy,
160166
)
161167

168+
def withLazy(self, lazy: bool) -> GithubIntegration:
169+
"""
170+
Create a GithubIntegration instance with identical configuration but the given lazy setting.
171+
172+
:param lazy: completable objects created from this instance are lazy, as well as completable objects created
173+
from those, and so on
174+
:return: new Github instance
175+
176+
"""
177+
kwargs = self.__requester.kwargs
178+
kwargs.update(lazy=lazy)
179+
return GithubIntegration(**kwargs)
180+
162181
def close(self) -> None:
163182
"""Close connections to the server. Alternatively, use the
164183
GithubIntegration object as a context manager:
@@ -211,7 +230,6 @@ def _get_installed_app(self, url: str) -> Installation:
211230
requester=self.__requester,
212231
headers=headers,
213232
attributes=response,
214-
completed=True,
215233
)
216234

217235
@deprecated.deprecated(
@@ -248,7 +266,6 @@ def get_access_token(
248266
requester=self.__requester,
249267
headers=headers,
250268
attributes=response,
251-
completed=True,
252269
)
253270

254271
@deprecated.deprecated("Use get_repo_installation")

0 commit comments

Comments
 (0)