Skip to content

fix: catch KeyError when accessing __name__ in _idval_from_value (#14560)#14561

Open
NIK-TIGER-BILL wants to merge 2 commits into
pytest-dev:mainfrom
NIK-TIGER-BILL:fix-idval-from-value-keyerror
Open

fix: catch KeyError when accessing __name__ in _idval_from_value (#14560)#14561
NIK-TIGER-BILL wants to merge 2 commits into
pytest-dev:mainfrom
NIK-TIGER-BILL:fix-idval-from-value-keyerror

Conversation

@NIK-TIGER-BILL
Copy link
Copy Markdown

Description

Fixes #14560.

Custom classes with a __getattr__ that raises KeyError (instead of AttributeError) would crash pytest during parameterized test id generation. This PR catches both AttributeError and KeyError when trying to access __name__.

Checklist

  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Add text like closes #14560 to the PR description and/or commits.
  • Create a new changelog file in the changelog directory.
  • Add yourself to AUTHORS in alphabetical order.

…est-dev#14560)

Custom classes with a __getattr__ that raises KeyError instead of
AttributeError would crash pytest during parameterized test id
generation. We now catch both AttributeError and KeyError.

Fixes pytest-dev#14560

Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 5, 2026
@RonnyPfannschmidt
Copy link
Copy Markdown
Member

Nope it's valid to fail there

@NIK-TIGER-BILL
Copy link
Copy Markdown
Author

Thanks for the quick feedback! I see your point—raising KeyError inside __getattr__ violates the Python data-model protocol (__getattr__ should raise AttributeError). However, the issue reporter (#14560) hit this with a third-party wrapper class they don’t control. Would you prefer that pytest stay strict and leave the crash as-is, or should we catch the exception and treat it as ‘no __name__’ so that test-id generation never crashes on broken __getattr__ implementations? Happy to adjust or close if the consensus is ‘wont-fix’.

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.

Pytest can't handle custom classes in parameterized tests (major bug).

2 participants