allure-python icon indicating copy to clipboard operation
allure-python copied to clipboard

fix(step): support attribute access in step title placeholders

Open golikovichev opened this issue 7 months ago • 2 comments

Closes #896.

Problem

@allure.step("Process: {data.name}") raises AttributeError when data is an object with a name attribute (for example a dataclass), because func_parameters() converts every parameter value to its string representation before str.format() runs. By the time the title is being formatted, the values are already strings, so attribute access on {data.name} fails.

Root cause

In allure-python-commons/src/allure_commons/utils.py, func_parameters() applies represent() to every value at the moment it builds the parameter mapping:

sorted_items = sorted(
    map(
        lambda kv: (kv[0], represent(kv[1])),  # values become strings here
        items
    ),
    ...
)

In _allure.py, the step decorator passed that already-stringified mapping to self.title.format(*args, **params), so {data.name} tried to access .name on a string and raised.

Fix

Introduce _StepTitleFormatter, a subclass of string.Formatter, that defers represent() to format_field, applied after attribute traversal. The decorator now hands the formatter the raw arguments (with defaults filled in from the function signature), so attribute traversal operates on the original objects. Leaf values are still wrapped via represent(), which preserves the existing repr-style display for plain positional and keyword placeholders.

Behaviour

  • Existing positional and keyword placeholders unchanged.
  • Object attribute placeholders work: {data.name} traverses to the attribute and then applies represent() on the leaf value.
  • Nested attribute access works: {user.address.city}.
  • Method calls (self and cls first arg) continue to work.
  • Default values for arguments are honoured, matching func_parameters().

Tests

Added a new regression test file tests/allure_pytest/acceptance/step/step_attribute_placeholder_test.py covering:

  1. Dataclass attribute access in a step title.
  2. Plain object (non-dataclass) attribute access.
  3. Nested attribute access (user.address.city).

All existing step tests continue to pass:

tests/allure_pytest/acceptance/step/ ... 14 passed
tests/allure_pytest/ (excluding pre-existing externals failures) ... 265 passed

The 6 failures in tests/allure_pytest/externals/ reproduce on master without my changes (missing optional plugins such as pytest-check in the dev environment) and are unrelated to this PR.

golikovichev avatar May 03 '26 22:05 golikovichev

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar May 03 '26 22:05 CLAassistant

Hi, this PR has been open for a few weeks now. CI is green and the CLA is signed, so it should be ready whenever a maintainer has a moment. Is there anything I can adjust to make the review easier? Happy to rebase if it has drifted from main.

golikovichev avatar Jun 09 '26 22:06 golikovichev