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