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

DJ008 (__str__ method) fails when inheriting from multiple models, and str only defined in other module #15454

Open
zyzzyxdonta opened this issue Jan 13, 2025 · 1 comment
Labels
rule Implementing or modifying a lint rule type-inference Requires more advanced type inference.

Comments

@zyzzyxdonta
Copy link

This is related to #9242.

When a Django model inherits from an abstract model in the same module and a model in a different module, the __str__ from the other module is not recognized and trips DJ008.

This is a minimal example that reproduces this behaviour:

ruff-bug-repro.zip

.
├── mypackage
│   ├── core
│   │   ├── __init__.py
│   │   └── models.py
│   ├── __init__.py
│   └── resources
│       ├── __init__.py
│       └── models.py
└── pyproject.toml

Core models:

from django.db import models

class Thing(models.Model):
    label = models.TextField()

    def __str__(self):
        return self.label or str(self.pk)

Resources models:

from django.db import models

from mypackage.core.models import Thing


class Resource(models.Model):
    resource_id = models.AutoField(primary_key=True)

    class Meta:
        abstract = True


class Job(Resource, Thing):
    job_id = models.TextField()

Output from ruff 0.9.1:

mypackage/resources/models.py:13:7: DJ008 Model does not define `__str__` method
   |
13 | class Job(Resource, Thing):
   |       ^^^ DJ008
14 |     job_id = models.TextField()
   |

Found 1 error.

Thanks!

@dylwil3 dylwil3 added the type-inference Requires more advanced type inference. label Jan 13, 2025
@dhruvmanila
Copy link
Member

I wonder if we should ignore this rule when the subclass is coming from a different file to reduce the false positive but that does come with the possibility of increasing the false negative.

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule type-inference Requires more advanced type inference.
Projects
None yet
Development

No branches or pull requests

4 participants