Skip to content

feat: warn about parametrization IDs that break -k selection - #14987

Open
royy92 wants to merge 1 commit into
pytest-dev:mainfrom
royy92:fix-6626
Open

feat: warn about parametrization IDs that break -k selection#14987
royy92 wants to merge 1 commit into
pytest-dev:mainfrom
royy92:fix-6626

Conversation

@royy92

@royy92 royy92 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

What

Add a warning for custom parametrization IDs that cannot be safely used as part of a -k identifier.

Also document guidance for choosing custom IDs that work with -k.

Why

Custom parametrization IDs can contain characters such as parentheses, commas, whitespace, or = that have special meaning in -k expressions.

This can produce valid collected test IDs that cannot be selected directly with -k, as reported in #6626.

Following the discussion in #6626, this starts with warnings for custom IDs only. Built-in generated IDs are left unchanged for separate consideration.

How

Add is_safe_identifier_part() using the existing -k expression scanner to check whether a custom ID can be safely embedded in an identifier.

Warn with PytestWarning when a custom ID that prevents direct -k selection comes from:

  • pytest.param(..., id=...).
  • ids=[...].
  • an ids callable.
  • pytest_make_parametrize_id.

Built-in generated IDs are not warned about.

Test

Add coverage for:

  • safe and unsafe identifier parts.
  • explicit pytest.param IDs.
  • IDs provided through ids=[...].
  • callable-generated IDs.
  • IDs returned by pytest_make_parametrize_id.
  • safe custom IDs such as and, or, not, and an empty ID.
  • unselectable built-in generated IDs remaining warning-free.

The relevant test suites pass with 304 tests.

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Add text like closes #XYZW to the PR description and/or commits (where XYZW is the issue number). See the github docs for more information.
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory, with a name like <ISSUE NUMBER>.<TYPE>.rst. See changelog/README.rst for details.
  • Add yourself to AUTHORS in alphabetical order.

Co-authored-by: ChatGPT <noreply@openai.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant