[DJ] Add django-model-with-dunder-unicode#12607
[DJ] Add django-model-with-dunder-unicode#12607Amar1729 wants to merge 1 commit intoastral-sh:mainfrom
DJ] Add django-model-with-dunder-unicode#12607Conversation
5af2392 to
778b4e6
Compare
778b4e6 to
cc67733
Compare
|
|
This looks reasonable to me, but would be helpful to have a second opinion -- maybe @AlexWaygood or @carljm with Django expertise? |
|
I think this rule makes sense, but I don't think there should be anything Django specific about it. The |
|
Would this already be caught by PLW3201? If not, should it be? |
Nope, this doesn't seem to be the case today playground Including it in |
The special handling of the method has been removed from Python's data model. I wouldn't say that the method was removed, since you can still define it on any class you like, and you won't get an error. It's just that it now behaves as a normal method rather than one that has any special semantics. But anybody defining it on any class is almost certainly doing so erroneously, because they think that it will have special semantics when it won't. |
|
Hm, is there a straightforward way within the PLW3201 ruff code to output an extra message for a specific case? or would it make more sense to introduce a new code eg PLW3202 deprecated-dunder? (are there even any other deprecated dunder methods?*) *edit: a quick search of the docs indicates some magic methods in py2 documentation that aren't mentioned in docs for py3. i don't know about some of them but |
Four that I know off, offhand, are |
|
In addition to the ones Alex mentioned, these are also all gone in Python 3: |
|
Other removed dunder methods are |
|
Thanks @Amar1729 for creating this PR and triggering this discussion. Considering that |
|
I created an issue for the improvement |
|
no problem! a new PR sounds good, I'll see what I can do. |
Summary
Derived from a datadog rule, I've implemented a check for a Django model implementing a
__unicode__method. This was a python2-ism that is not used anymore (as of Python 3.0).This method is probably quite uncommon by now, but i recently ran into a legacy codebase where
__unicode__()was still defined and figured it might be helpful to write a simple rule for it. I believe this method was common in Django projects at the time, but it's entirely possibly to have defined a__unicode__method outside of using Django; as such, I'm not sure if it makes sense to put this rule somewhere else (e.g. PYUP?) and make it more general, to any kind of class. Either way, it's not directly derived from either of those checkers, and so I'm not sure either ifDJ014is necessarily the best number for it. Thoughts?Test Plan
cargo run -p ruff -- check crates/ruff_linter/resources/test/fixtures/flake8_django/DJ014.py --no-cache --preview --select DJ014cargo test