Skip to content

An ergonomics suggestion - accept format strings as ids in mark.parametrize() #6335

@aldanor

Description

@aldanor

I couldn't find this in previously opened tickets, so here goes: why don't we allow ids passed to pytest.mark.parametrize to be treated as format strings? It doesn't make much sense for those ids to be strings anyway as they would typically be either lists/iterables or callables.

E.g.:

# current
@pytest.mark.parametrize('foo', [1, 2, 3], ids=lambda x: f'foo:{x}')  # either this
@pytest.mark.parametrize('bar', [0.1, 0.2, 0.3], ids='bar:{:.2%}'.format)  # or this
# proposed
@pytest.mark.parametrize('foo', [1, 2, 3], ids='foo:{}')
@pytest.mark.parametrize('bar', [0.1, 0.2, 0.3], ids='bar:{:.2%}')

In the end, if ids is a callable, it's function that takes a single argument and converts it to a string.

One of the best built-in ways to do it in Python is via format strings, so why not provide direct ergonomic support for it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions