LibCST/libcst
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
..
_nodes [WIP] Support Parenthesized With Statements (#584) 2022-01-07 12:21:58 -08:00
_parser Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
codegen Remove tox references (#588) 2022-01-06 21:09:27 -05:00
codemod Codemod for PEP 484 Assign w / type comments -> PEP 526 AnnAssign (#594) 2022-01-12 09:34:01 -08:00
helpers Support relative imports in AddImportsVisitor. (#585) 2022-01-08 10:18:10 +00:00
matchers [WIP] Support Parenthesized With Statements (#584) 2022-01-07 12:21:58 -08:00
metadata Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
testing Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
tests Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
__init__.py Implement PEP-634 - Match statement (#568) 2021-12-30 10:00:51 +00:00
_add_slots.py Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
_batched_visitor.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_exceptions.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_flatten_sentinel.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_maybe_sentinel.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_metadata_dependent.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_position.py Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
_removal_sentinel.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_tabs.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_type_enforce.py Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
_typed_visitor.py [WIP] Support Parenthesized With Statements (#584) 2022-01-07 12:21:58 -08:00
_typed_visitor_base.py Fix all type errors (#579) 2022-01-05 18:13:01 +00:00
_types.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
_visitors.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00
py.typed support mypy using py.typed 2019-09-16 13:49:08 -07:00
tool.py Fix license headers (#560) 2021-12-28 11:55:18 +00:00