Commit graph

46 commits

Author SHA1 Message Date
zaicruvoir1rominet
ca1f81f049
Avoid raising bare Exception (#1168)
* Keep old exception messages (avoid breaking-changes for users relying on exception messages)

* Move ``get_expected_str`` out of _exceptions.py, where it does not belong, to its own file in _parser/_parsing_check.py
2025-06-07 01:53:44 -07:00
Zsolt Dollenstein
3dc2289bf6
codegen: Support pipe syntax for Union types (#1336)
From 3.14 onwards, we'll get `foo | bar` instead of `typing.Union[foo, bar]` as the annotation for union types (including optional). This PR prepares the codegen script for this.
2025-05-26 08:40:54 +01:00
Zsolt Dollenstein
2ffca10845
remove typing dependencies (#1126)
Summary:
This PR removes the `typing_extensions` and `typing_inspect` dependencies as we can now rely on the built-in `typing` module since Python 3.9.

Test Plan:
existing tests
2024-04-03 19:50:14 +01:00
Sigurd Ljødal
a594fe1dd2
Fix type of evaluated_value on string to allow bytes (#721)
* Fix type of evaluated_value on string

This can return bytes if the string is a bytestring, e.g.:

    In [1]: import libcst as cst

    In [2]: cst.parse_expression('b"foo"').evaluated_value
    Out[2]: b'foo'

* Fix type errors from changed signature
2023-05-26 13:43:05 +01:00
Aarni Koskela
ae42deed9b
Ensure current Python interpreter is used for subprocesses (#898) 2023-03-27 10:56:25 +01:00
Aarni Koskela
9381fee9ab
Use subprocess.DEVNULL instead of opening os.devnull by hand (#897) 2023-03-24 10:03:11 +00:00
MapleCCC
973895a6c0
Several trivial refactors (#770)
* Enumeration members are singletons. Copying on them would be no-op

* Avoid generating unnecessary `pass` statement

* Several trivial refactor

* Avoid building unnecessary intermediate lists, which are mere slight waste of time and space

* Remove unused import, an overlook from commit 8e6bf9e9

* `collections.abc.Mapping.get()` defaults to return `None` when key doesn't exist

* Just use unittest's `assertRaises` to specify expected exception types, instead of catching every possible `Exception`s, which could suppress legitimate errors and hide bugs

* We know for sure that the body of `CSTTypedTransformerFunctions` won't be empty, so don't bother with complex formal completeness
2022-09-14 14:33:45 +01:00
Luke Petre
6434ca8512
Remove tox references (#588) 2022-01-06 21:09:27 -05:00
Steven Troxler
601db54880
Add instructions to codegen test failures (#582)
* Add instructions to codegen test failures

* Run ufmt

* Fix lint errors, mention tox -e codegen
2022-01-05 12:24:31 -08:00
Zsolt Dollenstein
c44ff0500b
Fix license headers (#560)
* Facebook -> Meta

* remove year from doc copyright
2021-12-28 11:55:18 +00:00
Zsolt Dollenstein
c02de9b718
Implement a Python PEG parser in Rust (#566)
This massive PR implements an alternative Python parser that will allow LibCST to parse Python 3.10's new grammar features. The parser is implemented in Rust, but it's turned off by default through the `LIBCST_PARSER_TYPE` environment variable. Set it to `native` to enable. The PR also enables new CI steps that test just the Rust parser, as well as steps that produce binary wheels for a variety of CPython versions and platforms.

Note: this PR aims to be roughly feature-equivalent to the main branch, so it doesn't include new 3.10 syntax features. That will be addressed as a follow-up PR.

The new parser is implemented in the `native/` directory, and is organized into two rust crates: `libcst_derive` contains some macros to facilitate various features of CST nodes, and `libcst` contains the `parser` itself (including the Python grammar), a `tokenizer` implementation by @bgw, and a very basic representation of CST `nodes`. Parsing is done by
1. **tokenizing** the input utf-8 string (bytes are not supported at the Rust layer, they are converted to utf-8 strings by the python wrapper)
2. running the **PEG parser** on the tokenized input, which also captures certain anchor tokens in the resulting syntax tree
3. using the anchor tokens to **inflate** the syntax tree into a proper CST

Co-authored-by: Benjamin Woodruff <github@benjam.info>
2021-12-21 18:14:39 +00:00
Luke Petre
a20d43e7e6
Rewrite the MatchIfTrue type to be generic on _MatchIfTrueT (#512) 2021-09-21 09:53:17 +01:00
Steven Troxler
5e1e3fe970
The ufmt tool combines usort and black with a consistent wrapper, (#515)
which ensures we won't have inconsistent black-vs-isort errors
going forward. We can always format by running `ufmt format .`
at the root, and check with `ufmt check .` in our CI actions.
2021-08-25 20:39:29 -04:00
Luke Petre
87f0becd01
Change codegen to treat typing.Union[Foo, NoneType] and typing.Optional[Foo] as the same (#508) 2021-08-11 09:48:30 -04:00
Caleb Donovick
0ee0831eb6
Add FlattenSentinel to support replacing a statement with multiple statements (#455)
* Add flatten_sentinal

* Add FlattenSentinal to __all__

* Fix lint errors

* Fix type errors

* Update test to use leave_Return

* Update and run codegen

* Add empty test

* Update docs

* autofix
2021-03-22 23:23:40 -07:00
Batuhan Taskaya
6ae25834bb
Implement TypeOf matcher (#384)
* Implement TypeOf matcher

* Satisfy the type checker

* Expand the test case

* Fix the annotation of _raw_options

* Add documentation...
2020-09-10 11:09:59 -07:00
Jimmy Lai
7ca738bf39
Upgrade dev tools (Black/Flake8/isort) and read install requirements from requirements.txt (#380)
* Read install requirements from requirements.txt

* read extras_require from requirements-dev.txt

* add requirements-dev.txt to MANIFEST.in

* apply fixes for new version of Black and Flake8

* don't upgrade Pyre

* re-format
2020-08-31 10:44:55 -07:00
jimmylai
c023fa7c4c
[typing] enable Pyre strict mode by default (#313)
Co-authored-by: Jimmy Lai <jimmylai@fb.com>
2020-06-12 18:24:18 -07:00
Jennifer Taylor
c5d7861ed5 Add "evaluated_value" support to integer types. 2020-01-30 17:07:21 -08:00
Jennifer Taylor
40e19ac457 Remove deprecated ExtSlice. 2020-01-08 17:18:19 -08:00
Jimmy Lai
073a5588d8 Upgrade pyre-check version 2019-12-17 14:27:53 -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
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
8cd4a87391 Implement an `extract` function, which will extract child nodes/sequences/metadata given a successful match. 2019-11-19 12:06:10 -08:00
Jennifer Taylor
35b308e73b Fix not widening matcher sequence types inside unions. 2019-11-13 15:35:25 -08: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
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
Jennifer Taylor
b3253de9b8 Update black to git commit that fixes 3.8 support. 2019-10-24 15:25:21 -07:00
Jennifer Taylor
e74ff11528 Implement base version of matchers + metadata
This adds the ability to match on metadata for a node as if it was an attribute on the node. It also opens up the ability to match on node attributes via metadata only. It allows for metadata match specifications with similar flexibility to standard matchers.
2019-10-21 13:25:20 -07:00
Jennifer Taylor
48ba3b0849 Remove parsing Subscript slice directly into Index/Slice.
This makes sure we always wrap elements in a SubscriptElement, even when there
is only one element. This makes things more regular while still being backwards
compatible with existing creation. The meat of this is in two halves, which can't
be split due to not wanting to break the build between commits. The first half
is just the changes to the parser and updates to tests. This includes a test to
be sure we can still render code that uses old construction types. The second half
is changes to codegen which made assumptions about `Subscript` and demonstrates
the need to make this change in the first place. This includes a fix to
`CSTNode.with_deep_changes` type to make it more correct and also more usable in
transforms without additional type assertions.
2019-10-18 13:40:10 -07:00
Jennifer Taylor
e533e6ae73 Rename ExtSlice to SubscriptElement in anticipation of further fitxes.
This is somewhat complicated by the fact that we need to not just allow
construction of nodes/matchers using `ExtSlice` still for backwards compatibility,
but we also need to be able to call `visit_ExtSlice` and `leave_ExtSlice` on
old visitors even though the new node is named `SubscriptElement`. The
construction/instance check/matching side of things will work since internally we
refer to everything as `SubscriptElement` and alias `ExtSlice` to this everywhere,
but for string-based function lookup, we need to get a little more clever and make
the default `visit_SubscriptElement` delegate onward to `visit_ExtSlice` so that
either form works.

This can all be removed again once we're past the deprecation period for ExtSlice.
2019-10-16 16:00:27 -07:00
Jennifer Taylor
1dd85d3c52 Refactor matcher class codegen to use common gather functions. 2019-10-16 16:00:27 -07:00
Jennifer Taylor
704fe2bc3a Codegen valid return types mapping for each CSTNode class. 2019-10-16 12:48:20 -07:00
Jennifer Taylor
ba471909c4 Generalize codegen cleanup step and apply it to all codegen.
I noticed that the typed visitors codegen was creating messy types such as Union[SingleType]. We have a clean-up for Union[SingleType] snuck into gen_matcher_classes. So, lets remove that snuck-in clean-up from gen_matcher_classes and apply it globally to all codegen. This makes its purpose a lot more obvious, while helping decouple necessary codegen from prettifying/simplifying transforms. It also cleans up typed visitors, so that's a bonus.
2019-10-11 12:29:23 -07:00
Jennifer Taylor
76dbfd65b9 Explicitly define hash and equality for matchers.
This makes matcher equality and hash equivalent to the way LibCST nodes behave. Not only does this make us more consistent, but it also fixes a bug where matcher decorators could not be used with a matcher that initialized a sequence type as a list.
2019-10-11 11:46:06 -07:00
Jennifer Taylor
4948c6a5f5 Add missing license header to matchers generator file. 2019-10-09 21:51:56 -07:00
Jennifer Taylor
27b5a1d286 Improve removing nodes from parents in LibCST.
Add a RemoveFromParent() function as a convenience to returning RemovalSentinel.REMOVE.
Introduce a `deep_remove()` on CSTNode analogous to `deep_replace()` but for removing.
2019-10-02 15:43:48 -07:00
Jennifer Taylor
271566bdbf Add a .remove() method to CSTNode as a convenience to returning RemovalSentinel.REMOVE 2019-10-01 15:28:58 -07:00
Jennifer Taylor
85417b957d Initial export of matchers into LibCST. 2019-09-27 11:39:11 -07:00
Jennifer Taylor
956460617e Codegen attribute visitors. 2019-08-28 13:28:29 -07:00
Jennifer Taylor
0da658d183 Calculate necessity of MaybeSentinel/RemovalSentinel based on usage.
There are a lot of nodes that cannot be removed or converted to maybes, such as
most of the Op tokens. It would be a bit of a lie to codegen leave_* methods
that allow these nodes to be converted, only to throw a runtime error later. So,
upgrade the codegen to allow us to see whether certain nodes are used in conjunction
with a MaybeSentinel/None, or inside a Sequence, to inform ourselves as to when to
allow MaybeSentinel or RemovalSentinel.
2019-08-28 13:28:29 -07:00
Jennifer Taylor
90e39ca4fa Add a tox environment for running codegen. 2019-08-28 13:28:29 -07:00
Jennifer Taylor
f9437f42b4 Add unit test to verify that codegen is clean.
We want to make sure that the generated function stubs stay in sync with
the node definitions. So, make a unit test that fails if codegen generates
a different file than the existing file, so that somebody modifying code
knows they need to re-run codegen.
2019-08-28 13:28:29 -07:00
Jennifer Taylor
f14b4fa37c Add codegen for visit_* and leave_* methods on CSTTransform and CSTVisitor.
This allows Pyre to typecheck returns inside visitors for us.
2019-08-28 13:28:29 -07:00