Simplify warning creation by using ast.parse

in py2 it's a ast.Name where in py3 it's a ast.NamedConstant

Fixes namespace by using import from
This commit is contained in:
Tomer Keren 2018-10-25 15:48:39 +03:00
parent aaf7f7fcca
commit 3e6f1fa2db
1 changed files with 17 additions and 23 deletions

View File

@ -891,30 +891,24 @@ class AssertionRewriter(ast.NodeVisitor):
)
"""
warning_msg = ast.Str(
'Asserting the value None directly, Please use "assert is None" to eliminate ambiguity'
)
AST_NONE = ast.NameConstant(None)
# using parse because it's different between py2 py3
AST_NONE = ast.parse("None").body[0].value
val_is_none = ast.Compare(node, [ast.Is()], [AST_NONE])
import_warnings = ast.ImportFrom(
module="warnings", names=[ast.alias("warn_explicit", None)], level=0
)
import_pytest_warning = ast.ImportFrom(
module="pytest", names=[ast.alias("PytestWarning", None)], level=0
)
pytest_warning = ast_Call_helper("PytestWarning", warning_msg)
# This won't work because this isn't the same "self" as an AssertionRewriter!
# ast_filename = improved_ast_Call('str',ast.Attribute('self','module_path',ast.Load).module_path)
warn = ast_Call_helper(
"warn_explicit",
pytest_warning,
category=AST_NONE,
filename=ast.Str(str(module_path)),
lineno=ast.Num(lineno),
)
return ast.If(
val_is_none, [import_warnings, import_pytest_warning, ast.Expr(warn)], []
)
send_warning = ast.parse(
"""
from _pytest.warning_types import PytestWarning
from warnings import warn_explicit
warn_explicit(
PytestWarning('assertion the value None, Please use "assert is None"'),
category=None,
filename='{filename}',
lineno={lineno},
)
""".format(
filename=str(module_path), lineno=lineno
)
).body
return ast.If(val_is_none, send_warning, [])
def visit_Name(self, name):
# Display the repr of the name if it's a local variable or