-
Notifications
You must be signed in to change notification settings - Fork 18
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
[FEATURE] Support for PEP 484
Redundant Module/Symbol Aliases
#149
Comments
Hi @adam-grant-hendry, first of all, I'm happy to see you around and supporting the project. Second, this feature request is accepted and will be added in the next release (perhaps today). Third, thanks for providing a regex, but Pycln is based on code AST analysis instead of regex, so now I'm trying to find a way to parse the AST of |
@hadialqattan Thank you for your support!
That's wonderful! It would be great if it could be added today! Congratulations! 🎉
No problem at all. A regex was simply my first thought. I would be happy, personally, however it is implemented! Thank you and your team for this wonderful package! Question: How do you intend the feature will work? Will it be automatic, or will the user have to set a certain flag on the command line? |
I think this should be automatic just like what pycln does with |
BTW, I'm not really familiar with |
That should be true, yes. AFAIK, the redundant nature ( I believe it is safe to say that the number of redundant imports ( For any possible edge cases, you might need an argument like
that always removes a named import regardless. As a bonus, such an argument might be double-useful for authors who wish to purposefully remove a used import everywhere it appears where they want to force contributors to use a specific package. However, honestly, I personally think the author could manually remove the import in that instance and adding an extra CLI argument like |
As an alternative, for backwards compatibility, you could optionally add a CLI argument
so users can opt-in or opt-out of the feature. |
Lastly, I think it is VERY IMPORTANT to add a small bit of documentation explaining the new behavior.
It could be as simple as saying
|
I see your point, but I think that PEP484 is old enough to be the default behavior without an opt-out arg. Moreover, this feature is totally safe because it's about skipping certain types of imports (not removing them) which can't break users' programs. |
Also, for simplicity, I believe that CLI tools should be default-first instead of having too many CLI arg that users should use. |
For sure. Thanks a lot for the suggested note ❤️. |
@adam-grant-hendry if you don't mind, could you test the master branch before creating a new release? |
@hadialqattan Completely agree with you on all points. Thank you so much for this! I can test early tomorrow as I am spending time with family right now for the 4th of July. To clarify, would you like me to write unit tests or just confirm it works on my machine? |
Happy Treason Day!
Only confirming it works on your machine the way you meant it to be. |
I'll get started on this right now 😃 |
Bro plz don't bother yourself thinking of every possible edge case, I mean you could test it only against the file(s) that you wanted to cleanup so you opened this issue 🌚 |
😆 Thank bro! I ran the |
@hadialqattan Please bump up the version~ 🎉 🥳 |
v2.0.1 has been released 🎉! |
Is your feature request related to a problem? Please describe.
PEP 484 states
Pyright calls
import A as A
a redundant module aliasfrom X import A as A
a redundant symbol aliasThese imports are needed, but not actually used:
PEP 484
, to allow static type checkers to see the module's contents, andThese typically occur in
__init__.py
and__init__.pyi
files, but I don't want to ignore these files entirely. I would also like to runpycln
on them to remove old-form uppercasetyping
module types (e.g.List
,Optional
,Dict
, etc.) after runningpyupgrade
and to find genuinely unused imports.Currently, the
pycln
CLI arguments--exclude
and--extend-exclude
only accept files and path regex's, not regex's matching specific lines in modules.To have
pycln
ignore these aliases, one currently must add a# nopycln: import
to each import in the project that is being exported, which is quite tedious.Describe the solution you'd like. A clear and concise description of what you want to happen.
Since
PEP 484
redundant aliases have a specific form, it would be nice to have a regex feature to exclude imports of a specific form; i.e. those matching the patternsimport A as A
andfrom X import A as A
A backreference could be used in a regular expression, like so:
and
respectively.
Since the module/symbol name is being repeated (
A
) instead of renamed, it is clear these are intended for the purpose of exporting (as opposed to, say,import numpy as np
orfrom matplotlib import pyplot as plt
), so there is minimal risk of accidentally skipping an actually unused import.Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
N/A
Additional context Add any other context or screenshots about the feature request here.
N/A
The text was updated successfully, but these errors were encountered: