From 9663f1d4cabd566a1a467ebd306ed99d42858be4 Mon Sep 17 00:00:00 2001 From: Drew Hubl Date: Sun, 1 Jan 2017 20:18:34 -0700 Subject: [PATCH] Add flake8 tests, and fix all flake8 errors we don't ignore --- .travis.yml | 3 ++- example/app/admin.py | 1 + example/app/models.py | 22 +++++++++++----------- example/app/test_msf.py | 4 ++-- example/app/urls.py | 3 ++- example/example/settings.py | 10 +++++----- example/example/settings_no_debug.py | 2 +- example/example/urls.py | 6 ++++-- example/example/wsgi.py | 9 +++++---- multiselectfield/__init__.py | 4 ++-- multiselectfield/db/fields.py | 3 ++- multiselectfield/utils.py | 4 ++-- multiselectfield/validators.py | 5 ++++- setup.py | 2 ++ tox.ini | 21 +++++++++++++++++++++ 15 files changed, 66 insertions(+), 33 deletions(-) diff --git a/.travis.yml b/.travis.yml index 12d752c..1e87fa6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -56,10 +56,11 @@ matrix: install: - pip install -q $DJANGO_VERSION - - pip install tox coveralls + - pip install tox coveralls flake8 script: - coverage erase + - if [[ $(python -c 'import sys; print("0" if sys.version_info < (2, 7) else "1")') == "1" ]]; then flake8 --ignore=E501; fi - PYTHONPATH=. coverage run -p example/run_tests.py - PYTHONPATH=. coverage run -p example/run_tests.py example.settings_no_debug after_success: diff --git a/example/app/admin.py b/example/app/admin.py index ef01fc1..1e7e7d0 100644 --- a/example/app/admin.py +++ b/example/app/admin.py @@ -22,4 +22,5 @@ from .models import Book class BookAdmin(admin.ModelAdmin): pass + admin.site.register(Book, BookAdmin) diff --git a/example/app/models.py b/example/app/models.py index 5d9eecd..eb2819d 100644 --- a/example/app/models.py +++ b/example/app/models.py @@ -28,15 +28,15 @@ CATEGORY_CHOICES = ( ) TAGS_CHOICES = ( - ('sex', _('Sex')), - ('work', _('Work')), - ('happy', _('Happy')), - ('food', _('Food')), - ('field', _('Field')), - ('boring', _('Boring')), - ('interesting', _('Interesting')), - ('huge', _('Huge')), - ('nice', _('Nice')), + ('sex', _('Sex')), # noqa: E241 + ('work', _('Work')), # noqa: E241 + ('happy', _('Happy')), # noqa: E241 + ('food', _('Food')), # noqa: E241 + ('field', _('Field')), # noqa: E241 + ('boring', _('Boring')), # noqa: E241 + ('interesting', _('Interesting')), # noqa: E241 + ('huge', _('Huge')), # noqa: E241 + ('nice', _('Nice')), # noqa: E241 ) PROVINCES = ( @@ -52,7 +52,7 @@ STATES = ( PROVINCES_AND_STATES = ( (_("Canada - Provinces"), PROVINCES), - (_("USA - States"), STATES), + (_("USA - States"), STATES), # noqa: E241 ) @@ -60,7 +60,7 @@ class Book(models.Model): title = models.CharField(max_length=200) categories = MultiSelectField(choices=CATEGORY_CHOICES, max_choices=3, - #default='1,5') + # default='1,5') default=1) tags = MultiSelectField(choices=TAGS_CHOICES, null=True, blank=True) diff --git a/example/app/test_msf.py b/example/app/test_msf.py index 831d52b..72481cf 100644 --- a/example/app/test_msf.py +++ b/example/app/test_msf.py @@ -27,7 +27,7 @@ from .models import Book, PROVINCES, STATES, PROVINCES_AND_STATES if sys.version_info < (3,): - u = unicode + u = unicode # noqa: F821 else: u = str @@ -129,7 +129,7 @@ class MultiSelectTestCase(TestCase): self.assertEqual(get_field(Book, 'categories').value_to_string(book), '1,3,5') def test_flatchoices(self): - self.assertEqual(get_field(Book, 'published_in').flatchoices, list(PROVINCES+STATES)) + self.assertEqual(get_field(Book, 'published_in').flatchoices, list(PROVINCES + STATES)) def test_named_groups(self): self.assertEqual(get_field(Book, 'published_in').choices, PROVINCES_AND_STATES) diff --git a/example/app/urls.py b/example/app/urls.py index 3ba0d5a..c675e2a 100644 --- a/example/app/urls.py +++ b/example/app/urls.py @@ -35,6 +35,7 @@ except ImportError: # Django < 1.4 from .views import app_index -urlpatterns = patterns('', +urlpatterns = patterns( + '', url(r'^$', app_index, name='app_index'), ) diff --git a/example/example/settings.py b/example/example/settings.py index 4b3076a..7e2de57 100644 --- a/example/example/settings.py +++ b/example/example/settings.py @@ -32,7 +32,7 @@ MANAGERS = ADMINS DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. + 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'example.db', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': '', @@ -100,7 +100,7 @@ STATICFILES_DIRS = ( STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', -# 'django.contrib.staticfiles.finders.DefaultStorageFinder', + # 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) # Make this unique, and don't share it with anybody. @@ -128,7 +128,7 @@ if VERSION < (1, 8): TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', - # 'django.template.loaders.eggs.Loader', + # 'django.template.loaders.eggs.Loader', ) TEMPLATE_DIRS = ( @@ -191,13 +191,13 @@ INSTALLED_APPS = ( # If formadmin is installed -from django.conf import ENVIRONMENT_VARIABLE +from django.conf import ENVIRONMENT_VARIABLE # noqa: E402 # I check it if formadmin is installed of this way because if I execute # python manage.py runserver --settings=settings_no_debug # I get an error if os.environ[ENVIRONMENT_VARIABLE] == 'example.settings': try: - import formadmin + import formadmin # noqa: F401 INSTALLED_APPS += ('formadmin',) except ImportError: pass diff --git a/example/example/settings_no_debug.py b/example/example/settings_no_debug.py index 21508ae..83c9dd5 100644 --- a/example/example/settings_no_debug.py +++ b/example/example/settings_no_debug.py @@ -1,4 +1,4 @@ -from example.settings import * +from example.settings import * # noqa: F401,F403 DEBUG = False TEMPLATE_DEBUG = DEBUG diff --git a/example/example/urls.py b/example/example/urls.py index 121903c..efa4a7b 100644 --- a/example/example/urls.py +++ b/example/example/urls.py @@ -42,12 +42,14 @@ js_info_dict = { 'packages': ('django.conf',), } -urlpatterns = patterns('', +urlpatterns = patterns( + '', url(r'^', include('app.urls')), url(r'^admin/', include(admin.site.urls)), ) -urlpatterns += patterns('', +urlpatterns += patterns( + '', url(r'^%s(?P.*)$' % settings.MEDIA_URL[1:], serve, {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), diff --git a/example/example/wsgi.py b/example/example/wsgi.py index 9908dea..5e9cfcd 100644 --- a/example/example/wsgi.py +++ b/example/example/wsgi.py @@ -15,16 +15,17 @@ framework. """ import os +# This application object is used by any WSGI server configured to use this +# file. This includes Django's development server, if the WSGI_APPLICATION +# setting points here. +from django.core.wsgi import get_wsgi_application + # We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks # if running multiple sites in the same mod_wsgi process. To fix this, use # mod_wsgi daemon mode with each site in its own daemon process, or use # os.environ["DJANGO_SETTINGS_MODULE"] = "test_project.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings") -# This application object is used by any WSGI server configured to use this -# file. This includes Django's development server, if the WSGI_APPLICATION -# setting points here. -from django.core.wsgi import get_wsgi_application application = get_wsgi_application() # Apply WSGI middleware here. diff --git a/multiselectfield/__init__.py b/multiselectfield/__init__.py index 32e5b67..56809c1 100644 --- a/multiselectfield/__init__.py +++ b/multiselectfield/__init__.py @@ -1,2 +1,2 @@ -from multiselectfield.db.fields import MultiSelectField -from multiselectfield.forms.fields import MultiSelectFormField +from multiselectfield.db.fields import MultiSelectField # noqa: F401 +from multiselectfield.forms.fields import MultiSelectFormField # noqa: F401 diff --git a/multiselectfield/db/fields.py b/multiselectfield/db/fields.py index 2162c49..80b5b1c 100644 --- a/multiselectfield/db/fields.py +++ b/multiselectfield/db/fields.py @@ -27,7 +27,7 @@ from ..utils import get_max_length from ..validators import MaxValueMultiFieldValidator if sys.version_info < (3,): - string_type = unicode + string_type = unicode # noqa: F821 else: string_type = str @@ -148,6 +148,7 @@ class MultiSelectField(models.CharField): setattr(cls, 'get_%s_list' % self.name, get_list) setattr(cls, 'get_%s_display' % self.name, get_display) + if VERSION < (1, 8): MultiSelectField = add_metaclass(models.SubfieldBase)(MultiSelectField) diff --git a/multiselectfield/utils.py b/multiselectfield/utils.py index 1f9e8c6..56c5d93 100644 --- a/multiselectfield/utils.py +++ b/multiselectfield/utils.py @@ -18,8 +18,8 @@ import sys if sys.version_info[0] == 2: - string = basestring - string_type = unicode + string = basestring # noqa: F821 + string_type = unicode # noqa: F821 else: string = str string_type = string diff --git a/multiselectfield/validators.py b/multiselectfield/validators.py index 50c8328..5c24521 100644 --- a/multiselectfield/validators.py +++ b/multiselectfield/validators.py @@ -20,14 +20,17 @@ from django.utils.translation import ugettext_lazy as _ class MaxValueMultiFieldValidator(validators.MaxLengthValidator): - clean = lambda self, x: len(','.join(x)) code = 'max_multifield_value' + def clean(self, x): + return len(','.join(x)) + class MinChoicesValidator(validators.MinLengthValidator): message = _(u'You must select a minimum of %(limit_value)d choices.') code = 'min_choices' + class MaxChoicesValidator(validators.MaxLengthValidator): message = _(u'You must select a maximum of %(limit_value)d choices.') code = 'max_choices' diff --git a/setup.py b/setup.py index f889201..5e37d96 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,7 @@ from setuptools import setup, find_packages def read(*rnames): return open(os.path.join(os.path.dirname(__file__), *rnames)).read() + setup( name="django-multiselectfield", version="0.1.4", @@ -51,6 +52,7 @@ setup( 'django>=1.4', 'tox', 'coverage', + 'flake8', ], install_requires=[ 'django>=1.4', diff --git a/tox.ini b/tox.ini index 30b8d3d..72bbf3f 100644 --- a/tox.ini +++ b/tox.ini @@ -6,6 +6,7 @@ usedevelop = True setenv = PYTHONPATH=. commands = + python {envbindir}/flake8 --ignore=E501 python {envbindir}/coverage run -p example/run_tests.py python {envbindir}/coverage run -p example/run_tests.py example.settings_no_debug install_command = @@ -18,6 +19,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py26-dj15] basepython = python2.6 @@ -26,6 +28,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py26-dj16] basepython = python2.6 @@ -34,6 +37,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py27-dj14] @@ -43,6 +47,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py27-dj15] basepython = python2.7 @@ -51,6 +56,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py27-dj16] basepython = python2.7 @@ -59,6 +65,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py27-dj17] basepython = python2.7 @@ -67,6 +74,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py27-dj18] basepython = python2.7 @@ -75,6 +83,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py27-dj19] basepython = python2.7 @@ -83,6 +92,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py27-dj110] basepython = python2.7 @@ -91,6 +101,7 @@ deps = pillow==1.7.8 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py33-dj16] @@ -100,6 +111,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py33-dj17] basepython = python3.3 @@ -108,6 +120,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py33-dj18] basepython = python3.3 @@ -116,6 +129,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py34-dj17] @@ -125,6 +139,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py34-dj18] basepython = python3.4 @@ -133,6 +148,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py34-dj19] basepython = python3.4 @@ -141,6 +157,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py34-dj110] basepython = python3.4 @@ -149,6 +166,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py35-dj18] @@ -158,6 +176,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py35-dj19] basepython = python3.5 @@ -166,6 +185,7 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8 [testenv:py35-dj110] basepython = python3.5 @@ -174,3 +194,4 @@ deps = pillow==2.1.0 PyYAML==3.10 coveralls==0.3 + flake8