Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if default_app_config is used on Django 1.7+ #41

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
name: run coverage
command: |
. venv/bin/activate
pip install -e .
pip install -e ".[tests]"
py.test --cov=. --cov-fail-under=100
- run:
name: upload coverage report
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# IntelliJ
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ $ pytest --cov=.
| `DJ06` | ModelForm should not set exclude, instead it should use fields, which is an explicit list of all the fields that should be included in the form |
| `DJ07` | ModelForm.Meta should not set fields to `__all__`|
| `DJ08` | Models that inherits from django db models should set `__str__`|
| `DJ09` | New applications should avoid default_app_config|

## Licence

Expand Down
11 changes: 9 additions & 2 deletions flake8_django/checker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast

from flake8_django.checkers import ModelDunderStrMissingChecker, ModelFieldChecker, ModelFormChecker, URLChecker, RenderChecker
from flake8_django.checkers import DefaultAppConfigChecker, ModelDunderStrMissingChecker, ModelFieldChecker, \
ModelFormChecker, URLChecker, RenderChecker

__version__ = '0.0.3'

Expand All @@ -18,7 +19,10 @@ class DjangoStyleFinder(ast.NodeVisitor):
'ClassDef': [
ModelFormChecker(),
ModelDunderStrMissingChecker(),
]
],
'Module': [
DefaultAppConfigChecker(),
],
}

def __init__(self, *args, **kwargs):
Expand All @@ -38,6 +42,9 @@ def visit_Call(self, node):
def visit_ClassDef(self, node):
self.capture_issues_visitor('ClassDef', node)

def visit_Module(self, node):
self.capture_issues_visitor('Module', node)


class DjangoStyleChecker(object):
"""
Expand Down
3 changes: 2 additions & 1 deletion flake8_django/checkers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .default_app_config import DefaultAppConfigChecker
from .model_dunder_str import ModelDunderStrMissingChecker
from .model_fields import ModelFieldChecker
from .model_form import ModelFormChecker
from .render import RenderChecker
from .urls import URLChecker


__all__ = ['ModelDunderStrMissingChecker', 'ModelFieldChecker', 'ModelFormChecker', 'RenderChecker', 'URLChecker']
__all__ = ['DefaultAppConfigChecker', 'ModelDunderStrMissingChecker', 'ModelFieldChecker', 'ModelFormChecker', 'RenderChecker', 'URLChecker']
29 changes: 29 additions & 0 deletions flake8_django/checkers/default_app_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import ast

import django

from .checker import Checker
from .issue import Issue


class DJ09(Issue):
code = 'DJ09'
description = 'New applications should avoid default_app_config'


class DefaultAppConfigChecker(Checker):

def run(self, node):
if django.VERSION < (1, 7):
return []
issues = []
for elem in node.body:
if not isinstance(elem, ast.Assign):
continue
target_names = map(lambda n: n.id == 'default_app_config', elem.targets)
if any(target_names):
issues.append(DJ09(
elem.lineno,
elem.col_offset,
))
return issues
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
},
install_requires=['flake8'],
tests_require=['pytest'],
tests_require=['django', 'pytest'],
classifiers=[
'Framework :: Flake8',
'License :: OSI Approved :: MIT License',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/default_app_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'foobar'
16 changes: 16 additions & 0 deletions tests/test_default_app_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import django

from .utils import run_check, load_fixture_file


def test_default_app_config_django_16(monkeypatch):
monkeypatch.setattr(django, 'VERSION', (1, 6))
code = load_fixture_file('default_app_config.py')
assert len(run_check(code)) == 0


def test_default_app_config_django_17(monkeypatch):
monkeypatch.setattr(django, 'VERSION', (1, 7))
code = load_fixture_file('default_app_config.py')
assert len(run_check(code)) == 1
assert 'DJ09' in run_check(code)[0][2]
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ envlist = py34,py35,py36,py37
max-line-length = 120
exclude = tests/fixtures/*
[testenv]
deps = pytest
deps =
pytest
django
commands =
pytest