-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Full handling for applying type comments to Assign #599
Full handling for applying type comments to Assign #599
Conversation
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 ```
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
Looks like I'll need to bump the required python version or find a 3.8 substitute for ast.unparse |
3e96c90
to
47edbbc
Compare
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
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:
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:
where we have to add type declarations