-
Notifications
You must be signed in to change notification settings - Fork 196
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #291 +/- ##
=======================================
Coverage 93.93% 93.93%
=======================================
Files 220 220
Lines 21307 21318 +11
=======================================
+ Hits 20014 20025 +11
Misses 1293 1293
Continue to review full report at Codecov.
|
Functionality looks good to me. I have one nit: |
Agree. |
self, | ||
context: CodemodContext, | ||
annotations: Optional[Annotations] = None, | ||
ignore_existing_annotations: bool = False, |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
3df2dd8
to
f805003
Compare
This will allow us to override existing types based on the stub.
Summary
This will allow us to override existing types based on the stub.
Test Plan
Added test case.