Skip to content

Commit da2989c

Browse files
committed
Stub out our universal base class
Experiment with Generics for the refresh method. And see how far that gets us.
1 parent 8c5b3e9 commit da2989c

File tree

3 files changed

+159
-33
lines changed

3 files changed

+159
-33
lines changed

github3/github.pyi

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,90 @@
11
"""This module contains the type stubs for github3.github."""
2-
from typing import Dict, List, Union, Optional
2+
from typing import (
3+
Any,
4+
Dict,
5+
List,
6+
Optional,
7+
TypeVar,
8+
Union,
9+
)
310

411
from . import auths
512
from . import events
6-
from .gists import gist
7-
from .issues import issue
13+
from . import models
814
from . import orgs
9-
from .repos import repo
1015
from . import structs
1116
from . import users
17+
from .gists import gist
18+
from .issues import issue
19+
from .repos import repo
1220

13-
AuthorizationsIterator = structs.GitHubIterator[auths.Authorization]
14-
EventsIterator = structs.GitHubIterator[events.Event]
15-
OrganizationsIterator = structs.GitHubIterator[orgs.ShortOrganization]
16-
RepositoriesIterator = structs.GitHubIterator[repo.ShortRepository]
17-
UsersIterator = structs.GitHubIterator[users.ShortUser]
21+
T = TypeVar('T', bound='GitHub')
1822

1923

20-
class GitHub:
24+
class GitHub(models.GitHubCore):
2125
def __init__(
22-
self,
26+
self: T,
2327
username: str='',
2428
password: str='',
2529
token: str='',
2630
) -> None:
2731
...
2832

2933
def add_email_addresses(
30-
self,
34+
self: T,
3135
addresses: List[str]=[],
3236
) -> List[users.Email]:
3337
...
3438

3539
def all_events(
36-
self,
40+
self: T,
3741
number: int=-1,
3842
etag: Optional[str]=None,
39-
) -> EventsIterator:
43+
) -> structs.GitHubIterator[events.Event]:
4044
...
4145

4246
def all_organizations(
43-
self,
47+
self: T,
4448
number: int=-1,
4549
since: Optional[int]=None,
4650
etag: Optional[str]=None,
4751
per_page: Optional[int]=None,
48-
) -> OrganizationsIterator:
52+
) -> structs.GitHubIterator[orgs.ShortOrganization]:
4953
...
5054

5155
def all_repositories(
52-
self,
56+
self: T,
5357
number: int=-1,
5458
since: Optional[int]=None,
5559
etag: Optional[str]=None,
5660
per_page: Optional[int]=None,
57-
) -> RepositoriesIterator:
61+
) -> structs.GitHubIterator[repo.ShortRepository]:
5862
...
5963

6064
def all_users(
61-
self,
65+
self: T,
6266
number: int=-1,
6367
etag: Optional[str]=None,
6468
per_page: Optional[int]=None,
6569
since: Optional[int]=None,
66-
) -> UsersIterator:
70+
) -> structs.GitHubIterator[users.ShortUser]:
6771
...
6872

6973
def authorization(
70-
self,
74+
self: T,
7175
id_num: Union[int, str],
72-
) -> AuthorizationsIterator:
76+
) -> auths.Authorization:
7377
...
7478

7579
def authorizations(
76-
self,
80+
self: T,
7781
number: int=-1,
7882
etag: Optional[str]=None,
79-
) -> AuthorizationsIterator:
83+
) -> structs.GitHubIterator[auths.Authorization]:
8084
...
8185

8286
def authorize(
83-
self,
87+
self: T,
8488
username: str,
8589
password: str,
8690
scopes: Optional[List[str]]=None,
@@ -92,21 +96,21 @@ class GitHub:
9296
...
9397

9498
def check_authorization(
95-
self,
99+
self: T,
96100
access_token: str,
97101
) -> bool:
98102
...
99103

100104
def create_gist(
101-
self,
105+
self: T,
102106
description: str,
103107
files: Dict[str, Dict[str, str]],
104108
public: bool=True,
105109
) -> gist.Gist:
106110
...
107111

108112
def create_issue(
109-
self,
113+
self: T,
110114
owner: str,
111115
repository: str,
112116
title: str,
@@ -117,3 +121,57 @@ class GitHub:
117121
assignees: Optional[List[str]]=None,
118122
) -> issue.ShortIssue:
119123
...
124+
125+
def create_key(
126+
self: T,
127+
title: str,
128+
key: str,
129+
read_only: bool=False,
130+
) -> users.Key:
131+
...
132+
133+
def create_repository(
134+
self: T,
135+
name: str,
136+
description: str='',
137+
homepage: str='',
138+
private: bool=False,
139+
has_issues: bool=True,
140+
has_wiki: bool=True,
141+
auto_init: bool=False,
142+
gitignore_template: str='',
143+
) -> repo.Repository:
144+
...
145+
146+
def delete_email_addresses(
147+
self: T,
148+
addresses: List[str],
149+
) -> bool:
150+
...
151+
152+
def emails(
153+
self: T,
154+
number: int=-1,
155+
etag: Optional[str]=None,
156+
) -> structs.GitHubIterator[users.Email]:
157+
...
158+
159+
def emojis(self: T) -> Dict[str, str]:
160+
...
161+
162+
def feeds(self: T) -> Dict[str, Any]:
163+
...
164+
165+
def follow(
166+
self: T,
167+
username: str
168+
) -> bool:
169+
...
170+
171+
def followed_by(
172+
self: T,
173+
username: str,
174+
number: int=-1,
175+
etag: Optional[str]=None,
176+
) -> structs.GitHubIterator[users.ShortUser]:
177+
...

github3/models.pyi

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from typing import (
2+
Any,
3+
Dict,
4+
Generic,
5+
Optional,
6+
Type,
7+
TypeVar,
8+
Union,
9+
)
10+
11+
from . import session
12+
13+
14+
Subclass = TypeVar('Subclass', bound='GitHubCore')
15+
Core = TypeVar('Core', bound='GitHubCore')
16+
Sessionish = Union[session.GitHubSession, GitHubCore]
17+
FullClass = TypeVar('FullClass')
18+
19+
class GitHubCore(Generic[FullClass]):
20+
_refresh_to = None # type: Optional[FullClass]
21+
22+
def __init__(
23+
self: Core,
24+
json: Dict[str, Any],
25+
session: Sessionish,
26+
) -> None:
27+
...
28+
29+
def as_dict(self: Core) -> Dict[str, Any]:
30+
...
31+
32+
def as_json(self: Core) -> str:
33+
...
34+
35+
@classmethod
36+
def from_dict(
37+
cls: Type[Subclass],
38+
json_dict: Dict[str, Any],
39+
session: Sessionish,
40+
) -> Subclass:
41+
...
42+
43+
@classmethod
44+
def from_json(
45+
cls: Type[Subclass],
46+
json_dict: str,
47+
session: Sessionish,
48+
) -> Subclass:
49+
...
50+
51+
@property
52+
def ratelimit_remaining(self: Core) -> int:
53+
...
54+
55+
def refresh(
56+
self: Core,
57+
conditional: bool,
58+
) -> Union[Core, FullClass]:
59+
...
60+
61+
def new_session(self: Core) -> session.GitHubSession:
62+
...

github3/users.pyi

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
class _User:
2-
...
3-
1+
from . import models
42

5-
class ShortUser(_User):
3+
class _User(models.GitHubCore):
64
...
75

86

97
class User(_User):
108
...
119

1210

11+
class ShortUser(_User):
12+
_refresh_to = User
13+
14+
1315
class Contributor(_User):
1416
...
1517

@@ -18,5 +20,9 @@ class AuthenticatedUser(User):
1820
...
1921

2022

21-
class Email:
23+
class Email(models.GitHubCore):
24+
...
25+
26+
27+
class Key(models.GitHubCore):
2228
...

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy