Skip to content

Commit c8e256c

Browse files
committed
Make session required for GitHubCore classes
We want the session to always be there. This change makes it required and updates the various places that broke. Fixes: 771 Signed-off-by: Jesse Keating <omgjlk@github.com>
1 parent 11ec3fb commit c8e256c

16 files changed

+42
-34
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/git.py

+2-2
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)

github3/github.py

+2-2
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:
@@ -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/models.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ class GitHubCore(object):
3232
have.
3333
"""
3434

35-
def __init__(self, json, session=None):
35+
def __init__(self, json, session):
3636
if hasattr(session, 'session'):
3737
# i.e. session is actually a GitHubCore instance
3838
session = session.session
39-
elif session is None:
40-
session = GitHubSession()
4139
self.session = session
4240

4341
# set a sane default
@@ -165,14 +163,14 @@ def __repr__(self):
165163
return repr_string
166164

167165
@classmethod
168-
def from_dict(cls, json_dict):
166+
def from_dict(cls, json_dict, session):
169167
"""Return an instance of this class formed from ``json_dict``."""
170-
return cls(json_dict)
168+
return cls(json_dict, session)
171169

172170
@classmethod
173-
def from_json(cls, json):
171+
def from_json(cls, json, session):
174172
"""Return an instance of this class formed from ``json``."""
175-
return cls(loads(json))
173+
return cls(loads(json), session)
176174

177175
def __eq__(self, other):
178176
return self._uniq == other._uniq
@@ -348,6 +346,10 @@ def refresh(self, conditional=False):
348346
self._update_attributes(json)
349347
return self
350348

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

352354
class BaseComment(GitHubCore):
353355

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/status.py

+1-1
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

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

tests/unit/test_projects.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_create_card_with_content_id(self):
142142
def test_create_card_with_issue(self):
143143
"""Show that a user can create a new project card with an Issue."""
144144
issue_data = get_issue_example_data()
145-
issue = issues.Issue(issue_data)
145+
issue = issues.Issue(issue_data, self.session)
146146

147147
self.instance.create_card_with_issue(issue)
148148
self.post_called_with(

tests/unit/test_users.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TestUser(helper.UnitHelper):
3636

3737
def test_equality(self):
3838
"""Show that two instances are equal."""
39-
user = github3.users.User(get_users_example_data())
39+
user = github3.users.User(get_users_example_data(), self.session)
4040
self.instance == user
4141

4242
user._uniq += 1
@@ -88,7 +88,7 @@ class TestUserKey(helper.UnitHelper):
8888

8989
def test_equality(self):
9090
"""Show that two instances of Key are equal."""
91-
key = github3.users.Key(get_user_key_example_data())
91+
key = github3.users.Key(get_user_key_example_data(), self.session)
9292
assert self.instance == key
9393

9494
key._uniq += "cruft"

0 commit comments

Comments
 (0)