Commit graph

1099 commits

Author SHA1 Message Date
Zach Hammer
8e6bf9e982 Just use plain Callable 2019-12-06 10:58:50 -08:00
Zach Hammer
6b696c2339 Replace "Any" with ... 2019-12-06 10:58:50 -08:00
Zach Hammer
6e19cc53ec Try removing nested TypeVar to fix pyre
from circleci pyre tests:

> libcst/_typed_visitor_base.py:15:0 Invalid type [31]: Expression `Variable[F (bound to typing.Callable[[libcst._typed_visitor.CSTTypedBaseFunctions, Variable[libcst._typed_visitor_base.T]], None])]` is not a valid type. Type variables cannot contain other type variables in their constraints.
2019-12-06 10:58:50 -08:00
Zach Hammer
70c7341225 Fix mark_no_op decorator type preservation on mypy
This seems to fix #161 for me locally, follows
https://mypy.readthedocs.io/en/latest/generics.html#declaring-decorators
2019-12-06 10:58:50 -08:00
Jennifer Taylor
9c6ccbb80e Bump LibCST to 0.2.5. 2019-12-05 16:36:09 -08:00
Jennifer Taylor
e4457425aa Implement a replace function.
This takes a tree, a matcher, and a replacement, replacing all nodes in the tree
which match the shape of the matcher with the replacement.
2019-12-05 14:11:22 -08:00
Jennifer Taylor
c9b10fe427 Deprecate "default_params" on Parameters in preparation for 3.8 position-only params. 2019-12-03 10:56:01 -08:00
Jimmy Lai
21e237c78b [metadata] add cache field to metadata wrapper 2019-11-22 10:07:46 -08:00
Jennifer Taylor
0717ffa097 Implement an extractall function.
This is equivalent to calling `findall` on a node, and then calling `extract` with the same
matcher on each of the returned nodes from `findall`.
2019-11-21 15:37:37 -08:00
Jennifer Taylor
79df3af5ce Make matcher type hint exception pickleable. 2019-11-20 16:29:46 -08:00
Jimmy Lai
e7315d2c28 remove unused import and run tox -e autofix 2019-11-19 16:09:26 -08:00
Jimmy Lai
337abb26c9 [metadata] consolidate MetadataWrapper methods to the same file 2019-11-19 12:46:37 -08:00
Jimmy Lai
0d77f653ec remove redundant code 2019-11-19 12:46:29 -08:00
Jennifer Taylor
8cd4a87391 Implement an `extract` function, which will extract child nodes/sequences/metadata given a successful match. 2019-11-19 12:06:10 -08:00
Jimmy Lai
b3fd4cf268 [metadata] fix typo and improve error message 2019-11-18 16:56:17 -08:00
Jennifer Taylor
64e298b2cc Fix batchable visitor not calling attribute visitors. 2019-11-14 11:28:15 -08:00
Jennifer Taylor
d1c2979b3a Remove length restrictions for AllOf/OneOf so they can be used with sequence expansion operators. 2019-11-14 11:27:46 -08:00
Jennifer Taylor
652ddca974 Bump LibCST to 0.2.4. 2019-11-13 17:56:09 -08:00
Jennifer Taylor
35b308e73b Fix not widening matcher sequence types inside unions. 2019-11-13 15:35:25 -08:00
Jennifer Taylor
ec7ee6f7d7 Bump LibCST to 0.2.3. 2019-11-12 10:58:28 -08:00
Thomas Mansencal
761c33f2dd Fix typo in "why_libcst.rst" file. 2019-11-09 17:58:45 -05:00
Thomas Mansencal
cda91dfaae Fix typo in "scope_tutorial.ipynb" file. 2019-11-09 17:57:34 -05:00
Jimmy Lai
f9628ca767 [metadata] add for loop support to ExpressionContext 2019-11-08 12:20:32 -05:00
Jennifer Taylor
f5079f6b39 Implement a MatchMetadataIfTrue matcher.
This is especially helpful for checking qualified names of nodes against one item
or a list of items that you wish to match against. I chose to create a new matcher
instead of widening the type of `MatchMetadata` to take in either a value or
a callable. I was originally going to do the former, but having a MatchIfTrue
and a MatchMetadataIfTrue felt more orthogonal and became easier to explain than
a single MatchIfTrue that could take two types of values.
2019-11-08 08:22:27 -08:00
Jennifer Taylor
675f8b3e82 Implement 'findall' in matcher API.
Findall does what you expect given a matcher and a tree: It returns all nodes
that exist in that tree which match the given matcher. For convenience, findall
works with regular LibCST trees as well as MetadataWrappers. It is also provided
as a helper method on the matcher transforms.
2019-10-31 17:16:29 -07:00
Jimmy Lai
e6b84fc11a [scope] add qualified name support for imported attribute 2019-10-31 13:08:36 -07:00
Jennifer Taylor
17fa6add8a Redo matcher codegen to be much faster and smaller output.
This does a few things, since it was easier to combine than separate:
 - Uses type aliases instead of fully-unrolled types, making it far easier for a human to read.
 - Makes codegen approximately 3x faster, which has the side effect of halving our test run times.
 - Consolidate the way we use type aliases by dropping the MetadataPredicate type for now, increasing consistency.
 - Widen types for AllOf/OneOf matchers to allow for MatchIfTrue since this is supported under the hood.

This results in a generated matchers file that is 1/3 the size it was, more readable by a human, and most importantly, faster to codegen, parse, format and typecheck.
2019-10-31 12:54:26 -07:00
Jennifer Taylor
75ebd2c3ed Fix string sanitization to not change things such as '"'. 2019-10-31 12:54:26 -07:00
Benjamin Woodruff
42c3f3f267 Add an unsafe_skip_copy option to MetadataWrapper
In certain cases (e.g. inside Instagram's lint framework) we know that
our tree originates from the parser, so we know that there shouldn't be
any duplicate nodes in our tree.

MetadataWrapper exists to copy the tree ensuring that there's no
duplicate nodes.

This diff provides an escape hatch on MetadataWrapper that allows us to
save a little time and avoid a copy when we know that it's safe to skip
the copy.

As part of this, I ran into some issues with `InitVar` and pyre, so I
removed `@dataclass` from the class. This means that this is techincally
a breaking change if someone depended on the MetadataWrapper being an
actual dataclass, but I think this is unlikely. I implemented `__repr__`
and added tests for hashing/equality behavior.
2019-10-29 13:35:38 -07:00
Jennifer Taylor
f889c0993d Update matchers docs with some clarification based on user feedback. 2019-10-28 18:13:55 -07:00
Jennifer Taylor
b5789d42ad Use official release of Black now that it is out.
We don't need to worry about formatting changes since we already paid the cost
in b3253de9b8. So, now that there's a new 3.8-compatible
Black, lets use it!
2019-10-28 18:11:17 -07:00
Benjamin Woodruff
7b3ae6f96a Rename CodegenPartial.get_*_node_code to get_*_statement_code
This should address @DragonMinded's review
https://github.com/Instagram/LibCST/pull/132#pullrequestreview-308173941
2019-10-28 17:23:26 -07:00
Benjamin Woodruff
88ac18872c Add a reentrant (incremental) codegen API
**Context:** This is an experimental performance optimization that we're
hoping to use for our internal linter at Instagram. I added some
documentation, but it's unsupported, and isn't very user-friendly.

This adds `ExperimentalReentrantCodegenProvider`, which tracks the
codegen's internal state (indentation level, character offsets,
encoding, etc.) and for each statement, it stores a `CodegenPartial`
object.

The `CodegenPartial` object has enough information about the previous
codegen pass to run the codegen on part of a tree and patch the result
back into the original module's string.

