I have a file in which I define an import of the format:
import django.db.models as models
Which when run through pylint triggers the "useless-import-alias" warning. However, models != django.db.models so I don't believe this should be triggering the warning.
You might as well do from django.db import models.
True, but the message's documentation says:
Used when an import alias is same as original package. e.g using import numpy as numpy instead of import numpy as np
To me at least this isn't the same use case. From that it seemed like it was designed to catch
import x as x
import a.b.x as x though it seems less likely to have been an error on the user's part and it I'd argue it's not "useless". It seems more a candidate for a consider-import-from check,
I agree. The warning here should indeed be 'consider-import-from'.
I also think this was closed prematurely. The error messages should be as clear as possible, which this one isn't. The import is not useless, it has a purpose (like the @heoga noted). There is also a different way to do it (like @PCManticore noted). Even if Pylint somehow decrees that @PCManticore 's way of doing it is the preferred one, the message should note it, so that people won't need to google and find this thread.
I'd at least change the message in cases like this bug is about to be clearer.
Hey @Sveder
I totally agree that the import is not useless, it has a purpose, just that it can be rewritten in a simpler way, which is what this check is supposed to be recommending anyway. Pylint is definitely not decreeing that the only way to write Python code is my way, not exactly sure how you got to that impression. We're all trying to be helpful here, to make the linter a good tool for recommending good and safe practices, and there's no hidden agenda behind our actions.
But as the folks have mentioned already, the message could be improved a bit, as it might be confusing on what it suggests. To that I agree. I don't agree though that import a.b as b shouldn't be rewritten as from a import b, which again is what this check is supposed to be doing anyway. As such, I'm reopening this issue and adding it to a new project for tracking error messages that can be improved. As mentioned, we want pylint to be a helpful tool, so if you find any other checks that have confusing names or confusing error messages, please open an issue so we can address them.
@PCManticore Sorry, I think I was misunderstood, I definitely don't think there is some hidden agenda. I just meant that even if that is indeed a best practice (which it seems like, and I changed our code to be from a import b), the message still needs to be changed to be more understandable.
And now it seems like it is going to which I'm happy about :)
Most helpful comment
I agree. The warning here should indeed be 'consider-import-from'.