Skip to content

Commit f8487f7

Browse files
authored
Merge pull request sigmavirus24#774 from omgjlk/send-the-session-771
Ensure a session is passed through to GitHubCore
2 parents 6e110c5 + c8e256c commit f8487f7

22 files changed

+59
-51
lines changed

github3/events.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ def _update_attributes(self, event):
119119
event = copy.deepcopy(event)
120120

121121
#: :class:`User <github3.users.User>` object representing the actor.
122-
self.actor = self._class_attribute(event, 'actor', EventUser)
122+
self.actor = self._class_attribute(event, 'actor', EventUser, self)
123123
#: datetime object representing when the event was created.
124124
self.created_at = self._strptime_attribute(event, 'created_at')
125125

126126
#: Unique id of the event
127127
self.id = self._get_attribute(event, 'id')
128128

129129
#: List all possible types of Events
130-
self.org = self._class_attribute(event, 'org', EventOrganization)
130+
self.org = self._class_attribute(event, 'org', EventOrganization, self)
131131

132132
#: Event type https://developer.github.com/v3/activity/events/types/
133133
self.type = self._get_attribute(event, 'type')

github3/gists/gist.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class GistFork(GitHubCore):
314314
def _update_attributes(self, fork):
315315
self.created_at = self._strptime(fork['created_at'])
316316
self.id = fork['id']
317-
self.owner = users.ShortUser(fork['user'])
317+
self.owner = users.ShortUser(fork['user'], self)
318318
self.updated_at = self._strptime(fork['updated_at'])
319319
self.url = self._api = fork['url']
320320

github3/git.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _update_attributes(self, ref):
108108
self.ref = self._get_attribute(ref, 'ref')
109109

110110
#: :class:`GitObject <GitObject>` the reference points to
111-
self.object = self._class_attribute(ref, 'object', GitObject)
111+
self.object = self._class_attribute(ref, 'object', GitObject, self)
112112

113113
def _repr(self):
114114
return '<Reference [{0}]>'.format(self.ref)
@@ -173,7 +173,7 @@ def _update_attributes(self, tag):
173173
self.tagger = self._get_attribute(tag, 'tagger')
174174

175175
#: :class:`GitObject <GitObject>` for the tag
176-
self.object = self._class_attribute(tag, 'object', GitObject)
176+
self.object = self._class_attribute(tag, 'object', GitObject, self)
177177

178178
def _repr(self):
179179
return '<Tag [{0}]>'.format(self.tag)
@@ -193,7 +193,7 @@ def _update_attributes(self, tree):
193193
#: list of :class:`Hash <Hash>` objects
194194
self.tree = self._get_attribute(tree, 'tree', [])
195195
if self.tree:
196-
self.tree = [Hash(t) for t in self.tree]
196+
self.tree = [Hash(t, self) for t in self.tree]
197197

198198
def _repr(self):
199199
return '<Tree [{0}]>'.format(self.sha)

github3/github.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class GitHub(GitHubCore):
5757
"""
5858

5959
def __init__(self, username='', password='', token=''):
60-
super(GitHub, self).__init__({})
60+
super(GitHub, self).__init__({}, self.new_session())
6161
if token:
6262
self.login(username, token=token)
6363
elif username and password:
@@ -80,7 +80,7 @@ def add_email_addresses(self, addresses=[]):
8080
if addresses:
8181
url = self._build_url('user', 'emails')
8282
json = self._json(self._post(url, data=addresses), 201)
83-
return [users.Email(email) for email in json] if json else []
83+
return [users.Email(email, self) for email in json] if json else []
8484

8585
def all_events(self, number=-1, etag=None):
8686
"""Iterate over public events.
@@ -1795,7 +1795,7 @@ class GitHubStatus(GitHubCore):
17951795
return the JSON objects returned by the API.
17961796
"""
17971797
def __init__(self):
1798-
super(GitHubStatus, self).__init__({})
1798+
super(GitHubStatus, self).__init__({}, self.new_session())
17991799
self.session.base_url = 'https://status.github.com'
18001800

18011801
def _repr(self):

github3/issues/issue.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ def _update_attributes(self, issue):
3434
#: was assigned to.
3535
self.assignee = issue['assignee']
3636
if self.assignee:
37-
self.assignee = users.ShortUser(self.assignee)
37+
self.assignee = users.ShortUser(self.assignee, self)
3838
self.assignees = issue['assignees']
3939
if self.assignees:
4040
self.assignees = [
41-
users.ShortUser(assignee) for assignee in self.assignees
41+
users.ShortUser(assignee, self) for assignee in self.assignees
4242
]
4343

4444
#: Body (description) of the issue.
@@ -107,7 +107,7 @@ def _update_attributes(self, issue):
107107
self.updated_at = self._strptime_attribute(issue, 'updated_at')
108108

109109
#: :class:`User <github3.users.User>` who opened the issue.
110-
self.user = users.ShortUser(issue['user'])
110+
self.user = users.ShortUser(issue['user'], self)
111111

112112
def _repr(self):
113113
return '<Issue [{r[0]}/{r[1]} #{n}]>'.format(r=self.repository,
@@ -389,4 +389,4 @@ def _update_attributes(self, issue):
389389
#: :class:`User <github3.users.User>` who closed the issue.
390390
self.closed_by = issue['closed_by']
391391
if self.closed_by:
392-
self.closed_by = users.ShortUser(self.closed_by)
392+
self.closed_by = users.ShortUser(self.closed_by, self)

github3/models.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ class GitHubCore(object):
3333
"""
3434
_ratelimit_resource = 'core'
3535

36-
def __init__(self, json, session=None):
36+
def __init__(self, json, session):
3737
if hasattr(session, 'session'):
3838
# i.e. session is actually a GitHubCore instance
3939
session = session.session
40-
elif session is None:
41-
session = GitHubSession()
4240
self.session = session
4341

4442
# set a sane default
@@ -166,14 +164,14 @@ def __repr__(self):
166164
return repr_string
167165

168166
@classmethod
169-
def from_dict(cls, json_dict):
167+
def from_dict(cls, json_dict, session):
170168
"""Return an instance of this class formed from ``json_dict``."""
171-
return cls(json_dict)
169+
return cls(json_dict, session)
172170

173171
@classmethod
174-
def from_json(cls, json):
172+
def from_json(cls, json, session):
175173
"""Return an instance of this class formed from ``json``."""
176-
return cls(loads(json))
174+
return cls(loads(json), session)
177175

178176
def __eq__(self, other):
179177
return self._uniq == other._uniq
@@ -349,6 +347,10 @@ def refresh(self, conditional=False):
349347
self._update_attributes(json)
350348
return self
351349

350+
def new_session(self):
351+
"""Helper function to generate a new session"""
352+
return GitHubSession()
353+
352354

353355
class BaseComment(GitHubCore):
354356

github3/pulls.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class PullDestination(models.GitHubCore):
2424
http://developer.github.com/v3/pulls/#get-a-single-pull-request
2525
"""
2626

27-
def __init__(self, dest, direction):
28-
super(PullDestination, self).__init__(dest)
27+
def __init__(self, dest, direction, session=None):
28+
super(PullDestination, self).__init__(dest, session)
2929
from .repos.repo import ShortRepository
3030
#: Direction of the merge with respect to this destination
3131
self.direction = direction
@@ -36,7 +36,7 @@ def __init__(self, dest, direction):
3636
#: :class:`User <github3.users.User>` representing the owner
3737
self.user = None
3838
if dest.get('user'):
39-
self.user = users.ShortUser(dest.get('user'), None)
39+
self.user = users.ShortUser(dest.get('user'), self)
4040
#: SHA of the commit at the head
4141
self.sha = dest.get('sha')
4242
self._repo_name = ''
@@ -118,7 +118,7 @@ def _update_attributes(self, pull):
118118
self._api = pull['url']
119119

120120
#: Base of the merge
121-
self.base = PullDestination(pull['base'], 'Base')
121+
self.base = PullDestination(pull['base'], 'Base', self)
122122

123123
#: Body of the pull request message
124124
self.body = pull['body']
@@ -136,7 +136,7 @@ def _update_attributes(self, pull):
136136
self.created_at = self._strptime_attribute(pull, 'created_at')
137137

138138
#: The new head after the pull request
139-
self.head = PullDestination(pull['head'], 'Head')
139+
self.head = PullDestination(pull['head'], 'Head', self)
140140

141141
#: The unique id of the pull request
142142
self.id = self._get_attribute(pull, 'id')
@@ -175,7 +175,7 @@ def _update_attributes(self, pull):
175175

176176
#: :class:`User <github3.users.ShortUser>` object representing the
177177
#: creator of the pull request
178-
self.user = users.ShortUser(pull['user'])
178+
self.user = users.ShortUser(pull['user'], self)
179179

180180
# This is only present if the PR has been assigned.
181181
#: :class:`User <github3.users.ShortUser>` object representing the

github3/repos/comparison.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _update_attributes(self, compare):
6969
#: objects.
7070
self.commits = self._get_attribute(compare, 'commits', [])
7171
if self.commits:
72-
self.commits = [RepoCommit(com) for com in self.commits]
72+
self.commits = [RepoCommit(com, self) for com in self.commits]
7373

7474
#: List of dicts describing the files modified.
7575
self.files = self._get_attribute(compare, 'files', [])

github3/repos/pages.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def _update_attributes(self, build):
3737
from .. import users
3838
#: :class:`User <github3.users.User>` representing who pushed the
3939
#: commit
40-
self.pusher = self._class_attribute(build, 'pusher', users.ShortUser)
40+
self.pusher = self._class_attribute(build, 'pusher', users.ShortUser,
41+
self)
4142

4243
#: SHA of the commit that triggered the build
4344
self.commit = self._get_attribute(build, 'commit')

github3/repos/release.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _update_attributes(self, release):
6565

6666
#: :class:`User <github3.users.ShortUser>` object representing the
6767
#: creator of the release
68-
self.author = users.ShortUser(release['author'])
68+
self.author = users.ShortUser(release['author'], self)
6969

7070
#: URLs to various attributes
7171
for urltype in ['assets_url', 'html_url', 'tarball_url',

github3/repos/status.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _update_attributes(self, status):
3131

3232
#: :class:`User <github3.users.User>` who created the object
3333
self.creator = self._class_attribute(
34-
status, 'creator', users.ShortUser
34+
status, 'creator', users.ShortUser, self
3535
)
3636

3737
#: Short description of the Status
@@ -74,7 +74,7 @@ def _update_attributes(self, combined_status):
7474
#: List of :class:`Status <github3.repos.status.Status>`
7575
#: objects.
7676
statuses = self._get_attribute(combined_status, 'statuses', [])
77-
self.statuses = [Status(s) for s in statuses]
77+
self.statuses = [Status(s, self) for s in statuses]
7878

7979
from . import repo
8080
#: Repository the combined status belongs too.

github3/users.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def _update_attributes(self, user):
608608
self.total_private_repos = user['total_private_repos']
609609

610610
#: Which plan this user is on
611-
self.plan = Plan(user['plan'])
611+
self.plan = Plan(user['plan'], self)
612612

613613
#: Number of repo contributions. Only appears in ``repo.contributors``
614614
contributions = user.get('contributions')

tests/unit/helper.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def create_instance_of_described_class(self):
8585
instance = self.described_class(self.example_data,
8686
self.session)
8787
elif self.example_data and not self.session:
88-
instance = self.described_class(self.example_data)
88+
session = self.create_session_mock()
89+
instance = self.described_class(self.example_data, session)
8990

9091
else:
9192
instance = self.described_class()

tests/unit/test_events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_org(self):
3737
json = self.instance.as_dict().copy()
3838
org = get_org_example_data()
3939
json['org'] = org
40-
event = github3.events.Event(json)
40+
event = github3.events.Event(json, self.session)
4141
assert isinstance(event.org, github3.events.EventOrganization)
4242

4343

tests/unit/test_gists.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ class TestGistHistory(helper.UnitHelper):
200200
def test_equality(self):
201201
"""Show that two instances of a GistHistory are equal."""
202202
history = github3.gists.history.GistHistory(
203-
gist_history_example_data()
203+
gist_history_example_data(),
204+
self.session
204205
)
205206
assert self.instance == history
206207
history._uniq = 'foo'
@@ -222,7 +223,8 @@ class TestGistComment(helper.UnitHelper):
222223
def test_equality(self):
223224
"""Show that two instances of a GistComment are equal."""
224225
comment = github3.gists.comment.GistComment(
225-
gist_comment_example_data()
226+
gist_comment_example_data(),
227+
self.session
226228
)
227229
assert self.instance == comment
228230
comment._uniq = '1'

tests/unit/test_git.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class TestTree(UnitHelper):
2323

2424
def test_eq(self):
2525
"""Assert that two trees are equal."""
26-
tree = github3.git.Tree(get_example_data())
26+
tree = github3.git.Tree(get_example_data(), self.session)
2727
assert self.instance == tree
2828

2929
def test_ne(self):
3030
"""Assert that two trees are not equal."""
31-
tree = github3.git.Tree(get_example_data())
31+
tree = github3.git.Tree(get_example_data(), self.session)
3232
tree._json_data['truncated'] = True
3333
assert self.instance != tree
3434

tests/unit/test_issues_issue.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ def test_edit_no_parameters(self):
225225
def test_enterprise(self):
226226
"""Show that enterprise data can be instantiated as Issue."""
227227
json = helper.create_example_data_helper('issue_enterprise')()
228-
assert github3.issues.Issue(json)
228+
assert github3.issues.Issue(json, self.session)
229229

230230
def test_equality(self):
231231
"""Show that two instances of Issue are equal."""
232-
issue = github3.issues.Issue(get_issue_example_data())
232+
issue = github3.issues.Issue(get_issue_example_data(), self.session)
233233
assert self.instance == issue
234234

235235
issue._uniq = 1
@@ -247,7 +247,8 @@ def test_issue_137(self):
247247
GitHub sometimes returns `pull` as part of of the `html_url` for Issue
248248
requests.
249249
"""
250-
issue = Issue(helper.create_example_data_helper('issue_137')())
250+
issue = Issue(helper.create_example_data_helper('issue_137')(),
251+
self.session)
251252
self.assertEqual(
252253
issue.html_url,
253254
"https://github.com/sigmavirus24/github3.py/pull/1")
@@ -392,7 +393,7 @@ class TestLabel(helper.UnitHelper):
392393

