Skip to content
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

Add support for methods with func type comment excluding self/cls #622

Merged
merged 6 commits into from
Jan 25, 2022

Conversation

stroxler
Copy link
Contributor

Summary

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).

Test Plan

I added a new test verifying various method cases: static and non-static,
with and without annotations of self / cls in the func type comment,
with and without inline type comments.

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).
@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 22, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 22, 2022

Codecov Report

Merging #622 (8cf00a7) into main (d35b6a5) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #622      +/-   ##
==========================================
- Coverage   94.78%   94.78%   -0.01%     
==========================================
  Files         245      245              
  Lines       25256    25281      +25     
==========================================
+ Hits        23940    23963      +23     
- Misses       1316     1318       +2     
Impacted Files Coverage Δ
libcst/codemod/commands/convert_type_comments.py 96.20% <100.00%> (+0.18%) ⬆️
...demod/commands/tests/test_convert_type_comments.py 97.10% <100.00%> (+0.17%) ⬆️
libcst/_nodes/statement.py 95.05% <0.00%> (-0.10%) ⬇️
libcst/_nodes/tests/test_atom.py 97.56% <0.00%> (ø)
libcst/_nodes/tests/test_match.py 100.00% <0.00%> (ø)
libcst/_nodes/tests/test_tuple.py 94.73% <0.00%> (ø)

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 d35b6a5...8cf00a7. Read the comment docs.

libcst/codemod/commands/convert_type_comments.py Outdated Show resolved Hide resolved
libcst/codemod/commands/convert_type_comments.py Outdated Show resolved Hide resolved
stroxler and others added 4 commits January 25, 2022 08:26
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.

4 participants