Commit graph

1099 commits

Author SHA1 Message Date
Zsolt Dollenstein
c5b073599d
fix typo in changelog 2022-01-28 11:22:49 +00:00
Zsolt Dollenstein
e0744c7d14
bump version to 0.4.1 2022-01-28 10:44:12 +00:00
Zsolt Dollenstein
dba8296e4b
[native] Return tuples instead of lists in CST (#631) 2022-01-28 10:34:23 +00:00
Zsolt Dollenstein
8ed3a9cd5c
[native] Box most enums (#632)
* Box most enums

* add big nested expression as fixture
2022-01-28 10:33:33 +00:00
Zsolt Dollenstein
3bd8c68207
[ci] build wheels on main branch for linux/arm64 (#630) 2022-01-28 10:31:53 +00:00
Steven Troxler
595d8c3948
Add support for methods with func type comment excluding self/cls (#622)
* Add support for methods with func type comment excluding self/cls

PEP 484 doesn't really specify carefully how function type
comments should work on methods, but since usually the type of
`self` / `cls` is automatic, most use cases choose to only annotate
the other arguments.

As a result, this commit modifies our codemod so that non-static
methods can specify either all the arguments, or all but one of
them. We'll correctly zip together the inline and func-type-comment
types either way, typically getting no type for `cls` or `self`.

We accomplish this by using matchers to trigger the visit
method for FunctionDef rather than using visit_FunctionDef, which gives
us enough context to determine when a function def is a regular
function versus a method (plus also matching the decorators against
`@staticmethod`, so that we trigger the normal function logic in
that case).

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
2022-01-25 09:37:36 -08:00
Batuhan Taskaya
5b6b19af84
Proxy both parentheses in some pattern matching nodes (#626) 2022-01-25 11:02:08 +00:00
Batuhan Taskaya
68780fd6b2
Don't require whitespace right after match (#628) 2022-01-24 09:20:44 -08:00
Batuhan Taskaya
2345848d4a
[native] Allow unparenthesized tuples inside f-strings (#621) 2022-01-23 09:10:47 -08:00
Batuhan Taskaya
2b2b25bb08
Don't redundantly nest StarredElement inside another Element (#624) 2022-01-23 05:45:27 -08:00
Steven Troxler
d35b6a54e5
Do not traverse lambdas - they have no Params (#617) 2022-01-22 11:25:16 +00:00
Steven Troxler
e459e60628
Handle ast.parse failures when converting function type comments (#616)
* Handle syntax errors in the ast parse function.

If we encounter a syntax error in either the type comment extraction
or the type comment parsing stages, ignore type information on that
cst node.

* Quote the FunctionType type, which does not exist in Python x3.6
2022-01-21 08:13:35 -08:00
Arie Bovenberg
5a1220097d
[ci] add slotscheck (#615)
Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
2022-01-20 18:19:33 +00:00
Zsolt Dollenstein
ceac7fca4d
specify minimum rust toolchain version (#614) 2022-01-19 19:31:48 +00:00
Zsolt Dollenstein
f2c7cfe00a
[ci] Cache rust build artifacts (#606)
* use Swatinem/rust-cache to cache rust build artifacts

* use rust cache in cibuildwheel on linux
2022-01-19 16:27:39 +00:00
Steven Troxler
9563b4a9d5
Support FunctionDef transformations (#610)
I've tested all of the edge cases I know of: type comments in various
locations, non-type-comments, arity mismatches where we should skip,
etc.

Assuming that all type comments parse, this should work as far as I
know. I'll make a separate PR to deal with SyntaxErrors when parsing
types, because that is cross-cutting and not specific to FunctionDef.
2022-01-18 07:20:21 -08:00
Zsolt Dollenstein
0c509b3f43
Stop indentation checking at EOF (#611) 2022-01-18 08:47:42 +00:00
Steven Troxler
332710ddc0
Add support for For and With (#607) 2022-01-17 06:58:01 -08:00
Steven Troxler
91212cd6d7
Full handling for applying type comments to Assign (#599)
* Add full support type comment -> PEP 526 conversion

Summary:

In the previous PR, I added basic support for converting an
Assign with a type comment to an AnnAssign, as long as there was
only one target.

This PR handles all fully PEP 484 compliant cases:
- multiple assignments
- multiple elements in the LHS l-value

We cannot handle arity errors because there's no way to do it. And
we don't try to handle the ambiguous case of multiple assignments with
mismatched arities (PEP 484 isn't super clear on which LHS is supposed
to pick up the type, we are conservative here). The ambiguous case is
probably very uncommon in real code anyway, multiple assignment is not
a widely used feature.

Test Plan:

There are new test cases covering:
- multiple elements in the LHS
- multiple assignment
- both of the above together
- semicolon expansion, which is handled differently in the cases
  where we have to add type declarations
- new error cases:
  - mismatched arity in both directions on one assignment
  - mismatched arity in multiple assignment
```
> python -m unittest libcst.codemod.commands.tests.test_convert_type_comments
.....
----------------------------------------------------------------------
Ran 5 tests in 0.150s

OK
```
2022-01-16 11:34:25 -08:00
Sehyo Chang
cafbfac150
change pyo3 as optional dependency in native Python Parser (#598) 2022-01-16 18:46:54 +00:00
Arie Bovenberg
bd5ede7953
add slots to base classes, @add_slots takes bases into account (#605)
* add slots to base classes, @add_slots takes bases into account
* state changes in apache 2.0 licensed add_slots
2022-01-16 14:14:32 +00:00
Zsolt Dollenstein
c7d76c65f3
Add docs about the native parts (#601)
Co-authored-by: Steven Troxler <steven.troxler@gmail.com>
2022-01-15 12:42:42 +00:00
Martin DeMello
e03ed43be8
Merge in TypeVars and Generic base classes in ApplyTypeAnnotationVisitor (#596)
* Tracks TypeVars that are used in type annotations in the pyi file, and
  adds their Assign statements to the merged file.
* Adds Generic[T] as a base class if needed.
2022-01-14 11:39:18 -08:00
Zsolt Dollenstein
5f22b6c438
bump version to 0.4.0 2022-01-12 19:51:51 +00:00
Steven Troxler
122627cabc
Codemod for PEP 484 Assign w / type comments -> PEP 526 AnnAssign (#594)
* Codemod for PEP 484 Assign w / type comments -> PEP 526 AnnAssign

Summary:

This codemod is intended to eventually handle all type comments from
PEP 484. This is a partial implementation specifically handling
assignment type comments, which as of PEP 526 are better dealt
with using AnnAssign nodes.

There is more work to do because there are two other kinds of
comments to support: function heading comments and function parameter
inline comments. But the PEP 526 functionality is complete so I feel
like it's worth havign a PR / CI signals / code review at this stage.

Test Plan:

```
python -m unittest libcst.codemod.commands.tests.test_convert_type_comments
```

* Disable on python 3.6, 3.7

The ast module didn't get the `type_comment` information we need
until python 3.8.

It is possible but not a priority right now to enable 3.6 and 3.7
via the typed_ast library, for now I just throw a NotImplementedError
with a nice description. There's a note in the code about where to look
for a typed_ast example in case anyone wants to add support in the
future.

* Fix type errors on the 3.8+ testing fix

* Do a better job of complaining on Python < 3.8

* Updates based on code review

Summary:

Do not strip type comments in the visitor pattern; instead,
reach down from the parent to do it because this makes it
much more reliable that we won't accidentally remove
other comments in a codemod (using visitor state to do this
isn't really feasible once we handle complex statements like
FunctionDef, With, For).

Handle multi-statement statement lines; this works since the
trailing whitespace can only apply to the final statement on
the line. It's not really a critical edge case to handle, but
the code is no more complicated so we might as well.

* Prevent comment stripping for multi-assign

* Note in the docstring that this is a limited WIP

* Reorder checks so the next step will be cleaner
2022-01-12 09:34:01 -08:00
Martin DeMello
31ba5bf583
Remove unused argument to handle_Index (#595) 2022-01-11 16:59:46 -08:00
Martin DeMello
8a7c13ff36
Use precise signature matching when inserting function type annotations (#591)
* Use precise signature matching when inserting function type annotations

* add type annotations

* Add an argument for strict annotation matching.

* don't use Any
2022-01-10 12:41:21 -08:00
Steven Troxler
1937fbf47d
Add a Building subsection to the Developing docs (#593)
* Add a build section to the docs

* Tweaks based on code review
2022-01-09 08:32:13 -08:00
Martin DeMello
8652974d87
Support relative imports in AddImportsVisitor. (#585)
* Support relative imports in AddImportsVisitor.

* Adds an Import dataclass to represent a single imported object
* Refactors AddImportsVisitor to pass around Import objects
* Separates out the main logic in get_absolute_module_for_import so that
  it can be used to resolve relative module names outside of a cst.Import
  node
* Resolves relative module names in AddImportsVisitor if we have a
  current module name set.

Fixes #578
2022-01-08 10:18:10 +00:00
Steven Troxler
1337022770
[WIP] Support Parenthesized With Statements (#584)
On the python side, we can add parentheses from MaybeSentinel.DEFAULT if the whitespace requires it.

On the rust side, we support the new grammar but codegen will only add explicitly included parentheses for now - it should be possible to match python behavior but it's not urgent so I've left a TODO
2022-01-07 12:21:58 -08:00
Luke Petre
9f6ff017f2
Use pyre site-package feature (#589)
* Use pyre site-package feature
* Update readme, remove example
2022-01-07 07:34:50 -05:00
Steven Troxler
2f75246c3a
Fix variable name in lambda (#590) 2022-01-07 09:29:51 +00:00
Luke Petre
6434ca8512
Remove tox references (#588) 2022-01-06 21:09:27 -05:00
Luke Petre
2f7f174daa
Remove setup matrix (#586) 2022-01-06 11:26:12 -05:00
Zsolt Dollenstein
6615ccb0ce
pin to git rev (#587) 2022-01-06 15:56:14 +00:00
Steven Troxler
3578f2fc3d
Add some color to children-vs-codegen error (#583)
It took me some time to track down the root cause of `children`
not matching codegen, having the error message directly hint that
visit and codegen are probably mimatched (my visit was running
out-of-order) will likely help newbies get going faster.
2022-01-05 21:26:51 +00: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
d9a1dc8473
Fix all type errors (#579)
* bump pyre version
* make sure CI-pyre uses working copy
* remove unused pyre suppressions
* suppress invalid decorations
* fix undefined attributes
* fix missing return annotations
* fix tuple concatenation issues
* add native stubs
* fix invalid typing of **kwargs in test_apply_type_annotations
* only install pyre on non-windows
* update test fixture to reflect changes in recent pyre versions
* suppress errors related to mismatched positions
2022-01-05 18:13:01 +00:00
Zsolt Dollenstein
73ecdf45c3
only run CI on pushes to main and pull requests (#581) 2022-01-05 17:09:30 +00:00
Zsolt Dollenstein
b939bf2998
run tests with 3.10 too (#577) 2022-01-05 15:43:43 +00:00
Zsolt Dollenstein
86431eea89
Make sure dedents are emitted for inputs without trailing newlines (#573) 2022-01-04 20:04:21 +00:00
Zsolt Dollenstein
cff47b767b
fix python version in pypi upload step (#576) 2022-01-04 20:03:59 +00:00
Zsolt Dollenstein
b5fb9d79c5
Fix pypi upload CI step (#570)
* pass in LIBCST_NO_LOCAL_SCHEME to CIBW

* only build a source tarball on one platform

* factor out upload into a separate job
2022-01-04 17:01:09 +00:00
Zsolt Dollenstein
9932a6d339
Implement PEP-634 - Match statement (#568)
* ParenthesizedNode implementation for Box

* match statement rust CST and grammar

* match statement python CST and docs

* run rust unit tests in release mode for now
2021-12-30 10:00:51 +00:00
Zsolt Dollenstein
67db03915d
implement PEP-654: except* (#571) 2021-12-29 21:23:46 +00: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
9c13ca5f9c
pin checkout action to v1 (#569) 2021-12-23 11:51:41 -05:00
Steven Troxler
1d1b7da05f
.gitignore shared-object built artifacts (#567) 2021-12-22 20:56:12 +00:00
John Reese
10c3aa09a7
Upgrade to µsort 1.0.0rc1, and apply formatting changes (#565)
* Upgrade to usort==1.0.0rc1

* Apply sorting changes from usort 1.0.0rc1

* reapply codegen

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
2021-12-21 14:55:04 -08: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