8000 Merge pull request #1081 from sigmavirus24/teams-deprecation · datashaman/github3.py@58f7060 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58f7060

Browse files
authored
Merge pull request sigmavirus24#1081 from sigmavirus24/teams-deprecation
Teams deprecation
2 parents 38b6f88 + c55b4b9 commit 58f7060

17 files changed

+130
-81
lines changed

docs/source/release-notes/3.2.0.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
3.2.0: 2022-xx-xx
1+
3.2.0: 2022-03-02
22
-----------------
33

4-
Dependency Change
5-
`````````````````
6-
4+
Bug Fixes
5+
`````````
76

8-
Features Added
9-
``````````````
7+
- Migrate to GitHub's supported Teams endpoints for select methods that were
8+
relying on the deprecated endpoints. See also gh-1080_
109

1110

12-
Bug Fix 6D40 es
13-
`````````
11+
.. _gh-1080:
12+
https://github.com/sigmavirus24/github3.py/issues/1080

src/github3/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
__author_email__ = "graffatcolmingov@gmail.com"
66
__license__ = "Modified BSD"
77
__copyright__ = "Copyright 2012-2022 Ian Stapleton Cordasco"
8-
__version__ = "3.1.2"
8+
__version__ = "3.2.0"
99
__version_info__ = tuple(
1010
int(i) for i in __version__.split(".") if i.isdigit()
1111
)

src/github3/models.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
"""This module provides the basic models used in github3.py."""
22
import json as jsonlib
33
import logging
4+
import typing as t
45

56
import dateutil.parser
67
import requests.compat
78

89
from . import exceptions
910
from . import session
1011

12+
13+
if t.TYPE_CHECKING:
14+
from . import structs
15+
1116
LOG = logging.getLogger(__package__)
1217

1318

19+
T = t.TypeVar("T")
20+
21+
1422
class GitHubCore:
1523
"""The base object for all objects that require a session.
1624
@@ -20,9 +28,9 @@ class GitHubCore:
2028
"""
2129

2230
_ratelimit_resource = "core"
23-
_refresh_to = None
31+
_refresh_to: t.Optional["GitHubCore"] = None
2432

25-
def __init__(self, json, session):
33+
def __init__(self, json, session: session.GitHubSession):
2634
"""Initialize our basic object.
2735
2836
Pretty much every object will pass in decoded JSON and a Session.
@@ -244,14 +252,14 @@ def _api(self, uri):
244252

245253
def _iter(
246254
self,
247-
count,
248-
url,
249-
cls,
250-
params=None,
251-
etag=None,
252-
headers=None,
253-
list_key=None,
254-
):
255+
count: int,
256+
url: str,
257+
cls: t.Type[T],
258+
params: t.Optional[t.Mapping[str, t.Optional[str]]] = None,
259+
etag: t.Optional[str] = None,
260+
headers: t.Optional[t.Mapping[str, str]] = None,
261+
list_key: t.Optional[str] = None,
262+
) -> "structs.GitHubIterator[T]":
255263
"""Generic iterator for this project.
256264
257265
:param int count: How many items to return.

src/github3/orgs.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def edit(
144144
bool
145145
"""
146146
if name:
147-
data = {"name": name}
147+
data: t.Dict[str, t.Union[str, int]] = {"name": name}
148148
if permission:
149149
data["permission"] = permission
150150
if parent_team_id is not None:
@@ -478,7 +478,14 @@ def add_repository(self, repository, team_id): # FIXME(jlk): add perms
478478
if int(team_id) < 0:
479479
return False
480480

481-
url = self._build_url("teams", str(team_id), "repos", str(repository))
481+
url = self._build_url(
482+
"organizations",
483+
str(self.id),
484+
"team",
485+
str(team_id),
486+
"repos",
487+
str(repository),
488+
)
482489
return self._boolean(self._put(url), 204, 404)
483490

484491
@requires_auth
@@ -736,10 +743,14 @@ def create_team(
736743
:rtype:
737744
:class:`~github3.orgs.Team`
738745
"""
739-
data = {
746+
data: t.Dict[str, t.Union[t.List[str], str, int]] = {
740747
"name": name,
741-
"repo_names": [getattr(r, "full_name", r) for r in repo_names],
742-
"maintainers": [getattr(m, "login", m) for m in maintainers],
748+
"repo_names": [
749+
getattr(r, "full_name", r) for r in (repo_names or [])
750+
],
751+
"maintainers": [
752+
getattr(m, "login", m) for m in (maintainers or [])
753+
],
743754
"permission": permission,
744755
"privacy": privacy,
745756
}
@@ -1149,7 +1160,7 @@ def teams(self, number=-1, etag=None):
11491160
return self._iter(int(number), url, ShortTeam, etag=etag)
11501161

11511162
@requires_auth
1152-
def publicize_member(self, username):
1163+
def publicize_member(self, username: str) -> bool:
11531164
"""Make ``username``'s membership in this organization public.
11541165
11551166
:param str username:
@@ -1163,7 +1174,7 @@ def publicize_member(self, username):
11631174
return self._boolean(self._put(url), 204, 404)
11641175

11651176
@requires_auth
1166-
def remove_member(self, username):
1177+
def remove_member(self, username: str) -> bool:
11671178
"""Remove the user named ``username`` from this organization.
11681179
11691180
.. note::
@@ -1182,7 +1193,11 @@ def remove_member(self, username):
11821193
return self._boolean(self._delete(url), 204, 404)
11831194

11841195
@requires_auth
1185-
def remove_repository(self, repository, team_id):
1196+
def remove_repository(
1197+
self,
1198+
repository: t.Union[Repository, ShortRepository, str],
1199+
team_id: int,
1200+
):
11861201
"""Remove ``repository`` from the team with ``team_id``.
11871202
11881203
:param str repository:
@@ -1196,13 +1211,18 @@ def remove_repository(self, repository, team_id):
11961211
"""
11971212
if int(team_id) > 0:
11981213
url = self._build_url(
1199-
"teams", str(team_id), "repos", str(repository)
1214+
"organizations",
1215+
str(self.id),
1216+
"team",
1217+
str(team_id),
1218+
"repos",
1219+
str(repository),
12001220
)
12011221
return self._boolean(self._delete(url), 204, 404)
12021222
return False
12031223

12041224
@requires_auth
1205-
def team(self, team_id):
1225+
def team(self, team_id: int) -> t.Optional[Team]:
12061226
"""Return the team specified by ``team_id``.
12071227
12081228
:param int team_id:
@@ -1214,12 +1234,14 @@ def team(self, team_id):
12141234
"""
12151235
json = None
12161236
if int(team_id) > 0:
1217-
url = self._build_url("teams", str(team_id))
1237+
url = self._build_url(
1238+
"organizations", str(self.id), "team", str(team_id)
1239+
)
12181240
json = self._json(self._get(url), 200)
12191241
return self._instance_or_null(Team, json)
12201242

12211243
@requires_auth
1222-
def team_by_name(self, team_slug):
1244+
def team_by_name(self, team_slug: str) -> t.Optional[Team]:
12231245
"""Return the team specified by ``team_slug``.
12241246
12251247
:param str team_slug:

src/github3/structs.py

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,75 @@
1-
import collections.abc as abc_collections
1+
import collections.abc
22
import functools
3+
import typing as t
34

45
from requests.compat import urlencode
56
from requests.compat import urlparse
67

78
from . import exceptions
89
from . import models
910

11+
if t.TYPE_CHECKING:
12+
import requests.models
1013

11-
class GitHubIterator(models.GitHubCore, abc_collections.Iterator):
14+
from . import session
15+
16+
17+
T = t.TypeVar("T")
18+
19+
20+
class GitHubIterator(models.GitHubCore, collections.abc.Iterator):
1221
"""The :class:`GitHubIterator` class powers all of the iter_* methods."""
1322

1423
def __init__(
1524
self,
16-
count,
17-
url,
18-
cls,
19-
session,
20-
params=None,
21-
etag=None,
22-
headers=None,
23-
list_key=None,
24-
):
25+
count: int,
26+
url: str,
27+
cls: t.Type[T],
28+
session: "session.GitHubSession",
29+
params: t.Optional[t.Mapping[str, t.Optional[str]]] = None,
30+
etag: t.Optional[str] = None,
31+
headers: t.Optional[t.Mapping[str, str]] = None,
32+
list_key: t.Optional[str] = None,
33+
) -> None:
2534
models.GitHubCore.__init__(self, {}, session)
2635
#: Original number of items requested
27-
self.original = count
36+
self.original: t.Final[int] = count
2837
#: Number of items left in the iterator
29-
self.count = count
38+
self.count: int = count
3039
#: URL the class used to make it's first GET
31-
self.url = url
40+
self.url: str = url
3241
#: Last URL that was requested
33-
self.last_url = None
34-
self._api = self.url
42+
self.last_url: t.Optional[str] = None
43+
self._api: str = self.url
3544
#: Class for constructing an item to return
36-
self.cls = cls
45+
self.cls: t.Type[T] = cls
3746
#: Parameters of the query string
38-
self.params = params or {}
47+
self.params: t.Mapping[str, t.Optional[str]] = params or {}
3948
self._remove_none(self.params)
4049
# We do not set this from the parameter sent. We want this to
4150
# represent the ETag header returned by GitHub no matter what.
4251
# If this is not None, then it won't be set from the response and
4352
# that's not what we want.
4453
#: The ETag Header value returned by GitHub
45-
self.etag = None
54+
self.etag: t.Optional[str] = None
4655
#: Headers generated for the GET request
47-
self.headers = headers or {}
56+
self.headers: t.Dict[str, str] = dict(headers or {})
4857
#: The last response seen
49-
self.last_response = None
58+
self.last_response: "requests.models.Response" = None
5059
#: Last status code received
51-
self.last_status = 0
60+
self.last_status: int = 0
5261
#: Key to get the list of items in case a dict is returned
53-
self.list_key = list_key
62+
self.list_key: t.Final[t.Optional[str]] = list_key
5463

5564
if etag:
5665
self.headers.update({"If-None-Match": etag})
5766

58-
self.path = urlparse(self.url).path
67+
self.path: str = urlparse(self.url).path
5968

60-
def _repr(self):
69+
def _repr(self) -> str:
6170
return f"<GitHubIterator [{self.count}, {self.path}]>"
6271

63-
def __iter__(self):
72+
def __iter__(self) -> t.Generator[T, None, None]:
6473
self.last_url, params = self.url, self.params
6574
headers = self.headers
6675

@@ -127,23 +136,23 @@ def __iter__(self):
127136
rel_next = response.links.get("next", {})
128137
self.last_url = rel_next.get("url", "")
129138

130-
def __next__(self):
139+
def __next__(self) -> T:
131140
if not hasattr(self, "__i__"):
132141
self.__i__ = self.__iter__()
133142
return next(self.__i__)
134143

135-
def _get_json(self, response):
144+
def _get_json(self, response: "requests.models.Response"):
136145
return self._json(response, 200)
137146

138-
def refresh(self, conditional=False):
147+
def refresh(self, conditional: bool = False) -> "GitHubIterator":
139148
self.count = self.original
140-
if conditional:
149+
if conditional and self.etag:
141150
self.headers["If-None-Match"] = self.etag
142151
self.etag = None
143152
self.__i__ = self.__iter__()
144153
return self
145154

146-
def next(self):
155+
def next(self) -> T:
147156
return self.__next__()
148157

149158

@@ -160,13 +169,20 @@ class SearchIterator(GitHubIterator):
160169
_ratelimit_resource = "search"
161170

162171
def __init__(
163-
self, count, url, cls, session, params=None, etag=None, headers=None
172+
self,
173+
count: int,
174+
url: str,
175+
cls: t.Type[T],
176+
session: "session.GitHubSession",
177+
params: t.Optional[t.Mapping[str, t.Optional[str]]] = None,
178+
etag: t.Optional[str] = None,
179+
headers: t.Optional[t.Mapping[str, str]] = None,
164180
):
165181
super().__init__(count, url, cls, session, params, etag, headers)
166182
#: Total count returned by GitHub
167-
self.total_count = 0
183+
self.total_count: int = 0
168184
#: Items array returned in the last request
169-
self.items = []
185+
self.items: t.List[t.Mapping[str, t.Any]] = []
170186

171187
def _repr(self):
172188
return "<SearchIterator [{}, {}?{}]>".format(

0 commit comments

Comments
 (0)
0