I'm trying to comment out specific occurrences of code using PyCharm's 'Replace in Files' functionality.
Specifically, I want the following to be commented out:
if TYPE_CHECKING:
from foo import bar
from x import y
So it can be replaced by:
#if TYPE_CHECKING:
# from foo import bar
# from x import y
I need this because I am checking for cyclical dependencies using pydeps, which at the time of writing doesn't seem to have an option to ignore imports under TYPE_CHECKING guards. Commenting these out manually is tedious for the project I'm working on.
Right now I'm using this regex, which matches as expected:
(^if TYPE_CHECKING:\n)(^\s+from.?)+
And I'm trying to replace it using:
#$1#$2
I didn't expect this to work, as I think $2 should only match the first occurrence of the second group.
Another way would be to simply replace every line starting with if TYPE_CHECKING: or \s+from.+\n individually using e.g.:
(^if TYPE_CHECKING:\n|^\s+from.+\n)+
And then simply replacing by $1. This works as long as no other 'from' imports are preceded by whitespace. However, this also replaces occurrences in comments or already commented out code (I know this may be considered bad practice, but I'm looking for a way to make this work robustly regardless).
Would anyone have suggestions for an approach?