Skip to content

Commit d375a35

Browse files
committed
wip: Add more types
1 parent 103e73b commit d375a35

File tree

4 files changed

+100
-51
lines changed

4 files changed

+100
-51
lines changed

src/github3/github.py

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ class GitHub(models.GitHubCore):
6060
"""
6161

6262
def __init__(
63-
self, username="", password="", token="", session=None, api_version=""
64-
):
63+
self,
64+
username: str = "",
65+
password: str = "",
66+
token: str = "",
67+
session: t.Optional[session.GitHubSession] = None,
68+
api_version: str = "",
69+
) -> None:
6570
"""Create a new GitHub instance to talk to the API.
6671
6772
:param str api_version:
@@ -79,13 +84,15 @@ def __init__(
7984
elif username and password:
8085
self.login(username, password)
8186

82-
def _repr(self):
87+
def _repr(self) -> str:
8388
if self.session.auth:
8489
return f"<GitHub [{self.session.auth!r}]>"
8590
return f"<Anonymous GitHub at 0x{id(self):x}>"
8691

8792
@requires_auth
88-
def activate_membership(self, organization):
93+
def activate_membership(
94+
self, organization: t.Union[str, orgs.Organization]
95+
) -> t.Optional[orgs.Membership]:
8996
"""Activate the membership to an organization.
9097
9198
:param organization:
@@ -109,7 +116,9 @@ def activate_membership(self, organization):
109116
return self._instance_or_null(orgs.Membership, _json)
110117

111118
@requires_auth
112-
def add_email_addresses(self, addresses=[]):
119+
def add_email_addresses(
120+
self, addresses: t.Optional[t.List[str]] = None
121+
) -> t.Sequence[users.Email]:
113122
"""Add the addresses to the authenticated user's account.
114123
115124
:param list addresses:
@@ -123,9 +132,15 @@ def add_email_addresses(self, addresses=[]):
123132
if addresses:
124133
url = self._build_url("user", "emails")
125134
json = self._json(self._post(url, data=addresses), 201)
126-
return [users.Email(email, self) for email in json] if json else []
135+
return (
136+
[users.Email(email, self.session) for email in json]
137+
if json
138+
else []
139+
)
127140

128-
def all_events(self, number=-1, etag=None):
141+
def all_events(
142+
self, number: int = -1, etag: t.Optional[str] = None
143+
) -> t.Iterable[events.Event]:
129144
"""Iterate over public events.
130145
131146
:param int number:
@@ -142,8 +157,12 @@ def all_events(self, number=-1, etag=None):
142157
return self._iter(int(number), url, events.Event, etag=etag)
143158

144159
def all_organizations(
145-
self, number=-1, since=None, etag=None, per_page=None
146-
):
160+
self,
161+
number: int = -1,
162+
since: t.Optional[int] = None,
163+
etag: t.Optional[str] = None,
164+
per_page: t.Optional[int] = None,
165+
) -> t.Iterable[orgs.ShortOrganization]:
147166
"""Iterate over every organization in the order they were created.
148167
149168
:param int number:
@@ -171,8 +190,12 @@ def all_organizations(
171190
)
172191

173192
def all_repositories(
174-
self, number=-1, since=None, etag=None, per_page=None
175-
):
193+
self,
194+
number: int = -1,
195+
since: t.Optional[int] = None,
196+
etag: t.Optional[str] = None,
197+
per_page: t.Optional[int] = None,
198+
) -> t.Iterable[repo.Repository]:
176199
"""Iterate over every repository in the order they were created.
177200
178201
:param int number:
@@ -199,7 +222,13 @@ def all_repositories(
199222
etag=etag,
200223
)
201224

202-
def all_users(self, number=-1, etag=None, per_page=None, since=None):
225+
def all_users(
226+
self,
227+
number: int = -1,
228+
etag: t.Optional[str] = None,
229+
per_page: t.Optional[int] = None,
230+
since: t.Optional[int] = None,
231+
) -> t.Iterable[users.ShortUser]:
203232
"""Iterate over every user in the order they signed up for GitHub.
204233
205234
.. versionchanged:: 1.0.0
@@ -229,7 +258,7 @@ def all_users(self, number=-1, etag=None, per_page=None, since=None):
229258
params={"per_page": per_page, "since": since},
230259
)
231260

232-
def app(self, app_slug):
261+
def app(self, app_slug: t.Any) -> t.Optional[apps.App]:
233262
"""Retrieve information about a specific app using its "slug".
234263
235264
.. versionadded:: 1.2.0
@@ -256,7 +285,9 @@ def app(self, app_slug):
256285
return self._instance_or_null(apps.App, json)
257286

258287
@decorators.requires_app_bearer_auth
259-
def app_installation(self, installation_id):
288+
def app_installation(
289+
self, installation_id: int
290+
) -> t.Optional[apps.Installation]:
260291
"""Retrieve a specific App installation by its ID.
261292
262293
.. versionadded: 1.2.0
@@ -283,7 +314,9 @@ def app_installation(self, installation_id):
283314
return self._instance_or_null(apps.Installation, json)
284315

