From 2e42d937dcbfc17398210e957e054e815c694ca2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 14 Oct 2018 16:11:47 -0700 Subject: [PATCH] Display the filename when encountering `SyntaxWarning`. ```console $ cd t && rm -rf __pycache__ && pytest t.py -q -c /dev/null; cd .. . [100%] =============================== warnings summary =============================== :2: DeprecationWarning: invalid escape sequence \. -- Docs: https://docs.pytest.org/en/latest/warnings.html 1 passed, 1 warnings in 0.01 seconds ``` ```console $ cd t && rm -rf __pycache__ && pytest t.py -q -c /dev/null; cd .. . [100%] =============================== warnings summary =============================== /tmp/pytest/t/t.py:2: DeprecationWarning: invalid escape sequence \. '\.wat' -- Docs: https://docs.pytest.org/en/latest/warnings.html 1 passed, 1 warnings in 0.01 seconds ``` --- changelog/4152.bugfix.rst | 1 + src/_pytest/assertion/rewrite.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 changelog/4152.bugfix.rst diff --git a/changelog/4152.bugfix.rst b/changelog/4152.bugfix.rst new file mode 100644 index 000000000..ce2547294 --- /dev/null +++ b/changelog/4152.bugfix.rst @@ -0,0 +1 @@ +Display the filename when encountering ``SyntaxWarning``. diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 7a11c4ec1..385ae4e77 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -398,7 +398,7 @@ def _rewrite_test(config, fn): finally: del state._indecode try: - tree = ast.parse(source) + tree = ast.parse(source, filename=fn.strpath) except SyntaxError: # Let this pop up again in the real import. state.trace("failed to parse: %r" % (fn,))