Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python 3.12] Assertion rewriting triggers DeprecationWarning #10977

Closed
seifertm opened this issue May 9, 2023 · 0 comments
Closed

[Python 3.12] Assertion rewriting triggers DeprecationWarning #10977

seifertm opened this issue May 9, 2023 · 0 comments
Labels
topic: rewrite related to the assertion rewrite mechanism type: enhancement new feature or API change, should be merged into features branch

Comments

@seifertm
Copy link

seifertm commented May 9, 2023

What's the problem this feature will solve?

As of pytest 7.3.1, the classes ast.Str and ast.NameConstant are used in assertion rewriting. Their use has been deprecated in Python 3.8. These classes will emit a DeprecationWarning starting with Python 3.12.

When a user runs pytest 7.3.1 on Python 3.12and pytest collects a test with an assert statement, the test will show deprecation warnings.

Describe the solution you'd like

Pytest does not trigger a DeprecationWarning when running tests on Python 3.12.

Tested with pytest 7.3.1 on Python 3.12 (commit 0aeda29793):

test_anything.py

def test_a():
    assert True
$ pytest
===== test session starts =====
platform linux -- Python 3.12.0a7+, pytest-7.3.1, pluggy-1.0.0
rootdir: /tmp/test-cpython-next
collected 1 item                                                                                                                                                                                                                                                             

test_anything.py .                                                                                                                                                                                                                                                     [100%]

===== warnings summary =====
../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:927
../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:927
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:927: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
    assertmsg = ast.Str("")

../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:929
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:929: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
    template = ast.BinOp(assertmsg, ast.Add(), ast.Str(explanation))

../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:817
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:817: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
    keys = [ast.Str(key) for key in current.keys()]

../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:941
../venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:941
  /tmp/venv-next/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:941: DeprecationWarning: ast.NameConstant is deprecated and will be removed in Python 3.14; use ast.Constant instead
    clear = ast.Assign(variables, ast.NameConstant(None))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
===== 1 passed, 6 warnings in 0.01s ====
bluetech added a commit that referenced this issue May 10, 2023
@Zac-HD Zac-HD added type: enhancement new feature or API change, should be merged into features branch topic: rewrite related to the assertion rewrite mechanism labels May 15, 2023
bluetech added a commit that referenced this issue May 23, 2023
bluetech added a commit that referenced this issue May 23, 2023
bluetech added a commit that referenced this issue May 26, 2023
bluetech added a commit that referenced this issue May 26, 2023
mjpieters added a commit to aio-libs/frozenlist that referenced this issue Jun 6, 2023
These warnings otherwise would abort the test suite. See
pytest-dev/pytest#10977.
mjpieters added a commit to aio-libs/frozenlist that referenced this issue Jun 6, 2023
These warnings otherwise would abort the test suite. See
pytest-dev/pytest#10977.
mjpieters added a commit to aio-libs/frozenlist that referenced this issue Jun 6, 2023
These warnings otherwise would abort the test suite. See
pytest-dev/pytest#10977.
mjpieters added a commit to aio-libs/yarl that referenced this issue Jun 6, 2023
These warnings otherwise would abort the test suite. See
pytest-dev/pytest#10977.
mjpieters added a commit to aio-libs/yarl that referenced this issue Jun 7, 2023
These warnings otherwise would abort the test suite. See
pytest-dev/pytest#10977.
The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Jun 9, 2023
graingert added a commit to graingert/urllib3 that referenced this issue Jul 12, 2023
zaro0508 pushed a commit to Sceptre/sceptre that referenced this issue Dec 3, 2023
Adds explicit support for Python 3.12. These are the minimal changes I needed for `pytest` to pass on 3.12 and macOS:

* Replaces deprecated [`datetime.datetime.utcnow`](https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow)
* Replaces `called_once_with` with `assert_called_once_with`, which now [raises](python/cpython#100690) `AttributeError: 'called_once_with' is not a valid assertion`
* Bumps `pytest` due to `DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead` pytest-dev/pytest#10977
* Refreshes the lockfile

depends on #1393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: rewrite related to the assertion rewrite mechanism type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants