Comments (3)
We have an additional layer that we use internally which includes an add_needed_import
transform. I want to see it open-sourced (we're effectively using our internal repository as an incubator for all of this at this point) but we aren't satisfied with the code quality yet.
Effectively we are using a separate visitor to gather up all of the import nodes, and then calling visit() on the tree inside visit_Module. Note that across multiple visitors (not transforms, since they return a new tree) nodes are comparable via identity so you can stick nodes you care about in a list/set/dict and then do x in y
checks against it later. This gives us all of the existing imports we care about, which lets us add to an existing import or create a new one using code similar to what you've posted. Part of the reason this didn't make open-source is a lot of the visitor behavior for gathering up existing nodes will be overcome by scope analysis work that @jimmylai is working on. This should be coming in the next few weeks and will make its way into libcst.metadata.
Long term, one of the things I'd like to see in this repository is a library of transforms that do high-level actions such as adding/removing imports. Sort of a batteries-included set of actions which abstract automated modifications to the next level. I'm super excited about all of that but there's a ton of work that we need to get to before it becomes a reality.
from libcst.
For now, we're totally okay with opening issues for questions. Keep 'em coming!
from libcst.
this is super helpful, thank you @DragonMinded. once your team has the bandwidth, it could be great to have a spectrum channel (or some other place) for libcst questions/conversations.
from libcst.
Related Issues (20)
- ParserSyntaxError for "unmatched ']'" faults wrong location
- AttributeError: module 'libcst' has no attribute '_InverseOf'
- Issue with `whitespace_after_*` not fixable HOT 2
- MaybeSentinel not dataclass
- Implement PEP 696 (defaults for type params) HOT 3
- pyright + `__or__` in generic type annotations gives unknown type HOT 1
- Python 3.13.0b1: Segmentation fault when running tests HOT 2
- Indentation wart when removing an Arg from a Call - is whitespace being represented in the right place? HOT 3
- `ApplyTypeAnnotationsVisitor` does not respect `overwrite_existing_annotations` for attributes
- CST validation error on multiline match-case-if blocks
- Failing to build wheel HOT 1
- Accessing .code attribute fails with "TypeError: CSTNode._codegen() missing 1 required positional argument: 'state'" HOT 3
- Importable libcst.tests.pyre
- [support] running test suite doesnโt find libcst.native HOT 2
- Recursion depth exceeded when applying MetadataWrapper on a very long concatenated string HOT 2
- test_parse_error for native parser HOT 1
- [Windows, Mac, Linux in future] Can't use call_if_inside/call_if_not_inside matcher decorators with multiprocessing
- Warnings persist in Context HOT 2
- Link https://libcst.me/ in the docs? HOT 1
- No mute option for reels
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libcst.