Skip to content

Commit dbbbb8c

Browse files
committed
linted and type checked
1 parent 2988a0f commit dbbbb8c

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

asyncPyGithub/User.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
from .base import req
1515

1616
from requests import get, patch
17+
from typing import Final, Literal
1718

1819

1920
UserQueryReturnable = tuple[
2021
int, PrivateUser | SimpleUser | list[SimpleUser] | ErrorMessage
2122
]
2223

24+
USER_ENDPOINT: Final[Literal["/user"]] = "/user"
25+
USERS_ENDPOINT: Final[Literal["/users"]] = "/users"
26+
2327

2428
class GitHubUserPortal(GitHubPortal):
2529

@@ -41,7 +45,7 @@ async def authenticate(
4145
ErrorMessage(
4246
code=res.status_code,
4347
message=res.json().get("message", "Unknown error"),
44-
endpoint="/user",
48+
endpoint=USER_ENDPOINT,
4549
),
4650
)
4751

@@ -60,15 +64,16 @@ async def update(
6064
This function uses the `/user` endpoint to update the user's information.
6165
Available: [https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#update-the-authenticated-user](https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#update-the-authenticated-user)
6266
"""
67+
endpoint = USER_ENDPOINT
6368
try:
64-
res = await req(fn=patch, url="/user", json=changes)
69+
res = await req(fn=patch, url=endpoint, json=changes)
6570
if res.status_code != 200:
6671
return (
6772
res.status_code,
6873
ErrorMessage(
6974
code=res.status_code,
7075
message=res.json().get("message", "Unknown error"),
71-
endpoint="/user",
76+
endpoint=endpoint,
7277
),
7378
)
7479

@@ -81,30 +86,31 @@ async def update(
8186
updated_self = PrivateUser(**updated_json)
8287

8388
except Exception as e:
84-
return (500, ErrorMessage(code=500, message=str(e), endpoint="/user"))
89+
return (500, ErrorMessage(code=500, message=str(e), endpoint=endpoint))
8590

8691
return (res.status_code, updated_self)
8792

8893
@needs_authentication
8994
async def get_by_id(
9095
cls: "GitHubUserPortal", uid: int
9196
) -> tuple[int, PrivateUser | ErrorMessage]:
97+
endpoint = f"{USER_ENDPOINT}/{uid}"
9298
try:
93-
res = await req(fn=get, url=f"/user/{uid}")
99+
res = await req(fn=get, url=endpoint)
94100
if res.status_code != 200:
95101
return (
96102
res.status_code,
97103
ErrorMessage(
98104
code=res.status_code,
99105
message=res.json().get("message", "Unknown error"),
100-
endpoint=f"/user/{uid}",
106+
endpoint=endpoint,
101107
),
102108
)
103109

104110
except Exception as e:
105111
return (
106112
500,
107-
ErrorMessage(code=500, message=str(e), endpoint=f"/user/{uid}"),
113+
ErrorMessage(code=500, message=str(e), endpoint=endpoint),
108114
)
109115

110116
return (res.status_code, PrivateUser(**res.json()))
@@ -113,23 +119,24 @@ async def get_by_id(
113119
async def get_by_username(
114120
cls: "GitHubUserPortal", username: str
115121
) -> tuple[int, PrivateUser | ErrorMessage]:
122+
endpoint = f"{USERS_ENDPOINT}/{username}"
116123
try:
117-
res = await req(fn=get, url=f"/users/{username}")
124+
res = await req(fn=get, url=endpoint)
118125
if res.status_code != 200:
119126
return (
120127
res.status_code,
121128
ErrorMessage(
122129
code=res.status_code,
123130
message=res.json().get("message", "Unknown error"),
124-
endpoint=f"/users/{username}",
131+
endpoint=endpoint,
125132
),
126133
)
127134

128135
return (res.status_code, PrivateUser(**res.json()))
129136
except Exception as e:
130137
return (
131138
500,
132-
ErrorMessage(code=500, message=str(e), endpoint=f"/users/{username}"),
139+
ErrorMessage(code=500, message=str(e), endpoint=endpoint),
133140
)
134141

135142
@needs_authentication
@@ -138,20 +145,25 @@ async def all(
138145
) -> tuple[int, list[SimpleUser] | ErrorMessage]:
139146
try:
140147
res = await req(
141-
fn=get, url="/users", params={"since": since, "per_page": per_page}
148+
fn=get,
149+
url=USERS_ENDPOINT,
150+
params={"since": since, "per_page": per_page},
142151
)
143152
if res.status_code != 200:
144153
return (
145154
res.status_code,
146155
ErrorMessage(
147156
code=res.status_code,
148157
message=res.json().get("message", "Unknown error"),
149-
endpoint="/users",
158+
endpoint=USERS_ENDPOINT,
150159
),
151160
)
152161

153162
except Exception as e:
154-
return (500, ErrorMessage(code=500, message=str(e), endpoint="/users"))
163+
return (
164+
500,
165+
ErrorMessage(code=500, message=str(e), endpoint=USERS_ENDPOINT),
166+
)
155167

156168
return (res.status_code, [SimpleUser(**user) for user in res.json()])
157169

@@ -164,16 +176,16 @@ async def get_hovercard(
164176
This function uses the `/users/{username}/hovercard` endpoint to get the hovercard information.
165177
Available: [https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#view-a-user-hovercard](https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#view-a-user-hovercard)
166178
"""
167-
179+
endpoint = f"{USERS_ENDPOINT}/{username}/hovercard"
168180
try:
169-
res = await req(fn=get, url=f"/users/{username}/hovercard")
181+
res = await req(fn=get, url=endpoint)
170182
if res.status_code != 200:
171183
return (
172184
res.status_code,
173185
ErrorMessage(
174186
code=res.status_code,
175187
message=res.json().get("message", "Unknown error"),
176-
endpoint=f"/users/{username}/hovercard",
188+
endpoint=endpoint,
177189
),
178190
)
179191

@@ -187,7 +199,5 @@ async def get_hovercard(
187199
except Exception as e:
188200
return (
189201
500,
190-
ErrorMessage(
191-
code=500, message=str(e), endpoint=f"/users/{username}/hovercard"
192-
),
202+
ErrorMessage(code=500, message=str(e), endpoint=endpoint),
193203
)

asyncPyGithub/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
__all__ = (
2626
"UserQueryReturnable",
2727
"ErrorMessage",
28-
"UserJSON",
2928
"SimpleUserJSON",
3029
"SimpleUser",
3130
"UserPlanJSON",

asyncPyGithub/_types/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
"UserPlanJSON",
3333
"PrivateUserJSON",
3434
"PrivateUser",
35-
"UserJSONSchema",
3635
"SimpleUserJSON",
3736
"SimpleUser",
38-
"HoverCardSchema",
3937
"HoverCardJSON",
4038
"HoverCard",
4139
"HoverCardContextJSON",

asyncPyGithub/_types/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
from typing_extensions import Self, Callable, Any
23
from collections.abc import Coroutine as CoroutineType
34
from pydantic import EmailStr, HttpUrl, PastDatetime
@@ -62,7 +63,7 @@ def authenticated(self: Self) -> bool:
6263

6364
def needs_authentication(
6465
function: Callable[..., Any],
65-
) -> classmethod[GitHubPortal, [Any], CoroutineType[Any, Any, Any]]:
66+
) -> classmethod[Any, ..., CoroutineType[Any, Any, Any]]:
6667
async def wrapper(cls: type[GitHubPortal], *args, **kwargs) -> Any:
6768
# Special case: allow authenticate() to run without being authenticated
6869
if function.__name__ == "authenticate":

0 commit comments

Comments
 (0)