Skip to content

Commit bd4e1b0

Browse files
committed
wip: Fix types
1 parent 4a2e8d2 commit bd4e1b0

18 files changed

+151
-116
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ repos:
2727
hooks:
2828
- id: pyupgrade
2929
args: [--py37-plus]
30-
#- repo: https://github.com/pre-commit/mirrors-mypy
31-
# rev: v0.910
32-
# hooks:
33-
# - id: mypy
34-
# exclude: ^(docs/|tests/)
30+
- repo: https://github.com/pre-commit/mirrors-mypy
31+
rev: v1.5.1
32+
hooks:
33+
- id: mypy
34+
additional_dependencies:
35+
- types-python-dateutil
36+
- types-requests
37+
exclude: ^(docs/|tests/)
3538
- repo: https://github.com/jorisroovers/gitlint
3639
rev: v0.19.1
3740
hooks:

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ exclude = '''
8686
)/
8787
)
8888
'''
89+
90+
[tool.mypy]
91+
files = ["."]
92+
exclude = [
93+
"^docs/",
94+
"^tests/",
95+
]

src/github3/checks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _update_attributes(self, pull):
4545
def _repr(self):
4646
return f"<CheckPullRequest [#{self.number}]>"
4747

48-
def to_pull(self):
48+
def to_pull(self, conditional: bool = False):
4949
"""Retrieve a full PullRequest object for this CheckPullRequest.
5050
5151
:returns:
@@ -119,7 +119,7 @@ def _repr(self):
119119
self.name, str(self.owner["login"])
120120
)
121121

122-
def to_app(self):
122+
def to_app(self, conditional: bool = False) -> models.GitHubCore:
123123
"""Retrieve a full App object for this CheckApp.
124124
125125
:returns:

src/github3/events.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _update_attributes(self, user):
4141
self.login = user["login"]
4242
self._api = self.url = user["url"]
4343

44-
def to_user(self):
44+
def to_user(self, conditional: bool = False) -> models.GitHubCore:
4545
"""Retrieve a full User object for this EventUser.
4646
4747
:returns:
@@ -93,7 +93,7 @@ def _update_attributes(self, org):
9393
self.login = org["login"]
9494
self._api = self.url = org["url"]
9595

96-
def to_org(self):
96+
def to_org(self, conditional: bool = False) -> models.GitHubCore:
9797
"""Retrieve a full Organization object for this EventOrganization.
9898
9999
:returns:
@@ -148,7 +148,7 @@ def _update_attributes(self, pull):
148148
self.locked = pull["locked"]
149149
self._api = self.url = pull["url"]
150150

151-
def to_pull(self):
151+
def to_pull(self, conditional: bool = False) -> models.GitHubCore:
152152
"""Retrieve a full PullRequest object for this EventPullRequest.
153153
154154
:returns:
@@ -258,7 +258,9 @@ def _update_attributes(self, comment):
258258
self.updated_at = self._strptime(comment["updated_at"])
259259
self.user = users.ShortUser(comment["user"], self)
260260

261-
def to_review_comment(self):
261+
def to_review_comment(
262+
self, conditional: bool = False
263+
) -> models.GitHubCore:
262264
"""Retrieve a full ReviewComment object for this EventReviewComment.
263265
264266
:returns:
@@ -269,7 +271,7 @@ def to_review_comment(self):
269271
from . import pulls
270272

271273
comment = self._json(self._get(self._api), 200)
272-
return pulls.ReviewComment(comment, self)
274+
return pulls.ReviewComment(comment, self.session)
273275

274276
refresh = to_review_comment
275277

@@ -285,7 +287,7 @@ def _update_attributes(self, issue):
285287
self.locked = issue["locked"]
286288
self._api = self.url = issue["url"]
287289

288-
def to_issue(self):
290+
def to_issue(self, conditional: bool = False) -> models.GitHubCore:
289291
"""Retrieve a full Issue object for this EventIssue."""
290292
from . import issues
291293

@@ -352,7 +354,9 @@ def _update_attributes(self, comment):
352354
self.updated_at = self._strptime(comment["updated_at"])
353355
self.user = users.ShortUser(comment["user"], self)
354356

355-
def to_issue_comment(self):
357+
def to_issue_comment(
358+
self, conditional: bool = False
359+
) -> models.GitHubCore:
356360
"""Retrieve the full IssueComment object for this comment.
357361
358362
:returns:

src/github3/gists/gist.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""This module contains the Gist, ShortGist, and GistFork objects."""
2+
import typing as t
23
from json import dumps
34

45
from . import comment
@@ -31,7 +32,9 @@ class _Gist(models.GitHubCore):
3132
"""
3233

3334
class_name = "_Gist"
34-
_file_class = gistfile.ShortGistFile
35+
_file_class: t.Type[
36+
t.Union[gistfile.ShortGistFile, gistfile.GistFile]
37+
] = gistfile.ShortGistFile
3538

3639
def _update_attributes(self, gist):
3740
self.comments_count = gist["comments"]
@@ -265,7 +268,7 @@ def _update_attributes(self, fork):
265268
def _repr(self):
266269
return f"<GistFork [{self.id}]>"
267270

268-
def to_gist(self):
271+
def to_gist(self, conditional: bool = False) -> models.GitHubCore:
269272
"""Retrieve the full Gist representation of this fork.
270273
271274
:returns:

src/github3/gists/history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class GistHistory(models.GitHubCore):
5151
def _update_attributes(self, history) -> None:
5252
self.url = self._api = history["url"]
5353
self.version = history["version"]
54-
self.user = users.ShortUser(history["user"], self)
54+
self.user = users.ShortUser(history["user"], self.session)
5555
self.change_status = history["change_status"]
5656
self.additions = self.change_status.get("additions")
5757
self.deletions = self.change_status.get("deletions")

src/github3/git.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -297,42 +297,6 @@ def _repr(self):
297297
return f"<Tag [{self.tag}]>"
298298

299299

300-
class CommitTree(models.GitHubCore):
301-
"""This object represents the abbreviated tree data in a commit.
302-
303-
The API returns different representations of different objects. When
304-
representing a :class:`~github3.git.ShortCommit` or
305-
:class:`~github3.git.Commit`, the API returns an abbreviated
306-
representation of a git tree.
307-
308-
This object has the following attributes:
309-
310-
.. attribute:: sha
311-
312-
The SHA1 of this tree in the git repository.
313-
"""
314-
315-
def _update_attributes(self, tree):
316-
self._api = tree["url"]
317-
self.sha = tree["sha"]
318-
319-
def _repr(self):
320-
return f"<CommitTree [{self.sha}]>"
321-
322-
def to_tree(self):
323-
"""Retrieve a full Tree object for this CommitTree.
324-
325-
:returns:
326-
The full git data about this tree
327-
:rtype:
328-
:class:`~github3.git.Tree`
329-
"""
330-
json = self._json(self._get(self._api), 200)
331-
return self._instance_or_null(Tree, json)
332-
333-
refresh = to_tree
334-
335-
336300
class Tree(models.GitHubCore):
337301
"""This represents a tree object from a git repository.
338302
@@ -382,6 +346,42 @@ def recurse(self):
382346
return self._instance_or_null(Tree, json)
383347

384348

349+
class CommitTree(models.GitHubCore):
350+
"""This object represents the abbreviated tree data in a commit.
351+
352+
The API returns different representations of different objects. When
353+
representing a :class:`~github3.git.ShortCommit` or
354+
:class:`~github3.git.Commit`, the API returns an abbreviated
355+
representation of a git tree.
356+
357+
This object has the following attributes:
358+
359+
.. attribute:: sha
360+
361+
The SHA1 of this tree in the git repository.
362+
"""
363+
364+
def _update_attributes(self, tree):
365+
self._api = tree["url"]
366+
self.sha = tree["sha"]
367+
368+
def _repr(self):
369+
return f"<CommitTree [{self.sha}]>"
370+
371+
def to_tree(self, conditional: bool = False) -> models.GitHubCore:
372+
"""Retrieve a full Tree object for this CommitTree.
373+
374+
:returns:
375+
The full git data about this tree
376+
:rtype:
377+
:class:`~github3.git.Tree`
378+
"""
379+
json = self._json(self._get(self._api), 200)
380+
return self._instance_or_null(Tree, json)
381+
382+
refresh = to_tree
383+
384+
385385
class Hash(models.GitHubCore):
386386
"""This is used to represent the elements of a tree.
387387

src/github3/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
import typing as t
55

6-
import uritemplate
6+
import uritemplate # type: ignore
77

88
from . import apps
99
from . import auths
@@ -544,7 +544,7 @@ def authorize(
544544
@requires_auth
545545
def blocked_users(
546546
self, number: int = -1, etag: t.Optional[str] = None
547-
) -> t.Generator[users.ShortUser, None, None]:
547+
) -> t.Iterator[users.ShortUser]:
548548
"""Iterate over the users blocked by this organization.
549549
550550
.. versionadded:: 2.1.0

src/github3/issues/issue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Module containing the Issue logic."""
22
from json import dumps
33

