Replace indentation of source before parsing with ast.

closes #5323
This commit is contained in:
Godson-Gnanaraj 2022-10-25 08:44:43 +05:30
parent b33244e2f0
commit b61b71c6f0
1 changed files with 8 additions and 1 deletions

View File

@ -226,7 +226,14 @@ def is_generator_with_return_value(callable):
return value is None or isinstance(value, ast.NameConstant) and value.value is None
if inspect.isgeneratorfunction(callable):
code = re.sub(r"^[\t ]+", "", inspect.getsource(callable))
pattern = r"(^[\t ]+)"
src = inspect.getsource(callable)
match = re.match(pattern, src) # Find indentation
code = re.sub(pattern, "", src)
if match:
# Remove indentation
code = re.sub(f"\n{match.group(0)}", "\n", code)
tree = ast.parse(code)
for node in walk_callable(tree):
if isinstance(node, ast.Return) and not returns_none(node):