From 3e6f0f34ff97787897506e93cd5ddcdd17133887 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 25 Nov 2019 16:49:31 +0200 Subject: [PATCH] Cleanup unhelpful alias _AST_FLAG Also replace one direct call to `compile` with this flag with the equivalent wrapper `ast.parse`. This function can have a more precise type. --- src/_pytest/_code/source.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index d7cef683d..ee3f7cb14 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -5,7 +5,6 @@ import sys import textwrap import tokenize import warnings -from ast import PyCF_ONLY_AST as _AST_FLAG from bisect import bisect_right from types import FrameType from typing import Iterator @@ -196,7 +195,7 @@ class Source: newex.text = ex.text raise newex else: - if flag & _AST_FLAG: + if flag & ast.PyCF_ONLY_AST: return co lines = [(x + "\n") for x in self.lines] # Type ignored because linecache.cache is private. @@ -321,7 +320,7 @@ def getstatementrange_ast( # don't produce duplicate warnings when compiling source to find ast with warnings.catch_warnings(): warnings.simplefilter("ignore") - astnode = compile(content, "source", "exec", _AST_FLAG) + astnode = ast.parse(content, "source", "exec") start, end = get_statement_startend2(lineno, astnode) # we need to correct the end: