From 71c5da81699062e06f93dcfe71e0208c074c04e9 Mon Sep 17 00:00:00 2001 From: Shannon Zhu Date: Thu, 17 Feb 2022 19:51:41 -0800 Subject: [PATCH] Avoid removing existing starred arg annotations in infer 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 --- client/commands/tests/infer_test.py | 42 +++++++++++++++++++ .../_apply_type_annotations.py | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/client/commands/tests/infer_test.py b/client/commands/tests/infer_test.py index c77ad480ccc..46113240cb4 100644 --- a/client/commands/tests/infer_test.py +++ b/client/commands/tests/infer_test.py @@ -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( """ diff --git a/client/libcst_vendored_visitors/_apply_type_annotations.py b/client/libcst_vendored_visitors/_apply_type_annotations.py index fd63a13150b..79f0b5c45ea 100644 --- a/client/libcst_vendored_visitors/_apply_type_annotations.py +++ b/client/libcst_vendored_visitors/_apply_type_annotations.py @@ -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 ),