Assert statements in Enum subclass definitions case errors when assertion rewriting is enabled
See original GitHub issueWhile running pytest with the following code:
from enum import Enum
STEP = 100
class SomeEnum(Enum):
FIRST = 100
SECOND = FIRST + STEP
THIRD = SECOND + STEP
assert THIRD == 300
…assertion rewriting causes an error because of an interaction with Enum:
================================ test session starts ================================
platform darwin -- Python 3.8.12, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
rootdir: /private/tmp/pytest-enum-assert-bug
plugins: profiling-1.7.0, order-1.0.1, cov-3.0.0
collected 0 items / 1 error
====================================== ERRORS =======================================
______________________________ ERROR collecting bug.py ______________________________
bug.py:5: in <module>
class SomeEnum(Enum):
bug.py:11: in SomeEnum
assert THIRD == 300
/Users/dsuffling/.pyenv/versions/3.8.12/lib/python3.8/enum.py:112: in __setitem__
raise TypeError('Attempted to reuse key: %r' % key)
E TypeError: Attempted to reuse key: '@py_assert1'
============================== short test summary info ==============================
ERROR bug.py - TypeError: Attempted to reuse key: '@py_assert1'
!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!
================================= 1 error in 0.28s ==================================
This error does not occur with --assert=plain:
================================ test session starts ================================
platform darwin -- Python 3.8.12, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
rootdir: /private/tmp/pytest-enum-assert-bug
plugins: profiling-1.7.0, order-1.0.1, cov-3.0.0
collected 0 items
=============================== no tests ran in 0.02s ===============================
In fact, the issue can be reproduced by:
from enum import Enum
class SomeEnum(Enum):
assert True
Environment:
pytest version: 6.2.5 environment: macOS 10.14, homebrew 3.3.12, pyenv 2.2.3, CPython 3.8.12 pip list (new virtualenv; nothing but pytest installed):
Package Version
---------- -------
attrs 21.4.0
iniconfig 1.1.1
packaging 21.3
pip 21.1.1
pluggy 1.0.0
py 1.11.0
pyparsing 3.0.7
pytest 6.2.5
setuptools 56.0.0
toml 0.10.2
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Mocking Java enum to add a value to test fail case
To get the above example to work in my own tests, I had to add ALL values of the original Enum, AND the...
Read more >Advanced googletest Topics
This document will show you more assertions as well as how to construct complex failure messages, propagate fatal failures, reuse and speed up...
Read more >AssertJ - fluent assertions java library - GitHub Pages
AssertJ is a Java library that provides a rich set of assertions and truly helpful error messages, improves test code readability, and is...
Read more >Documentation - TypeScript 3.7
Note that if bar is null or undefined , our code will still hit an error ... Assertions in JavaScript are often used...
Read more >JUnit 5 User Guide
A first test case. import static org.junit.jupiter.api.Assertions.assertEquals; import example.util.Calculator; import org.junit.jupiter.api ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@finite-state-machine adding the magic value PYTEST_DONT_REWRITE to the docstring of the module can help as a temporary workaround
Just from quick analysis of this code, The issue come from rewrite [temporary variables clear].(https://github.com/yuvalshi0/pytest/blob/8b211983ff11be5238f90bfd0b3523f03fea09ae/_pytest/assertion/rewrite.py#L463)
From reading this part I assume what pytest actually runs is:
Causing a double
Enumkey statement inside theEnumclass body. I personally think there is no a straightforward fix for this issue, even if you detect the assert statement is inside a class body, you don’t have any way to know that it does not allow duplicate members, such as this case.