Skip to content

Commit

Permalink
Avoid removing existing starred arg annotations in infer
Browse files Browse the repository at this point in the history
Summary:
Noticed during inference codemods that pre-existing annotations on starred args were being deleted for no apparent reason.

It turns out I think there is a bug in the annotation application transform, where rather than taking the existing node and making changes to the pieces where we want to set annotations, we're taking the inferred annotations as the base object and returning that.

cc stroxler are we tracking delta of this module vs. the open sourced version of LibCST? What's the best way to keep changes here maintainable (I think I've made one or two over the course of bug-correcting during the codemods)?

Reviewed By: stroxler

Differential Revision: D34277896

fbshipit-source-id: 3d7719711bc0edfd68bb0e9e98f5094f062bdb7a
  • Loading branch information
shannonzhu authored and facebook-github-bot committed Feb 18, 2022
1 parent d318147 commit 71c5da8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions client/commands/tests/infer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,48 @@ def _assert_in_place(
)
self.assertEqual(expected_code, annotated_code)

def test_apply_functions(self) -> None:
self._assert_in_place(
"""
def foo(x: int) -> None: ...
""",
"""
def foo(x):
pass
""",
"""
def foo(x: int) -> None:
pass
""",
)
self._assert_in_place(
"""
def incomplete_stubs(x: int, y) -> None: ...
""",
"""
def incomplete_stubs(x, y: int):
pass
""",
"""
def incomplete_stubs(x: int, y: int) -> None:
pass
""",
)
self._assert_in_place(
"""
def incomplete_stubs_with_stars(x: int, *args, **kwargs) -> None: ...
""",
"""
def incomplete_stubs_with_stars(x, *args: P.args, **kwargs: P.kwargs):
pass
""",
"""
def incomplete_stubs_with_stars(x: int, *args: P.args, **kwargs: P.kwargs) -> None:
pass
""",
)


def test_apply_globals(self) -> None:
self._assert_in_place(
"""
Expand Down
2 changes: 1 addition & 1 deletion client/libcst_vendored_visitors/_apply_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def update_annotation(
annotated_parameters.append(parameter)
return annotated_parameters

return annotations.parameters.with_changes(
return updated_node.params.with_changes(
params=update_annotation(
updated_node.params.params, annotations.parameters.params
),
Expand Down

0 comments on commit 71c5da8

Please sign in to comment.