From c5a1ff9ef010c9e886522d03afe7b4c3080cab68 Mon Sep 17 00:00:00 2001 From: Zach Nussbaum Date: Fri, 3 Mar 2023 08:43:59 -0500 Subject: [PATCH] feat: filter try/except when looking at custom code (#21914) * feat: filter try/except * Update src/transformers/dynamic_module_utils.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> --------- Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> --- src/transformers/dynamic_module_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/transformers/dynamic_module_utils.py b/src/transformers/dynamic_module_utils.py index 7e43b27d46..26d877d804 100644 --- a/src/transformers/dynamic_module_utils.py +++ b/src/transformers/dynamic_module_utils.py @@ -116,6 +116,9 @@ def check_imports(filename): with open(filename, "r", encoding="utf-8") as f: 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) + # Imports of the form `import xxx` imports = re.findall(r"^\s*import\s+(\S+)\s*$", content, flags=re.MULTILINE) # Imports of the form `from xxx import yyy`