LibCST icon indicating copy to clipboard operation
LibCST copied to clipboard

Merge class attribute annotations in ApplyTypeAnnotationsVisitor

Open mkurnikov opened this issue 4 years ago • 4 comments

Currently, it just ignores class attributes

class MyClass:
    name: str

name: str won't appear in the sources.

mkurnikov avatar Jun 26 '20 22:06 mkurnikov

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

jimmylai avatar Jun 26 '20 23:06 jimmylai

I'm hoping to fix this in the next month or two, it is affecting our ability to add annotations that empower static analysis

stroxler avatar Aug 26 '21 21:08 stroxler