4-
from uritemplate import URITemplate
4+
from uritemplate import URITemplate # type: ignore
55

66
from . import comment
77
from . import event

src/github3/models.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
LOG = logging.getLogger(__package__)
1717

1818

19-
T = t.TypeVar("T")
20-
21-
2219
class GitHubCore:
2320
"""The base object for all objects that require a session.
2421
@@ -28,7 +25,7 @@ class GitHubCore:
2825
"""
2926

3027
_ratelimit_resource = "core"
31-
_refresh_to: t.Optional["GitHubCore"] = None
28+
_refresh_to: t.Optional[t.Type["GitHubCore"]] = None
3229

3330
def __init__(self, json, session: session.GitHubSession):
3431
"""Initialize our basic object.
@@ -240,26 +237,28 @@ def _api(self):
240237
value += f"?{self._uri.query}"
241238
return value
242239

243-
@staticmethod
244-
def _uri_parse(uri):
245-
return requests.compat.urlparse(uri)
246-
247240
@_api.setter
248241
def _api(self, uri):
249242
if uri:
250243
self._uri = self._uri_parse(uri)
251244
self.url = uri
252245

246+
@staticmethod
247+
def _uri_parse(uri):
248+
return requests.compat.urlparse(uri)
249+
253250
def _iter(
254251
self,
255252
count: int,
256253
url: str,
257-
cls: t.Type[T],
258-
params: t.Optional[t.Mapping[str, t.Optional[str]]] = None,
254+
cls: t.Type["GitHubCore"],
255+
params: t.Optional[
256+
t.MutableMapping[str, t.Union[str, int, None]]
257+
] = None,
259258
etag: t.Optional[str] = None,
260259
headers: t.Optional[t.Mapping[str, str]] = None,
261260
list_key: t.Optional[str] = None,
262-
) -> "structs.GitHubIterator[T]":
261+
) -> "structs.GitHubIterator":
263262
"""Generic iterator for this project.
264263
265264
:param int count: How many items to return.
@@ -276,7 +275,7 @@ def _iter(
276275
from .structs import GitHubIterator
277276

278277
return GitHubIterator(
279-
count, url, cls, self, params, etag, headers, list_key
278+
count, url, cls, self.session, params, etag, headers, list_key
280279
)
281280

282281
@property
@@ -329,7 +328,7 @@ def refresh(self, conditional: bool = False) -> "GitHubCore":
329328
self._json_data = json
330329
self._update_attributes(json)
331330
else:
332-
return self._refresh_to(json, self)
331+
return self._refresh_to(json, self.session)
333332
return self
334333

335334
def new_session(self):

src/github3/orgs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import typing as t
33
from json import dumps
44

5-
from uritemplate import URITemplate
5+
from uritemplate import URITemplate # type: ignore
66

77
from . import models
88
from . import users
@@ -211,7 +211,7 @@ def permissions_for(
211211
headers = {"Accept": "application/vnd.github.v3.repository+json"}
212212
url = self._build_url("repos", repository, base_url=self._api)
213213
json = self._json(self._get(url, headers=headers), 200)
214-
return ShortRepositoryWithPermissions(json, self)
214+
return ShortRepositoryWithPermissions(json, self.session)
215215

216216
@requires_auth
217217
def repositories(self, number=-1, etag=None):
@@ -491,7 +491,7 @@ def add_repository(self, repository, team_id): # FIXME(jlk): add perms
491491
@requires_auth
492492
def blocked_users(
493493
self, number: int = -1, etag: t.Optional[str] = None
494-
) -> t.Generator[users.ShortUser, None, None]:
494+
) -> t.Iterator[users.ShortUser]:
495495
"""Iterate over the users blocked by this organization.
496496
497497
.. versionadded:: 2.1.0
@@ -749,7 +749,7 @@ def create_team(
749749
getattr(r, "full_name", r) for r in (repo_names or [])
750750
],
751751
"maintainers": [
752-
getattr(m, "login", m) for m in (maintainers or [])
752+
str(getattr(m, "login", m)) for m in (maintainers or [])
753753
],
754754
"permission": permission,
755755
"privacy": privacy,

src/github3/pulls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""This module contains all the classes relating to pull requests."""
22
from json import dumps
33

4-
from uritemplate import URITemplate
4+
from uritemplate import URITemplate # type: ignore
55

66
from . import models
77
from . import users

src/github3/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)