fix(step): support attribute access in step title placeholders#910
Open
golikovichev wants to merge 1 commit intoallure-framework:masterfrom
Open
fix(step): support attribute access in step title placeholders#910golikovichev wants to merge 1 commit intoallure-framework:masterfrom
golikovichev wants to merge 1 commit intoallure-framework:masterfrom
Conversation
Closes allure-framework#896. @allure.step("Process: {data.name}") used to raise AttributeError when data was an object such as a dataclass. The reason was that func_parameters() called represent() on every parameter value before str.format() ran, so by the time the title was being formatted all arguments were already strings and {data.name} tried attribute access on a string. Fix: introduce _StepTitleFormatter, a subclass of string.Formatter, that applies represent() in format_field instead of at parameter binding time. The formatter is given the raw arguments (with defaults filled in from the function signature), so attribute traversal works on the original objects. The leaf value is then wrapped via represent(), preserving the existing repr-style display for plain positional and keyword placeholders. Added regression tests covering dataclass attribute access, plain object attribute access, and nested attribute access (user.address.city).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #896.
Problem
@allure.step("Process: {data.name}")raisesAttributeErrorwhendatais an object with anameattribute (for example a dataclass), becausefunc_parameters()converts every parameter value to its string representation beforestr.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()appliesrepresent()to every value at the moment it builds the parameter mapping:In
_allure.py, the step decorator passed that already-stringified mapping toself.title.format(*args, **params), so{data.name}tried to access.nameon a string and raised.Fix
Introduce
_StepTitleFormatter, a subclass ofstring.Formatter, that defersrepresent()toformat_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 viarepresent(), which preserves the existing repr-style display for plain positional and keyword placeholders.Behaviour
{data.name}traverses to the attribute and then appliesrepresent()on the leaf value.{user.address.city}.selfandclsfirst arg) continue to work.func_parameters().Tests
Added a new regression test file
tests/allure_pytest/acceptance/step/step_attribute_placeholder_test.pycovering:user.address.city).All existing step tests continue to pass:
The 6 failures in
tests/allure_pytest/externals/reproduce on master without my changes (missing optional plugins such aspytest-checkin the dev environment) and are unrelated to this PR.