sphinxcontrib-django icon indicating copy to clipboard operation
sphinxcontrib-django copied to clipboard

Add support for `ruff --select=DJ`

Open cclauss opened this issue 1 year ago • 1 comments

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

cclauss avatar Feb 25 '24 16:02 cclauss

@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.

timobrembeck avatar Mar 03 '24 16:03 timobrembeck