-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[DJ
] Add django-model-with-dunder-unicode
#12607
Conversation
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 |
@carljm do you know if there are any other dunder methods that were deprecated as part of Python 3? @Amar1729 here's an example of a rule that uses different messages. ruff/crates/ruff_linter/src/rules/pylint/rules/assert_on_string_literal.rs Lines 28 to 43 in 18452cf
|
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 ifDJ014
is 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 DJ014
cargo test