Fix the regex in get_imports to support multiline try blocks and excepts with specific exception types (#23725)

* fix and test get_imports for multiline try blocks, and excepts with specific errors

* fixup

* add some more tests

* add license
This commit is contained in:
Daniel King
2023-05-24 12:40:19 -07:00
committed by GitHub
parent d8222be57e
commit 89159651ba
2 changed files with 130 additions and 1 deletions

View File

@@ -123,7 +123,7 @@ def get_imports(filename):
content = f.read()
# filter out try/except block so in custom code we can have try/except imports
content = re.sub(r"\s*try\s*:\s*.*?\s*except\s*:", "", content, flags=re.MULTILINE)
content = re.sub(r"\s*try\s*:\s*.*?\s*except\s*.*?:", "", content, flags=re.MULTILINE | re.DOTALL)
# Imports of the form `import xxx`
imports = re.findall(r"^\s*import\s+(\S+)\s*$", content, flags=re.MULTILINE)