sphinx-doc / sphinxcontrib-django

This is a sphinx extension which improves the documentation of Django apps.
https://pypi.org/project/sphinxcontrib-django/
Apache License 2.0
43 stars 25 forks source link

Add support for `ruff --select=DJ` #65

Open cclauss opened 7 months ago

cclauss commented 7 months ago

Motivation

% ruff --select=DJ # https://docs.astral.sh/ruff/rules/django-model-without-dunder-str

warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `pyproject.toml`:
  - 'ignore' -> 'lint.ignore'
  - 'select' -> 'lint.select'
tests/roots/test-docstrings/dummy_django_app/models.py:22:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:26:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:82:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:86:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:90:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:119:11: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:123:7: DJ008 Model does not define `__str__` method
Found 7 errors.

Proposed Solution

Edit pyproject.toml to fix the two warnings above and add DJ to the lint.select and then add .__str__() methods to the 7 Django models.

% ruff rule DJ008

django-model-without-dunder-str (DJ008)

Derived from the flake8-django linter.

What it does

Checks that __str__ method is defined in Django models.

Why is this bad?

Django models should define __str__ method to return a string representation of the model instance, as Django calls this method to display the object in the Django Admin and elsewhere.

Models without __str__ method will display a non-meaningful representation of the object in the Django Admin.

Example

from django.db import models

class MyModel(models.Model):
    field = models.CharField(max_length=255)

Use instead:

from django.db import models

class MyModel(models.Model):
    field = models.CharField(max_length=255)

    def __str__(self):
        return f"{self.field}"

Alternatives

Additional Context

timobrembeck commented 7 months ago

@cclauss feel free to contribute this change, but I don't think it's strictly necessary. The dummy django app is only used for testing and not contained in the package distribution shipped via PyPI.