Refactor check_banned_imports.py
to allow Python 3 files to import subprocess
#7706
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Once we drop Python 2, we will start directly importing the stdlib module for
subprocess
and will stop usingsubprocess32
andpants.util.process_handler.subprocess
. However, ourcheck_banned_imports.py
would not allow this to happen.At the same time, we would like to keep checking that our Python 2 files are valid, so we don't want to remove the check outright.
Solution
Extract out a
filter_files()
function that will remove any files without the given snippet. This allows us to identify and save in memory all of our Python 2 files, then to callcheck_banned_import
on only those files.We check for the presence of
from __future__
imports, which are only used in our codebase for Python 2 compatibility.Result
We can now import
subprocess
in Python 3-only files.There is a 1-second slowdown in the performance of that script. After we finish dropping Python 2, we could reclaim this second by removing both the filter to get Python 2 only files and removing the
subprocess
check entirely.