-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
DJ012 produces false positive when there are other dunder methods in model class #13892
Comments
I'm not a Django expert, so it's unclear to me why the Django style guide only mentions the ordering of It seems overriding |
|
I want to make sure that we're 100% sure that this change is in conformance with the style guide because it is a breaking change: All dunder methods other than As I said before, I'm not a Django expert but I did some extra research.
The impression I get is that Ruff's current behavior is correct. My suspicious is that overriding other dunder methods is just very uncommon (or not advised) for Django models. Could you share some examples (from your project and/or other projects) where it is necessary to override additional dunder methods? |
For example, you might want to define For More on that - https://medium.com/@mmzeynalli/how-to-detect-field-changes-in-django-ae4bc719aea2 To summarize, in real-world scenarios, there are a lot of cases where that style guide is impossible to follow, which is why this check is going to produce fake positives |
@AlexWaygood any chance that you're familiar with DJango? |
I think @carljm is almost certainly our best django expert on the team 😄 |
Possibly we should open an issue on Django itself to get the style guide clarified? I assume the upstream rule was simply based on a literal reading of the style guide as currently written. Although overriding Personally I agree with @maddrum that the typical Python convention to put dunder methods first should be respected here, and |
I have raised this |
This one applies to staticmethods, private methods, properties and a lot of other methods that come on top. |
Ticket accepted! (Hello, Django Fellow here 👋 )
As I put in the ticket, my reading of the Django docs for "any custom method" is "any custom business logic or helper method that you create for your model", and it does not apply to overrides of what I accepted the ticket on the basis that the docs could be more clear about this. Thanks everyone! |
ruff check
produces false positive DJ012 for Django models like so:According to Python best practices, dunder methods should always be kept at the beginning of the class.
But since
save()
is a Django method which is after other methods method it invokes error.DJ012 should obey other Python class structuring, before evaluating Django styleguide.
The text was updated successfully, but these errors were encountered: