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

Better messaging for useless-import-alias and from imports #2309

Closed
heoga opened this issue Jul 17, 2018 · 7 comments · Fixed by #4491
Closed

Better messaging for useless-import-alias and from imports #2309

heoga opened this issue Jul 17, 2018 · 7 comments · Fixed by #4491
Assignees
Labels
Enhancement ✨ Improvement to a component
Milestone

Comments

@heoga
Copy link

heoga commented Jul 17, 2018

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.

@PCManticore
Copy link
Contributor

You might as well do from django.db import models.

@heoga
Copy link
Author

heoga commented Jul 17, 2018

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,

@guidovansteen
Copy link

I agree. The warning here should indeed be 'consider-import-from'.

@Sveder
Copy link

Sveder commented Aug 6, 2018

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.

@PCManticore
Copy link
Contributor

PCManticore commented Aug 8, 2018

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 PCManticore reopened this Aug 8, 2018
@PCManticore PCManticore added the Enhancement ✨ Improvement to a component label Aug 8, 2018
@PCManticore PCManticore changed the title False positive with useless-import-alias Better messaging for useless-import-alias and from imports Aug 8, 2018
@Sveder
Copy link

Sveder commented Aug 8, 2018

@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 :)

@Pierre-Sassoulas
Copy link
Member

@sushobhit27 @yushao2 implemented a fix for this in #4491, you can check it if you want as I see you assigned yourself to this issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
Development

Successfully merging a pull request may close this issue.

6 participants