285316
@decorators.requires_app_bearer_auth
286-
def app_installations(self, number=-1):
317+
def app_installations(
318+
self, number: int = -1
319+
) -> t.Iterable[apps.Installation]:
287320
"""Retrieve the list of installations for the authenticated app.
288321
289322
.. versionadded:: 1.2.0
@@ -310,7 +343,9 @@ def app_installations(self, number=-1):
310343
)
311344

312345
@decorators.requires_app_bearer_auth
313-
def app_installation_for_organization(self, organization):
346+
def app_installation_for_organization(
347+
self, organization: str
348+
) -> t.Iterable[apps.Installation]:
314349
"""Retrieve an App installation for a specific organization.
315350
316351
.. versionadded:: 1.2.0
@@ -337,7 +372,9 @@ def app_installation_for_organization(self, organization):
337372
return self._instance_or_null(apps.Installation, json)
338373

339374
@decorators.requires_app_bearer_auth
340-
def app_installation_for_repository(self, owner, repository):
375+
def app_installation_for_repository(
376+
self, owner: str, repository: str
377+
) -> t.Optional[apps.Installation]:
341378
"""Retrieve an App installation for a specific repository.
342379
343380
.. versionadded:: 1.2.0
@@ -1283,7 +1320,7 @@ def issues_on(
12831320
assignee=None,
12841321
mentioned=None,
12851322
labels=None,
1286-
sort=None,
1323+
sort: str | None = None,
12871324
direction=None,
12881325
since=None,
12891326
number=-1,
@@ -2037,13 +2074,13 @@ def repositories(
20372074

20382075
def repositories_by(
20392076
self,
2040-
username,
2041-
type=None,
2042-
sort=None,
2043-
direction=None,
2044-
number=-1,
2045-
etag=None,
2046-
):
2077+
username: str,
2078+
type: t.Optional[str] = None,
2079+
sort: t.Optional[str] = None,
2080+
direction: t.Optional[str] = None,
2081+
number: int = -1,
2082+
etag: t.Optional[str] = None,
2083+
) -> t.Iterable[repo.ShortRepository]:
20472084
"""List public repositories for the specified ``username``.
20482085
20492086
.. versionadded:: 0.6
@@ -2073,7 +2110,7 @@ def repositories_by(
20732110
"""
20742111
url = self._build_url("users", username, "repos")
20752112

2076-
params = {}
2113+
params: t.MutableMapping[str, t.Union[int, str, None]] = {}
20772114
if type in ("all", "owner", "member"):
20782115
params.update(type=type)
20792116
if sort in ("created", "updated", "pushed", "full_name"):
@@ -2085,7 +2122,9 @@ def repositories_by(
20852122
int(number), url, repo.ShortRepository, params, etag
20862123
)
20872124

2088-
def repository(self, owner, repository):
2125+
def repository(
2126+
self, owner: str, repository: str
2127+
) -> t.Optional[repo.Repository]:
20892128
"""Retrieve the desired repository.
20902129
20912130
:param str owner:
@@ -2104,7 +2143,9 @@ def repository(self, owner, repository):
21042143
return self._instance_or_null(repo.Repository, json)
21052144

21062145
@requires_auth
2107-
def repository_invitations(self, number=-1, etag=None):
2146+
def repository_invitations(
2147+
self, number: int = -1, etag: t.Optional[str] = None
2148+
) -> t.Iterator[invitation.Invitation]:
21082149
"""Iterate over the repository invitations for the current user.
21092150
21102151
:param int number:

src/github3/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def _repr(self):
130130
return f"<github3-core at 0x{id(self):x}>"
131131

132132
@staticmethod
133-
def _remove_none(data):
133+
def _remove_none(data: t.Optional[t.MutableMapping[str, t.Any]]):
134134
if not data:
135135
return
136136
for k, v in list(data.items()):

src/github3/pulls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def _update_attributes(self, pull):
210210
self.created_at = self._strptime(pull["created_at"])
211211
self.diff_url = pull["diff_url"]
212212
self.head = Head(pull["head"], self)
213-
self.html_url = pull["html_url"]
213+
self.html_url: str = pull["html_url"]
214214
self.id = pull["id"]
215215
self.issue_url = pull["issue_url"]
216216
self.links = pull["_links"]
@@ -992,7 +992,7 @@ def _update_attributes(self, review):
992992
# PullReview.
993993
self.commit_id = review.get("commit_id", None)
994994
self.html_url = review["html_url"]
995-
self.user = users.ShortUser(review["user"], self)
995+
self.user = users.ShortUser(review["user"], self.session)
996996
self.state = review["state"]
997997
self.submitted_at = self._strptime(review.get("submitted_at"))
998998
self.pull_request_url = review["pull_request_url"]

src/github3/repos/repo.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import base64
88
import json as jsonlib
9+
import typing as t
910

1011
import uritemplate as urit # type: ignore
1112

@@ -91,7 +92,7 @@ def _update_attributes(self, repo):
9192
self.milestones_urlt = urit.URITemplate(repo["milestones_url"])
9293
self.name = repo["name"]
9394
self.notifications_urlt = urit.URITemplate(repo["notifications_url"])
94-
self.owner = users.ShortUser(repo["owner"], self)
95+
self.owner = users.ShortUser(repo["owner"], self.session)
9596
self.private = repo["private"]
9697
self.pulls_urlt = urit.URITemplate(repo["pulls_url"])
9798
self.releases_urlt = urit.URITemplate(repo["releases_url"])
@@ -103,13 +104,13 @@ def _update_attributes(self, repo):
103104
self.teams_url = repo["teams_url"]
104105
self.trees_urlt = urit.URITemplate(repo["trees_url"])
105106

106-
def _repr(self):
107+
def _repr(self) -> str:
107108
return f"<{self.class_name} [{self}]>"
108109

109-
def __str__(self):
110+
def __str__(self) -> str:
110111
return self.full_name
111112

112-
def _create_pull(self, data):
113+
def _create_pull(self, data: t.MutableMapping[str, t.Any] | None):
113114
self._remove_none(data)
114115
json = None
115116
if data:
@@ -543,7 +544,9 @@ def contributor_statistics(self, number=-1, etag=None):
543544
url = self._build_url("stats", "contributors", base_url=self._api)
544545
return self._iter(int(number), url, stats.ContributorStats, etag=etag)
545546

546-
def contributors(self, anon=False, number=-1, etag=None):
547+
def contributors(
548+
self, anon: bool = False, number: int = -1, etag: str | None = None
549+
) -> t.Iterable[users.Contributor]:
547550
"""Iterate over the contributors to this repository.
548551
549552
:param bool anon:
@@ -559,7 +562,7 @@ def contributors(self, anon=False, number=-1, etag=None):
559562
:class:`~github3.users.Contributor`
560563
"""
561564
url = self._build_url("contributors", base_url=self._api)
562-
params = {}
565+
params: t.MutableMapping[str, int | str | None] = {}
563566
if anon:
564567
params = {"anon": "true"}
565568
return self._iter(int(number), url, users.Contributor, params, etag)
@@ -940,16 +943,21 @@ def create_file(
940943
self._remove_none(data)
941944
json = self._json(self._put(url, data=jsonlib.dumps(data)), 201)
942945
if json and "content" in json and "commit" in json:
943-
json["content"] = contents.Contents(json["content"], self)
944-
json["commit"] = git.Commit(json["commit"], self)
946+
json["content"] = contents.Contents(
947+
json["content"], self.session
948+
)
949+
json["commit"] = git.Commit(json["commit"], self.session)
945950
return json
946951

947952
@decorators.requires_auth
948-
def create_fork(self, organization=None):
953+
def create_fork(
954+
self, organization: str | None = None
955+
) -> t.Optional["Repository"]:
949956
"""Create a fork of this repository.
950957
951958
:param str organization:
952-
(required), login for organization to create the fork under
959+
(optional), login for organization to create the fork under. If
960+
omitted the fork will be a personal fork.
953961
:returns:
954962
the fork of this repository
955963
:rtype:
@@ -991,7 +999,7 @@ def create_hook(self, name, config, events=["push"], active=True):
991999
"active": active,
9921000
}
9931001
json = self._json(self._post(url, data=data), 201)
994-
return hook.Hook(json, self) if json else None
1002+
return hook.Hook(json, self.session) if json else None
9951003

9961004
@decorators.requires_auth
9971005
def create_issue(
@@ -2422,14 +2430,14 @@ def pull_request(self, number):
24222430

24232431
def pull_requests(
24242432
self,
2425-
state=None,
2426-
head=None,
2427-
base=None,
2428-
sort="created",
2429-
direction="desc",
2430-
number=-1,
2431-
etag=None,
2432-
):
2433+
state: str | None = None,
2434+
head: str | None = None,
2435+
base: str | None = None,
2436+
sort: str = "created",
2437+
direction: str = "desc",
2438+
number: int = -1,
2439+
etag: str | None = None,
2440+
) -> t.Iterable["pulls.ShortPullRequest"]:
24332441
"""List pull requests on repository.
24342442
24352443
.. versionchanged:: 0.9.0
@@ -2466,7 +2474,7 @@ def pull_requests(
24662474
:class:`~github3.pulls.ShortPullRequest`
24672475
"""
24682476
url = self._build_url("pulls", base_url=self._api)
2469-
params = {}
2477+
params: t.MutableMapping[str, int | str | None] = {}
24702478

24712479
if state:
24722480
state = state.lower()

0 commit comments

Comments
 (0)