LibCST
LibCST copied to clipboard
Merge class attribute annotations in ApplyTypeAnnotationsVisitor
Currently, it just ignores class attributes
class MyClass:
name: str
name: str
won't appear in the sources.
Nice find! Looks like ApplyTypeAnnotationsVisitor
only try to restore the type annotation when the source code is an Assign
(e.g. name = "a"
). In this case of a simple name, it's not handled properly.
https://github.com/Instagram/LibCST/blob/5992a7d83d234e7575ad81107d352846ff1abb37/libcst/codemod/visitors/_apply_type_annotations.py#L468-L470
That can be fixed by match the Name node with pattern like this and reuse the code from leave_Assign
to apply the type nnotation.
SimpleStatementLine(
body=[
Expr(
value=Name(
),
),
],
)
Feel free to submit a PR to propose a fix. Test case can be added in https://github.com/Instagram/LibCST/blob/5992a7d83d234e7575ad81107d352846ff1abb37/libcst/codemod/visitors/tests/test_apply_type_annotations.py
CC @pradeep90
I'm hoping to fix this in the next month or two, it is affecting our ability to add annotations that empower static analysis