393394
def test_equality(self):
394395
"""Show that two instances of Label are equal."""
395-
label = Label(get_issue_label_example_data())
396+
label = Label(get_issue_label_example_data(), self.session)
396397
assert self.instance == label
397398

398399
label._uniq = ('https://https//api.github.com/repos/sigmavirus24/'
@@ -444,7 +445,8 @@ def test_repr(self):
444445
def test_equality(self):
445446
"""Show that two instances of IssueEvent are equal."""
446447
issue_event = github3.issues.event.IssueEvent(
447-
get_issue_event_example_data()
448+
get_issue_event_example_data(),
449+
self.session
448450
)
449451

450452
assert self.instance == issue_event

tests/unit/test_issues_milestone.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ def test_none_creator(self):
5050
"""Show that creator is None when json attribute is set to None."""
5151
json = self.instance.as_dict().copy()
5252
json['creator'] = None
53-
milestone = github3.issues.milestone.Milestone(json)
53+
milestone = github3.issues.milestone.Milestone(json, self.session)
5454
assert milestone.creator is None
5555

5656
def test_due_on(self):
5757
"""Show that due on attribute is a datetime object."""
5858
json = self.instance.as_dict().copy()
5959
json['due_on'] = '2012-12-31T23:59:59Z'
60-
milestone = github3.issues.milestone.Milestone(json)
60+
milestone = github3.issues.milestone.Milestone(json, self.session)
6161
assert isinstance(milestone.due_on, datetime.datetime)
6262

6363
def test_repr(self):

tests/unit/test_models.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class MyTestRefreshClass(GitHubCore):
1414
"""Subclass for testing refresh on GitHubCore."""
15-
def __init__(self, example_data, session=None):
15+
def __init__(self, example_data, session):
1616
super(MyTestRefreshClass, self).__init__(example_data, session)
1717
self._api = example_data['url']
1818
self.last_modified = example_data['last_modified']
@@ -105,7 +105,7 @@ def test_exposes_attributes(self):
105105

106106
def test_from_json(self):
107107
"""Verify that method returns GitHubObject from json."""
108-
github_core = GitHubCore.from_json('{}')
108+
github_core = GitHubCore.from_json('{}', self.session)
109109
assert isinstance(github_core, GitHubCore)
110110

111111
def test_instance_or_null(self):

tests/unit/test_notifications.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestThread(UnitHelper):
1616

1717
def test_equality(self):
1818
"""Test equality/inequality between two instances."""
19-
thread = github3.notifications.Thread(get_example_data())
19+
thread = github3.notifications.Thread(get_example_data(), self.session)
2020
assert self.instance == thread
2121
thread._uniq = 1
2222
assert self.instance != thread

0 commit comments

Comments
 (0)