In cases where we need to generate a bunch of small independent patches
for the same file (and we can't just generate a new tree with each patch
applied), this *should* be a faster alternative.

I don't have any performance numbers because I still need to test this
end-to-end with our internal codebase, but I'd be shocked if it was
slower than what we're doing.

This could theoretically live outside of LibCST, but it depends on a
whole bunch of LibCST internals, so there's some value in making sure
that this is in sync with the rest of LibCST.
2019-10-28 17:23:26 -07:00
Benjamin Woodruff
c10a960fc9 Rename before_visit/after_leave to before_codegen/after_codegen
This addresses @DragonMinded's review here:
https://github.com/Instagram/LibCST/pull/126#pullrequestreview-305577895
2019-10-28 11:05:35 -07:00
Benjamin Woodruff
3bfbb4b2dd Make the codegen enter/leave tracking more generic
I need to do some additional work on visit/leave to make codegen
re-entrant, so this makes it more generic.

This should have an additional small positive effect of creating less
throwaway objects when we're doing codegen without position calculation.
2019-10-28 11:05:35 -07:00
Jennifer Taylor
522f7c9a67 Add CircleCI config for Python 3.8. 2019-10-24 15:25:21 -07:00
Jennifer Taylor
0f25f5ce2c Implement parsing of the walrus operator.
This hooks the operator up to a parser, and adds it into appropriate spots in the grammar given 3.8's official grammar specification.
2019-10-24 15:25:21 -07:00
Jennifer Taylor
c10619ff31 Implement the walrus operator node as NamedExpr.
This implements the node, all codegen for using it and tests for construction.
This does not handle parsing, which will come in a later diff.
2019-10-24 15:25:21 -07:00
Jennifer Taylor
42499a0863 Hook up 3.8 to various tox environments. 2019-10-24 15:25:21 -07:00
Jennifer Taylor
b3253de9b8 Update black to git commit that fixes 3.8 support. 2019-10-24 15:25:21 -07:00
Jennifer Taylor
7703d4b93d Add bugfix grammar changes for Python 3.8. 2019-10-24 15:25:21 -07:00
jimmylai
cb2b94f113 Deprecated BasicPositionProvider and SyntacticPositionProvider. Replaced with new name WhitespaceInclusivePositionProvider and PositionProvider. 2019-10-24 15:01:39 -07:00
jimmylai
fe86b0588d Update CHANGELOG.md 2019-10-24 15:01:39 -07:00
Jennifer Taylor
0391905bde Bump LibCST to 0.2.2. 2019-10-24 15:01:39 -07:00
Jennifer Taylor
ff65afaa26 Add a best practices section with various recommended practices.
There are going to be many more where this comes from, but these are the three cases that have come up most frequently, so I started with these. Hopefully this helps give additional direction to people using LibCST.
2019-10-23 16:50:47 -07:00
Jimmy Lai
ba54bb0075 add node to all LocalScope 2019-10-23 14:15:08 -07:00
Jimmy Lai
8d66bbdbbc [scope] add node to FunctionScope and ClassScope 2019-10-23 14:15:08 -07:00
Jennifer Taylor
bb72a0556b Fix deep_clone for sequence types.
It turns out that when we had a sequence type, like Module's body, we were incorrectly
leaning on copy.deepcopy for the rest of the tree. This works, but its likely to blow
the stack when doing a deep clone of a very very large file. Fix that oversight here.
2019-10-23 12:19:09 -07:00
Jimmy Lai
b546356604 [doc] improve documentation of Scope.__getitem__ 2019-10-22 17:56:42 -07:00
Jennifer Taylor
f492103ad2 More explicit fuzz testing environments.
Let's split from just fuzzing on the default python to having a separate fuzzer for each version of python we support. This should help us catch any issues should they arise in differences with grammars across releases. Right now we're fuzz-clean as well, so I also bumped up the thresholds to stress test LibCST more. I anticipate that this will be useful when we begin work on 3.8 support.
2019-10-22 17:24:03 -07:00