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

Update imports with autofix #170

Closed
ljodal opened this issue Jan 15, 2021 · 3 comments
Closed

Update imports with autofix #170

ljodal opened this issue Jan 15, 2021 · 3 comments

Comments

@ljodal
Copy link

ljodal commented Jan 15, 2021

Hi, I'm looking into creating a rule telling users not to use a certain base class, but rather use a different one. Providing an autofix for this would require changing an import, which I can't figure out how to do. Is this currently possible?

For example I'd like to replace this:

from a import A

class MyClass(A):
    pass

with this:

from b import B

class MyClass(B):
    pass

I see that the NoNamedTupleRule leaves old imports in place and does not add an import for dataclasses, so I'm half assuming this isn't possible at the moment. Any plans to add support for this? Let me know if there's anything I can do to help :)

@ljodal ljodal changed the title Update imports on Autofix Update imports with autofix Jan 15, 2021
@jmhodges-color
Copy link

jmhodges-color commented Feb 18, 2023

Would love to hear if there's a way to do this! I could totally guess that I missed something obvious in the API, but AddImportsVisitor in libcst is so handy and doesn't have a trivial mapping, it seems.

@zsol
Copy link
Member

zsol commented Feb 19, 2023

I don't think this is possible at the moment, but agreed would be very useful. Unfortunately this feature would require rethinking how the replacement API works (currently you can only declare autofixes in the node you're visiting). Or at least adding a special case for imports. @amyreese what do you think?

@amyreese
Copy link
Member

I believe this should be possible in the upcoming 2.0 release, but would require a separate call to self.report(…) and providing a separate target node and replacement. I opened #356 to track the more general idea of offering multiple replacements in a single report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants