pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Mark.parametrize eccepting type-annotated arguments as argnames string argument

Open peroksid opened this issue 1 year ago • 5 comments

What's the problem this feature will solve?

The decorator pytest.mark.parameterize() could use arglist which contains type-annotated arguments. This way one can quickly copy function signature and clean it later.

Describe the solution you'd like

I'd like to see

@pytest.mark.parametrize("flag: bool, context: Tuple[BaseModel], error_message: Optional[str]")
def test_func(flag: bool, context: Tuple[BaseModel], error_message: Optional[str]):
    ...

For this I change the parsing of string argnames value in https://github.com/pytest-dev/pytest/blob/5645fa45fb063027aab6d49d4d64090446d82292/src/_pytest/mark/structures.py#L128

If I detect colons in the argnames after splitting with ',', I split by colons and take the correct half before stripping spaces.

The existing functionality is not affected, only extended.

Alternative Solutions

Additional context

peroksid avatar Jan 12 '24 18:01 peroksid

But why?

dolfinus avatar Jan 28 '24 09:01 dolfinus

But why? What mark.parametrize should do with these hints, perform type validation?

dolfinus avatar Jan 28 '24 09:01 dolfinus

This severely complicated parsing without adding value as of now, unless added value is shown this shouldn't be considered

RonnyPfannschmidt avatar Jan 28 '24 09:01 RonnyPfannschmidt

It could, but it's not essential. It'd be handy to support simple copy-pasted fragments with typw hints.

Currently you can copy-paste test function arguments to parameterise arguments list, it's quick. If you use type hints you start stumbling upon.

The implementation will not be super complicated.

On Sun 28 Jan 2024, 09:46 Maxim Martynov, @.***> wrote:

But why? What mark.parametrize should do with these hints, perform type validation?

— Reply to this email directly, view it on GitHub https://github.com/pytest-dev/pytest/issues/11806#issuecomment-1913535887, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAHALY435PDTFNE25DDGBDYQYM6PAVCNFSM6AAAAABBYRYYM2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJTGUZTKOBYG4 . You are receiving this because you authored the thread.Message ID: @.***>

peroksid avatar Jan 28 '24 17:01 peroksid

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] avatar Feb 12 '24 01:02 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Feb 19 '24 01:02 github-actions[bot]