Skip to content

Commit dec89af

Browse files
slivercmblayman
authored andcommitted
Enforcing of flake8 linter (django-json-api#348)
* Enforcing of flake8 linter * Allow F405 (star imports) as this is part of drf json api concept * Remove E501 so defined max-line-length won't be ignored * Adjusted changelog and authors
1 parent 76b60fe commit dec89af

Some content is hidden

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

42 files changed

+403
-195
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ before_install:
4040
# Force an upgrade of py & pytest to avoid VersionConflict
4141
- pip install --upgrade py
4242
- pip install "pytest>=2.8,<3"
43-
- pip install codecov
43+
- pip install codecov flake8
4444
install:
4545
- pip install Django${DJANGO} djangorestframework${DRF}
4646
- python setup.py install
4747
script:
48+
- flake8
4849
- coverage run setup.py -v test
4950
after_success:
5051
- codecov

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ Jerel Unruh <[email protected]>
22
Greg Aker <[email protected]>
33
Adam Wróbel <https://adamwrobel.com>
44
Christian Zosel <https://zosel.ch>
5+
Oliver Sauder <[email protected]>
56

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ v2.3.0
55
and there was no way to turn it off.
66
* Fix for apps that don't use `django.contrib.contenttypes`.
77
* Fix `resource_name` support for POST requests and nested serializers
8+
* Enforcing flake8 linting
89

910
v2.2.0
1011

example/api/resources/identity.py

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class GenericIdentity(generics.GenericAPIView):
5959
renderer_classes = (renderers.JSONRenderer, )
6060
parser_classes = (parsers.JSONParser, )
6161

62-
6362
def get_queryset(self):
6463
return auth_models.User.objects.all()
6564

example/api/serializers/identity.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ class IdentitySerializer(serializers.ModelSerializer):
66
"""
77
Identity Serializer
88
"""
9+
910
def validate_first_name(self, data):
1011
if len(data) > 10:
1112
raise serializers.ValidationError(
12-
'There\'s a problem with first name')
13+
'There\'s a problem with first name')
1314
return data
1415

1516
def validate_last_name(self, data):

example/api/serializers/post.py

-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ class PostSerializer(serializers.Serializer):
66
Blog post serializer
77
"""
88
title = serializers.CharField(max_length=50)
9-

example/factories/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
faker = FakerFactory.create()
88
faker.seed(983843)
99

10+
1011
class BlogFactory(factory.django.DjangoModelFactory):
1112
class Meta:
1213
model = Blog
@@ -23,6 +24,7 @@ class Meta:
2324

2425
bio = factory.RelatedFactory('example.factories.AuthorBioFactory', 'author')
2526

27+
2628
class AuthorBioFactory(factory.django.DjangoModelFactory):
2729
class Meta:
2830
model = AuthorBio

example/models.py

-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,3 @@ class Comment(BaseModel):
8686

8787
def __str__(self):
8888
return self.body
89-

example/serializers.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_copyright(self, resource):
2424

2525
def get_root_meta(self, resource, many):
2626
return {
27-
'api_docs': '/docs/api/blogs'
27+
'api_docs': '/docs/api/blogs'
2828
}
2929

3030
class Meta:
@@ -55,17 +55,17 @@ def __init__(self, *args, **kwargs):
5555
body_format = serializers.SerializerMethodField()
5656
# many related from model
5757
comments = relations.ResourceRelatedField(
58-
many=True, read_only=True)
58+
many=True, read_only=True)
5959
# many related from serializer
6060
suggested = relations.SerializerMethodResourceRelatedField(
61-
source='get_suggested', model=Entry, many=True, read_only=True,
62-
related_link_view_name='entry-suggested',
63-
related_link_url_kwarg='entry_pk',
64-
self_link_view_name='entry-relationships',
61+
source='get_suggested', model=Entry, many=True, read_only=True,
62+
related_link_view_name='entry-suggested',
63+
related_link_url_kwarg='entry_pk',
64+
self_link_view_name='entry-relationships',
6565
)
6666
# single related from serializer
6767
featured = relations.SerializerMethodResourceRelatedField(
68-
source='get_featured', model=Entry, read_only=True)
68+
source='get_featured', model=Entry, read_only=True)
6969
tags = TaggedItemSerializer(many=True, read_only=True)
7070

7171
def get_suggested(self, obj):

example/settings/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .dev import *
1+
from .dev import * # noqa

example/settings/test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .dev import *
1+
from .dev import * # noqa
22

33
DATABASES = {
44
'default': {

example/tests/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class TestBase(APITestCase):
77
"""
88
Test base class to setup a couple users.
99
"""
10+
1011
def setUp(self):
1112
"""
1213
Create those users
@@ -15,7 +16,7 @@ def setUp(self):
1516
self.create_users()
1617

1718
def create_user(self, username, email, password="pw",
18-
first_name='', last_name=''):
19+
first_name='', last_name=''):
1920
"""
2021
Helper method to create a user
2122
"""
@@ -39,4 +40,3 @@ def create_users(self):
3940
self.miles = self.create_user(
4041
'miles', '[email protected]',
4142
first_name="Miles", last_name="Davis")
42-

example/tests/conftest.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import pytest
22
from pytest_factoryboy import register
33

4-
from example.factories import BlogFactory, AuthorFactory, AuthorBioFactory, EntryFactory, CommentFactory, \
4+
from example.factories import (
5+
BlogFactory, AuthorFactory, AuthorBioFactory, EntryFactory, CommentFactory,
56
TaggedItemFactory
7+
)
68

79
register(BlogFactory)
810
register(AuthorFactory)
@@ -31,4 +33,3 @@ def multiple_entries(blog_factory, author_factory, entry_factory, comment_factor
3133
comment_factory(entry=entries[0])
3234
comment_factory(entry=entries[1])
3335
return entries
34-

example/tests/integration/test_includes.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77

88

99
def test_default_included_data_on_list(multiple_entries, client):
10-
return test_included_data_on_list(multiple_entries=multiple_entries, client=client, query='?page_size=5')
10+
return test_included_data_on_list(
11+
multiple_entries=multiple_entries, client=client, query='?page_size=5'
12+
)
1113

1214

1315
def test_included_data_on_list(multiple_entries, client, query='?include=comments&page_size=5'):
1416
response = client.get(reverse("entry-list") + query)
1517
included = load_json(response.content).get('included')
1618

17-
assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count'
18-
assert [x.get('type') for x in included] == ['comments', 'comments'], 'List included types are incorrect'
19+
assert len(load_json(response.content)['data']) == len(multiple_entries), (
20+
'Incorrect entry count'
21+
)
22+
assert [x.get('type') for x in included] == ['comments', 'comments'], (
23+
'List included types are incorrect'
24+
)
1925

2026
comment_count = len([resource for resource in included if resource["type"] == "comments"])
2127
expected_comment_count = sum([entry.comments.count() for entry in multiple_entries])
@@ -39,7 +45,9 @@ def test_included_data_on_detail(single_entry, client, query='?include=comments'
3945

4046
def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
4147
entry_factory()
42-
response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=featured')
48+
response = client.get(
49+
reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=featured'
50+
)
4351
included = load_json(response.content).get('included')
4452

4553
assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect'
@@ -48,7 +56,9 @@ def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
4856

4957
def test_dynamic_many_related_data_is_included(single_entry, entry_factory, client):
5058
entry_factory()
51-
response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=suggested')
59+
response = client.get(
60+
reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=suggested'
61+
)
5262
included = load_json(response.content).get('included')
5363

5464
assert included
@@ -58,12 +68,12 @@ def test_dynamic_many_related_data_is_included(single_entry, entry_factory, clie
5868
def test_missing_field_not_included(author_bio_factory, author_factory, client):
5969
# First author does not have a bio
6070
author = author_factory(bio=None)
61-
response = client.get(reverse('author-detail', args=[author.pk])+'?include=bio')
71+
response = client.get(reverse('author-detail', args=[author.pk]) + '?include=bio')
6272
data = load_json(response.content)
6373
assert 'included' not in data
6474
# Second author does
6575
author = author_factory()
66-
response = client.get(reverse('author-detail', args=[author.pk])+'?include=bio')
76+
response = client.get(reverse('author-detail', args=[author.pk]) + '?include=bio')
6777
data = load_json(response.content)
6878
assert 'included' in data
6979
assert len(data['included']) == 1
@@ -75,7 +85,9 @@ def test_deep_included_data_on_list(multiple_entries, client):
7585
'comments.author.bio&page_size=5')
7686
included = load_json(response.content).get('included')
7787

78-
assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count'
88+
assert len(load_json(response.content)['data']) == len(multiple_entries), (
89+
'Incorrect entry count'
90+
)
7991
assert [x.get('type') for x in included] == [
8092
'authorBios', 'authorBios', 'authors', 'authors', 'comments', 'comments'
8193
], 'List included types are incorrect'
@@ -99,7 +111,9 @@ def test_deep_included_data_on_list(multiple_entries, client):
99111
'comments.author.bio&page_size=5')
100112
included = load_json(response.content).get('included')
101113

102-
assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count'
114+
assert len(load_json(response.content)['data']) == len(multiple_entries), (
115+
'Incorrect entry count'
116+
)
103117
assert [x.get('type') for x in included] == [
104118
'authorBios', 'authorBios', 'authors', 'authors', 'authors', 'authors',
105119
'comments', 'comments'], 'List included types are incorrect'

example/tests/integration/test_model_resource_name.py

+30-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def _check_resource_and_relationship_comment_type_match(django_client):
2222
comment_relationship_type = load_json(entry_response.content).get(
2323
'data')[0].get('relationships').get('comments').get('data')[0].get('type')
2424

25-
assert comment_resource_type == comment_relationship_type, "The resource type seen in the relationships and head resource do not match"
25+
assert comment_resource_type == comment_relationship_type, (
26+
"The resource type seen in the relationships and head resource do not match"
27+
)
2628

2729

2830
def _check_relationship_and_included_comment_type_are_the_same(django_client, url):
@@ -33,7 +35,9 @@ def _check_relationship_and_included_comment_type_are_the_same(django_client, ur
3335
comment_relationship_type = data.get('relationships').get('comments').get('data')[0].get('type')
3436
comment_included_type = comment.get('type')
3537

36-
assert comment_relationship_type == comment_included_type, "The resource type seen in the relationships and included do not match"
38+
assert comment_relationship_type == comment_included_type, (
39+
"The resource type seen in the relationships and included do not match"
40+
)
3741

3842

3943
@pytest.mark.usefixtures("single_entry")
@@ -81,7 +85,12 @@ def test_resource_name_precendence(self, client, monkeypatch):
8185
'resource_name from model incorrect on list')
8286

8387
# serializer > model
84-
monkeypatch.setattr(serializers.CommentSerializer.Meta, 'resource_name', 'resource_name_from_serializer', False)
88+
monkeypatch.setattr(
89+
serializers.CommentSerializer.Meta,
90+
'resource_name',
91+
'resource_name_from_serializer',
92+
False
93+
)
8594
response = client.get(reverse("comment-list"))
8695
data = load_json(response.content)['data'][0]
8796
assert (data.get('type') == 'resource_name_from_serializer'), (
@@ -104,8 +113,18 @@ def test_model_resource_name_create(self, client):
104113
assert response.status_code == status.HTTP_201_CREATED
105114

106115
def test_serializer_resource_name_create(self, client, monkeypatch):
107-
monkeypatch.setattr(serializers.CommentSerializer.Meta, 'resource_name', 'renamed_comments', False)
108-
monkeypatch.setattr(serializers.EntrySerializer.Meta, 'resource_name', 'renamed_entries', False)
116+
monkeypatch.setattr(
117+
serializers.CommentSerializer.Meta,
118+
'resource_name',
119+
'renamed_comments',
120+
False
121+
)
122+
monkeypatch.setattr(
123+
serializers.EntrySerializer.Meta,
124+
'resource_name',
125+
'renamed_entries',
126+
False
127+
)
109128
create_data = deepcopy(self.create_data)
110129
create_data['data']['type'] = 'renamed_comments'
111130
create_data['data']['relationships']['entry']['data']['type'] = 'renamed_entries'
@@ -143,7 +162,9 @@ def test_type_match_on_included_and_inline_without_serializer_resource_name(self
143162

144163
_check_relationship_and_included_comment_type_are_the_same(client, reverse("entry-list"))
145164

146-
def test_type_match_on_included_and_inline_with_serializer_resource_name_and_JSONAPIMeta(self, client):
165+
def test_type_match_on_included_and_inline_with_serializer_resource_name_and_JSONAPIMeta(
166+
self, client
167+
):
147168
models.Comment.__bases__ += (_PatchedModel,)
148169
serializers.CommentSerializer.Meta.resource_name = "resource_name_from_serializer"
149170

@@ -163,7 +184,9 @@ def test_resource_and_relationship_type_match_with_JSONAPIMeta(self, client):
163184

164185
_check_resource_and_relationship_comment_type_match(client)
165186

166-
def test_resource_and_relationship_type_match_with_serializer_resource_name_and_JSONAPIMeta(self, client):
187+
def test_resource_and_relationship_type_match_with_serializer_resource_name_and_JSONAPIMeta(
188+
self, client
189+
):
167190
models.Comment.__bases__ += (_PatchedModel,)
168191
serializers.CommentSerializer.Meta.resource_name = "resource_name_from_serializer"
169192

example/tests/integration/test_non_paginated_responses.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django.core.urlresolvers import reverse
2-
from django.conf import settings
32

43
try:
54
from unittest import mock

example/tests/integration/test_pagination.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
pytestmark = pytest.mark.django_db
1212

13+
1314
@mock.patch(
1415
'rest_framework_json_api.utils'
1516
'.get_default_included_resources_from_serializer',
@@ -62,11 +63,11 @@ def test_pagination_with_single_entry(single_entry, client):
6263
}
6364
}],
6465
"links": {
65-
"first": "http://testserver/entries?page=1",
66-
"last": "http://testserver/entries?page=1",
67-
"next": None,
68-
"prev": None,
69-
},
66+
"first": "http://testserver/entries?page=1",
67+
"last": "http://testserver/entries?page=1",
68+
"next": None,
69+
"prev": None,
70+
},
7071
"meta":
7172
{
7273
"pagination":

example/tests/integration/test_sparse_fieldsets.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
def test_sparse_fieldset_ordered_dict_error(multiple_entries, client):
1010
base_url = reverse('entry-list')
1111
querystring = '?fields[entries]=blog,headline'
12-
response = client.get(base_url + querystring) # RuntimeError: OrderedDict mutated during iteration
12+
# RuntimeError: OrderedDict mutated during iteration
13+
response = client.get(base_url + querystring)
1314
assert response.status_code == 200 # succeed if we didn't fail due to the above RuntimeError

example/tests/test_format_keys.py

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def setUp(self):
1616
super(FormatKeysSetTests, self).setUp()
1717
self.detail_url = reverse('user-detail', kwargs={'pk': self.miles.pk})
1818

19-
2019
def test_camelization(self):
2120
"""
2221
Test that camelization works.

example/tests/test_generic_validation.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import json
2-
31
from django.core.urlresolvers import reverse
4-
from django.conf import settings
52

6-
from rest_framework.serializers import ValidationError
73

84
from example.tests import TestBase
95
from example.tests.utils import load_json
@@ -13,6 +9,7 @@ class GenericValidationTest(TestBase):
139
"""
1410
Test that a non serializer specific validation can be thrown and formatted
1511
"""
12+
1613
def setUp(self):
1714
super(GenericValidationTest, self).setUp()
1815
self.url = reverse('user-validation', kwargs={'pk': self.miles.pk})

example/tests/test_generic_viewset.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import json
2-
31
from django.core.urlresolvers import reverse
42
from django.conf import settings
53

@@ -42,7 +40,6 @@ def test_default_rest_framework_behavior(self):
4240

4341
assert expected == parsed_content
4442

45-
4643
def test_ember_expected_renderer(self):
4744
"""
4845
The :class:`UserEmber` ViewSet has the ``resource_name`` of 'data'

0 commit comments

Comments
 (0)