-
Notifications
You must be signed in to change notification settings - Fork 191
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
[AddImportsVisitor] Docstring Check Only for the Top Element of the Body #841
Conversation
Hey @zsol can you please take a look? |
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.
This looks like a correct fix to me. Thanks for adding a test!
One test-naming nit, and it looks like CI is not fully happy.
@@ -891,3 +891,36 @@ def test_import_in_docstring_module(self) -> None: | |||
full_module_name="a.b.foobar", full_package_name="a.b" | |||
), | |||
) | |||
|
|||
def test_import_in_docstring_module_with_docstring_not_as_the_top_line( |
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.
nit: A "docstring" in the wrong location is not really a docstring at all, technically speaking. It's just a random string expression in a statement by itself, which is thrown away at runtime. So I would prefer to rename this test to test_import_in_module_with_standalone_string_not_a_docstring
.
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.
Agreed, thank you
Codecov ReportBase: 94.79% // Head: 94.79% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #841 +/- ##
=======================================
Coverage 94.79% 94.79%
=======================================
Files 249 249
Lines 25827 25831 +4
=======================================
+ Hits 24483 24487 +4
Misses 1344 1344
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Sorry for bothering again @carljm but any pointers on how can I fix the failing CI checks? |
It looks to me like an infra failure unrelated to this PR. |
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.
This looks good to me. I think the failing checks are an infra problem that isn't related to this diff.
I haven't been an active maintainer of LibCST for quite a while so I'd prefer to wait a bit and see if someone else can verify that and merge this. If nobody weighs in in the next few days I can probably merge, I think the fix is clearly correct.
Great, thank you very much! |
Thanks for this @sagarbadiyani! |
Summary
Refer #343 (comment)
Instead of checking for docstring for all the elements of the body, this PR only keeps the check for the 0th element since our objective is to only honor the module docstrings.
The reason I am asking for this, I found a case in one of the repositories where the docstrings were written outside the function like how we have in Java, obviously, this is unconventional and unpythonic, but my only concern is why are we checking for docstrings which are not module docstrings?
Consider a case
Let's say we are trying to add an import
from typing import List
, the final code after applying the add imports tranformation will look likeThis is because the docstring is a part of the body of the module and when the for loop reached there, it set the import_add_location to 1, which should not have been the case
And this will break the code because all the
from __future__ imports
must occur at the beginning of the fileTest Plan
Added a Testcase.