Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@stroxler
Copy link
Contributor

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.

NOTE: my local pyre is complaining and I don't understand it, putting
a PR up anyway to see what CI does.

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

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.

NOTE: my local pyre is complaining and I don't understand it, putting
a PR up anyway to see what CI does.

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
```
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 12, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 12, 2022

Codecov Report

Merging #599 (cc31ad1) into main (5f22b6c) will increase coverage by 0.05%.
The diff coverage is 97.59%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #599      +/-   ##
==========================================
+ Coverage   94.72%   94.78%   +0.05%     
==========================================
  Files         244      245       +1     
  Lines       24916    25101     +185     
==========================================
+ Hits        23602    23791     +189     
+ Misses       1314     1310       -4     
Impacted Files Coverage Δ
libcst/codemod/commands/convert_type_comments.py 97.02% <96.92%> (+3.02%) ⬆️
...demod/commands/tests/test_convert_type_comments.py 94.28% <100.00%> (+2.98%) ⬆️
libcst/tests/test_batched_visitor.py 100.00% <0.00%> (ø)
...emod/visitors/tests/test_apply_type_annotations.py 100.00% <0.00%> (ø)
libcst/tests/test_add_slots.py 100.00% <0.00%> (ø)
libcst/_nodes/whitespace.py 98.94% <0.00%> (+0.01%) ⬆️
libcst/_nodes/statement.py 95.15% <0.00%> (+0.01%) ⬆️
libcst/_nodes/expression.py 96.78% <0.00%> (+0.03%) ⬆️
libcst/_nodes/op.py 95.73% <0.00%> (+0.07%) ⬆️
libcst/_nodes/base.py 95.48% <0.00%> (+0.07%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5f22b6c...cc31ad1. Read the comment docs.

@stroxler
Copy link
Contributor Author

Looks like I'll need to bump the required python version or find a 3.8 substitute for ast.unparse

@stroxler stroxler force-pushed the convert-type-comments--pep-526-flattening branch from 3e96c90 to 47edbbc Compare January 13, 2022 00:05
@stroxler stroxler changed the title Convert type comments pep 526 flattening Complete support for codemodding assignment type comments Jan 13, 2022
There were three different layers of target things, which
meant using target / targets as a naming convention was not
working well.

Now, I say:
- a target is one or more `target` values
- a target is a single lvalue, and maps to a `bindings` object
  of one or more bindings
- a binding is a specific, single value that can be annotated

Similarly, there are two layers in annotation handling:
- a type comment corresponds to an annotations, one or more types
- an annotations atom is an annotation

When zipping, we zip a single bindings against a single annotations.

When handling multi-assignment Assigns, we have to do this multiple
times and concatenate the results.
@stroxler stroxler changed the title Complete support for codemodding assignment type comments Full handling for applying type comments to Assign Jan 14, 2022
Copy link
Contributor

@zsol zsol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants