Skip to content

Commit

Permalink
fix(tests): python >=3.11 compatibility for identifiers
Browse files Browse the repository at this point in the history
Python 3.11 changed IntEnum.__str__ to return the number instead of the
enum value name. This breaks fixtures.json because pytest uses
str(value) to generate the test identifier names, and in a lot of places
our identifiers use the enum values.

This override of `_idval_from_value` explicitly generates a name from
the IntEnum instead of using the __str__ implementation.
  • Loading branch information
matejcik committed Sep 3, 2024
1 parent fa00575 commit 5009b34
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
from __future__ import annotations

import os
import typing as t
from enum import IntEnum
from pathlib import Path
import typing as t

import pytest
import xdist
from _pytest.python import IdMaker
from _pytest.reports import TestReport

from trezorlib import debuglink, log, models
Expand Down Expand Up @@ -464,6 +466,16 @@ def pytest_configure(config: "Config") -> None:
if config.getoption("verbose"):
log.enable_debug_output()

idval_orig = IdMaker._idval_from_value

def idval_from_value(self: IdMaker, val: object) -> str | None:
if isinstance(val, IntEnum):
return f"{type(val).__name__}.{val.name}"
return idval_orig(self, val)

IdMaker._idval_from_value = idval_from_value



def pytest_runtest_setup(item: pytest.Item) -> None:
"""Called for each test item (class, individual tests).
Expand Down

0 comments on commit 5009b34

Please sign in to comment.