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

[apply-annotations] Add argument for ignoring existing annotations. #291

Merged
merged 1 commit into from
May 12, 2020

Conversation

pradeep90
Copy link
Contributor

Summary

This will allow us to override existing types based on the stub.

Test Plan

Added test 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 Apr 28, 2020
@codecov-io
Copy link

codecov-io commented Apr 28, 2020

Codecov Report

Merging #291 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #291   +/-   ##
=======================================
  Coverage   93.93%   93.93%           
=======================================
  Files         220      220           
  Lines       21307    21318   +11     
=======================================
+ Hits        20014    20025   +11     
  Misses       1293     1293           
Impacted Files Coverage Δ
libcst/codemod/visitors/_apply_type_annotations.py 96.16% <100.00%> (+0.04%) ⬆️
...emod/visitors/tests/test_apply_type_annotations.py 100.00% <100.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 940647e...4bcef66. Read the comment docs.

@zsol
Copy link
Member

zsol commented Apr 28, 2020

Functionality looks good to me. I have one nit: ignore_existing_annotations is not expressive enough IMO. I'd be much happier with something like overwrite_existing_annotations. Wdyt?

@jimmylai
Copy link
Contributor

Functionality looks good to me. I have one nit: ignore_existing_annotations is not expressive enough IMO. I'd be much happier with something like overwrite_existing_annotations. Wdyt?

Agree. overwrite_existing_annotations is more direct.

self,
context: CodemodContext,
annotations: Optional[Annotations] = None,
ignore_existing_annotations: bool = False,
Copy link
Contributor

@jimmylai jimmylai Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add overwrite_existing_annotations to the static function add_stub_to_context (can add it to both)?
So overwrite_existing_annotations is stored in the context (probably use a different key).
That can make it easier for user since in some codemod application, user doesn't need to explicitly create the visitor instance and it's done by the high level Codemod helper.
E.g. In this codemod, user just calls the static method to add needed imports without needed to create AddImportsVisitor instance.

AddImportsVisitor.add_needed_import(self.context, "__future__", "annotations")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, added the argument to the static method as well.

Design choice: Setting the argument to True in any call will overwrite annotations for all stubs. The alternative was to store a flag per stub (and thus per annotation), which seemed like overkill.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found that API a bit confusing since it wasn't clear what would happen if you called add_stub_to_context(stub1, overwrite_existing_annotations=True) and add_stub_to_context(stub2, overwrite_existing_annotations=False).

Decided to go with a separate static method set_overwrite_existing_annotations_flag_in_context such that calling it at any point will set the flag to True. (It does not accept a bool flag to avoid the ambiguity I mentioned above with multiple calls.)

Copy link
Contributor

@jimmylai jimmylai May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect user calls ApplyTypeAnnotationsVisitor.add_stub_to_context multiple times?
The function takes a stub module and apply the stub on current module. When you run over a list of stub files, you'll need to call add_stub_to_context with different stub module and context. Separate set_overwrite_existing_annotations_flag_in_context as another staticmethod adds one more step to config the overwrite which doesn't seem make it easier to use due to the overwrite flag is stored in the context. That means each module needs to call set_overwrite_existing_annotations_flag_in_context separately if they want to enable overwrite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect user calls ApplyTypeAnnotationsVisitor.add_stub_to_context multiple times?

Yes, as it stands, add_stub_to_context can be called multiple times to add annotations for the same module, just like add_needed_import. That is what creates the ambiguity I pointed out above.

Decided to store only one stub in the context instead of maintaining a list, i.e., ApplyTypeAnnotationsVisitor.store_stub_in_context(context, stub, overwrite_existing_annotations). This is more limited than the option of adding multiple stubs for the same module, but is less ambiguous and matches the notion of one stub per Python source file. Let me know if that works.

@pradeep90 pradeep90 force-pushed the master branch 4 times, most recently from 3df2dd8 to f805003 Compare May 5, 2020 19:28
This will allow us to override existing types based on the stub.
@jimmylai jimmylai merged commit bfcc456 into Instagram:master May 12, 2020
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.

5 participants