-
Notifications
You must be signed in to change notification settings - Fork 192
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
Clear warnings for each file in codemod cli #1184
Conversation
When codemodding too fast
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1184 +/- ##
=======================================
Coverage 91.26% 91.26%
=======================================
Files 261 261
Lines 26883 26898 +15
=======================================
+ Hits 24535 24549 +14
- Misses 2348 2349 +1 ☔ View full report in Codecov by Sentry. |
libcst/codemod/_cli.py
Outdated
# We do this after the fork so that a context that was initialized with | ||
# some defaults before calling parallel_exec_transform_with_prettyprint | ||
# will be updated per-file. | ||
# Clean the warnings as well, otherwise they will be | ||
# passed from the previous file | ||
transformer.context = replace( | ||
transformer.context, | ||
filename=filename, | ||
scratch=deepcopy(scratch), | ||
warnings=[], |
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.
While this change will fix the issue, there's a deeper problem here. Apart from metadata_manager
, every other field of context
should be reset per file.
Concretely, I'm surprised we change filename
here but not full_module_name
or full_package_name
.
Maybe we should make this explicit and have a global context that persists between files, and a local one that gets reset. Then this assignment would look something like:
try:
mod_and_pkg = calculate_module_and_package(config.repo_root or ".", filename)
mod_name = mod_and_pkg.name
pkg_name = mod_and_pkg.package
except ValueError as exc:
print(f"Failed to determine module name for {filename}: {ex}", file=sys.stderr)
mod_name = None
pkg_name = None
transformer.context = CodemodContext(scratch=deepcopy(scratch), filename=filename, full_module_name=mod_name, full_package_name=pkg_name, metadata_manager=transformer.context.metadata_manager)
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.
Having said that, I'm happy to merge the PR as is because it's definitely an improvement over the current state. Let me know if you wanna roll this change into this PR or not
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.
full_module_name
or full_package_name
are changed below, so currently context replaced twice. And if it isn't possible to determine the module/package name, it will persist from the previous file. The wrapper
also persists. That would be changing in the new commit I'm submitting.
Before:
full_module_name
/full_package_name
persist if can't get them for the new filewrapper
persists across files
After:
full_module_name
/full_package_name
both set toNone
if can't figure them out.wrapper
is set toNone
on every file.
Is that okay to not set the wrapper
?
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.
Yep, it should be
Keep only context.metadata_manager Remove wrapper from context defaults on each file
Thanks! |
Summary
Original idea belongs to @jschavesr. I am just resubmitting his abandoned PR.
Fixes the issue: #1183
Context warning messages were not being cleaned between files, so warnings from one files could be passed to other files in the context. Because of that warnings were being shown wrongly in the summary and the total number of warnings was wrong as well.
I also encountered another error in Windows CI. Seems like codemodding small files happens faster than the timer counts more than zero (probably less precision on Windows). So fixing that, too.
Test Plan
Before:
Running
convert_format_to_fstring.ConvertFormatStringCommand
on three files with identical contents:All three are the same, so they should get one warning each.
But instead every following file gets more warnings copied from previous files:
The new test fails:
After:
Each file generates one warning correctly:
And the new test passes.