question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Assert statements in Enum subclass definitions case errors when assertion rewriting is enabled

See original GitHub issue

While 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:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RonnyPfannschmidtcommented, Jan 31, 2022

@finite-state-machine adding the magic value PYTEST_DONT_REWRITE to the docstring of the module can help as a temporary workaround

0reactions
yuvalshi0commented, Feb 24, 2022

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:

class SomeEnum(Enum):
    @py_assert0 = True
    ...  # rewrite magic
    @py_assert = None

Causing a double Enum key statement inside the Enum class 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found