Skip to content

Commit accc015

Browse files
authored
Finish linting on py3ish code (#2949)
* Resolve first round of linting errors in restapi. This is a first step and only adds docstrings, etc. It doesn't refactor any code. Conflicts: prospector-more.yml readthedocs/restapi/views/model_views.py * Reduce unused params. In several cases, we can use the provided `request` rather than access `self` (which could make these functions easier to test in isolation). In others, the unused args could be folded into an unnamed kwarg param. * Don't replace section variable. This worked as intended, but only because the overridden section variable would later evaluate to True. Using a different variable name makes this a bit less error-prone. * Minor docstring update * Fix indent on docstring * Punt on refactoring * Fix call args * Try unpinning testing reqs * Bump up prospector higher level strictness * Linting issues: arguments-differ, len-as-condition, redefined variables * Remove unnecessary elses This addresses no-else-return, which removes a `return` in an `else` after an `if` that already has a `return`. This makes the default return more obvious in most cases. * Fix new linting issues with import order Also, update travis and tox config for multiple versions of python * Another fix on travis envs
1 parent 0cf5786 commit accc015

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+353
-299
lines changed

.travis.yml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
language: python
22
python:
3-
- 3.6
3+
- 2.7
4+
- 3.6
45
sudo: false
5-
env:
6-
- TOX_ENV=py27
7-
- TOX_ENV=py36
8-
- TOX_ENV=docs
9-
- TOX_ENV=lint
10-
- TOX_ENV=eslint
6+
matrix:
7+
include:
8+
- python: 2.7
9+
env: TOXENV=docs
10+
- python: 2.7
11+
env: TOXENV=lint
12+
- python: 2.7
13+
env: TOXENV=eslint
14+
script:
15+
- tox
1116
cache:
1217
directories:
1318
- ~/.cache/pip
1419
- ~/.nvm/nvm.sh
1520
install:
16-
- pip install tox
17-
- curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
18-
- source ~/.nvm/nvm.sh
19-
- nvm install --lts
20-
- nvm use --lts
21-
- npm install
22-
- bower install
21+
- pip install tox-travis
22+
- curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
23+
- source ~/.nvm/nvm.sh
24+
- nvm install --lts
25+
- nvm use --lts
26+
- npm install
27+
- bower install
2328
script:
24-
- tox -e $TOX_ENV
29+
- tox
2530
notifications:
2631
slack:
2732
rooms:

prospector-more.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
inherits: prospector
22

3-
strictness: medium
3+
strictness: high
44

55
ignore-paths:
6-
- restapi/
6+
- constants.py
7+
- urls.py
8+
- wsgi.py
9+
- acl
10+
- api
11+
- betterversion
12+
- bookmarks
13+
- builds
14+
- cdn
15+
- comments
16+
- core
17+
- djangome
18+
- doc_builder
19+
- donate
20+
- gold
21+
- integrations
22+
- locale
23+
- notifications
24+
- oauth
25+
- payments
26+
- privacy
27+
- profiles
28+
- projects
29+
- redirects
30+
- restapi
31+
- rtd_tests
32+
- search
33+
- settings
34+
- tastyapi
35+
- templates
36+
- vcs_support
737

838
pylint:
939
options:

readthedocs/api/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def is_authenticated(self, request, **kwargs):
162162

163163

164164
class EnhancedModelResource(ModelResource):
165-
def obj_get_list(self, request=None, *_, **kwargs):
165+
def obj_get_list(self, request=None, *_, **kwargs): # pylint: disable=arguments-differ
166166
"""
167167
A ORM-specific implementation of ``obj_get_list``.
168168

readthedocs/bookmarks/views.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
class BookmarkExistsView(View):
2626

2727
@method_decorator(csrf_exempt)
28-
def dispatch(self, *args, **kwargs):
29-
return super(BookmarkExistsView, self).dispatch(*args, **kwargs)
28+
def dispatch(self, request, *args, **kwargs):
29+
return super(BookmarkExistsView, self).dispatch(request, *args, **kwargs)
3030

3131
def get(self, request):
3232
return HttpResponse(
@@ -81,8 +81,8 @@ class BookmarkListView(ListView):
8181
model = Bookmark
8282

8383
@method_decorator(login_required)
84-
def dispatch(self, *args, **kwargs):
85-
return super(BookmarkListView, self).dispatch(*args, **kwargs)
84+
def dispatch(self, request, *args, **kwargs):
85+
return super(BookmarkListView, self).dispatch(request, *args, **kwargs)
8686

8787
def get_queryset(self):
8888
return Bookmark.objects.filter(user=self.request.user)
@@ -94,8 +94,8 @@ class BookmarkAddView(View):
9494

9595
@method_decorator(login_required)
9696
@method_decorator(csrf_exempt)
97-
def dispatch(self, *args, **kwargs):
98-
return super(BookmarkAddView, self).dispatch(*args, **kwargs)
97+
def dispatch(self, request, *args, **kwargs):
98+
return super(BookmarkAddView, self).dispatch(request, *args, **kwargs)
9999

100100
def get(self, request):
101101
return HttpResponse(
@@ -157,8 +157,8 @@ class BookmarkRemoveView(View):
157157

158158
@method_decorator(login_required)
159159
@method_decorator(csrf_exempt)
160-
def dispatch(self, *args, **kwargs):
161-
return super(BookmarkRemoveView, self).dispatch(*args, **kwargs)
160+
def dispatch(self, request, *args, **kwargs):
161+
return super(BookmarkRemoveView, self).dispatch(request, *args, **kwargs)
162162

163163
def get(self, request, *args, **kwargs):
164164
return render_to_response(
@@ -176,30 +176,29 @@ def post(self, request, *args, **kwargs):
176176
bookmark = get_object_or_404(Bookmark, pk=kwargs['bookmark_pk'])
177177
bookmark.delete()
178178
return HttpResponseRedirect(reverse('bookmark_list'))
179-
else:
180-
try:
181-
post_json = json.loads(request.body)
182-
project = Project.objects.get(slug=post_json['project'])
183-
version = project.versions.get(slug=post_json['version'])
184-
url = post_json['url']
185-
page = post_json['page']
186-
except KeyError:
187-
return HttpResponseBadRequest(
188-
json.dumps({'error': "Invalid parameters"})
189-
)
190-
191-
bookmark = get_object_or_404(
192-
Bookmark,
193-
user=request.user,
194-
url=url,
195-
project=project,
196-
version=version,
197-
page=page
179+
try:
180+
post_json = json.loads(request.body)
181+
project = Project.objects.get(slug=post_json['project'])
182+
version = project.versions.get(slug=post_json['version'])
183+
url = post_json['url']
184+
page = post_json['page']
185+
except KeyError:
186+
return HttpResponseBadRequest(
187+
json.dumps({'error': "Invalid parameters"})
198188
)
199-
bookmark.delete()
200189

201-
return HttpResponse(
202-
json.dumps({'removed': True}),
203-
status=200,
204-
content_type="application/json"
205-
)
190+
bookmark = get_object_or_404(
191+
Bookmark,
192+
user=request.user,
193+
url=url,
194+
project=project,
195+
version=version,
196+
page=page
197+
)
198+
bookmark.delete()
199+
200+
return HttpResponse(
201+
json.dumps({'removed': True}),
202+
status=200,
203+
content_type="application/json"
204+
)

readthedocs/builds/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class Meta(object):
3333
model = Version
3434
fields = ['active', 'privacy_level', 'tags']
3535

36-
def save(self, *args, **kwargs):
37-
obj = super(VersionForm, self).save(*args, **kwargs)
36+
def save(self, commit=True):
37+
obj = super(VersionForm, self).save(commit=commit)
3838
if obj.active and not obj.built and not obj.uploaded:
3939
trigger_build(project=obj.project, version=obj)
4040
return obj

readthedocs/builds/models.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ def commit_name(self):
110110
if self.slug == LATEST:
111111
if self.project.default_branch:
112112
return self.project.default_branch
113-
else:
114-
return self.project.vcs_repo().fallback_branch
113+
return self.project.vcs_repo().fallback_branch
115114

116115
if self.slug == STABLE:
117116
if self.type == BRANCH:
@@ -148,7 +147,7 @@ def get_absolute_url(self):
148147
private = self.privacy_level == PRIVATE
149148
return self.project.get_docs_url(version_slug=self.slug, private=private)
150149

151-
def save(self, *args, **kwargs):
150+
def save(self, *args, **kwargs): # pylint: disable=arguments-differ
152151
"""Add permissions to the Version for all owners on save."""
153152
from readthedocs.projects import tasks
154153
obj = super(Version, self).save(*args, **kwargs)
@@ -161,7 +160,7 @@ def save(self, *args, **kwargs):
161160
broadcast(type='app', task=tasks.symlink_project, args=[self.project.pk])
162161
return obj
163162

164-
def delete(self, *args, **kwargs):
163+
def delete(self, *args, **kwargs): # pylint: disable=arguments-differ
165164
from readthedocs.projects import tasks
166165
log.info('Removing files for version %s', self.slug)
167166
tasks.clear_artifacts.delay(version_pk=self.pk)

readthedocs/builds/version_slug.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
"""
1818

1919
from __future__ import absolute_import
20-
from builtins import range
20+
2121
import math
2222
import re
2323
import string
2424
from operator import truediv
25+
2526
from django.db import models
2627
from django.utils.encoding import force_text
27-
from six.moves import range
28+
from builtins import range
2829

2930

3031
# Regex breakdown:

readthedocs/cdn/purge.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Utility to purge MaxCDN files, if configured."""
22

33
from __future__ import absolute_import
4-
from builtins import range
4+
55
import logging
66

77
from django.conf import settings
8-
from six.moves import range
8+
from builtins import range
9+
910

1011
log = logging.getLogger(__name__)
1112

readthedocs/comments/models.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ def from_hash(self, version_slug, page, node_hash, project_slug=None):
3636
if project_slug:
3737
snapshots = snapshots.filter(node__project__slug=project_slug)
3838

39-
if len(snapshots) == 0:
39+
if not snapshots.exists():
4040
raise DocumentNode.DoesNotExist(
4141
"No node exists on %s with a current hash of %s" % (
4242
page, node_hash))
4343

44-
if len(snapshots) == 1:
44+
if snapshots.count() == 1:
4545
# If we have found only one snapshot, we know we have the correct node.
4646
node = snapshots[0].node
4747
else:
@@ -89,22 +89,20 @@ def latest_commit(self):
8989
def visible_comments(self):
9090
if not self.project.comment_moderation:
9191
return self.comments.all()
92-
else:
93-
# non-optimal SQL warning.
94-
decisions = ModerationAction.objects.filter(
95-
comment__node=self,
96-
decision=1,
97-
date__gt=self.snapshots.latest().date
98-
)
99-
valid_comments = self.comments.filter(moderation_actions__in=decisions).distinct()
100-
return valid_comments
92+
# non-optimal SQL warning.
93+
decisions = ModerationAction.objects.filter(
94+
comment__node=self,
95+
decision=1,
96+
date__gt=self.snapshots.latest().date
97+
)
98+
valid_comments = self.comments.filter(moderation_actions__in=decisions).distinct()
99+
return valid_comments
101100

102101
def update_hash(self, new_hash, commit):
103102
latest_snapshot = self.snapshots.latest()
104103
if latest_snapshot.hash == new_hash and latest_snapshot.commit == commit:
105104
return latest_snapshot
106-
else:
107-
return self.snapshots.create(hash=new_hash, commit=commit)
105+
return self.snapshots.create(hash=new_hash, commit=commit)
108106

109107

110108
class DocumentNodeSerializer(serializers.ModelSerializer):
@@ -190,8 +188,7 @@ def has_been_approved_since_most_recent_node_change(self):
190188
# If we do have an approval action which is newer than the most recent change,
191189
# we'll return True or False commensurate with its "approved" attribute.
192190
return latest_moderation_action.approved()
193-
else:
194-
return False
191+
return False
195192

196193
def is_orphaned(self):
197194
raise NotImplementedError('TODO')

readthedocs/comments/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def get_queryset(self):
185185
return queryset
186186

187187
@method_decorator(login_required)
188-
def create(self, request):
188+
def create(self, request, *args, **kwargs):
189189
project = Project.objects.get(slug=request.data['project'])
190190
comment = project.add_comment(version_slug=request.data['version'],
191191
page=request.data['document_page'],

readthedocs/core/forms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def __init__(self, *args, **kwargs):
3232
except AttributeError:
3333
pass
3434

35-
def save(self, *args, **kwargs):
35+
def save(self, commit=True):
3636
first_name = self.cleaned_data.pop('first_name', None)
3737
last_name = self.cleaned_data.pop('last_name', None)
38-
profile = super(UserProfileForm, self).save(*args, **kwargs)
39-
if kwargs.get('commit', True):
38+
profile = super(UserProfileForm, self).save(commit=commit)
39+
if commit:
4040
user = profile.user
4141
user.first_name = first_name
4242
user.last_name = last_name

readthedocs/core/management/commands/import_github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Command(BaseCommand):
1212
help = __doc__
1313

1414
def handle(self, *args, **options):
15-
if len(args):
15+
if args:
1616
for slug in args:
1717
for service in GitHubService.for_user(
1818
User.objects.get(username=slug)

readthedocs/core/management/commands/pull.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Command(BaseCommand):
1717
help = __doc__
1818

1919
def handle(self, *args, **options):
20-
if len(args):
20+
if args:
2121
for slug in args:
2222
tasks.update_imported_docs(
2323
utils.version_from_slug(slug, LATEST).pk

readthedocs/core/management/commands/update_repos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def handle(self, *args, **options):
4343
record = options['record']
4444
force = options['force']
4545
version = options['version']
46-
if len(args):
46+
if args:
4747
for slug in args:
4848
if version and version != "all":
4949
log.info("Updating version %s for %s", version, slug)

0 commit comments

Comments
 (0)