Skip to content

Use match/case for isinstance dispatch - #14799

Merged
Pierre-Sassoulas merged 3 commits into
pytest-dev:mainfrom
Pierre-Sassoulas:match-case-isinstance-dispatch
Aug 3, 2026
Merged

Use match/case for isinstance dispatch#14799
Pierre-Sassoulas merged 3 commits into
pytest-dev:mainfrom
Pierre-Sassoulas:match-case-isinstance-dispatch

Conversation

@Pierre-Sassoulas

Copy link
Copy Markdown
Member

Same spirit as #13762, but outside assert_repr. Focused on the isinstance chained check to convert them to match case.

@Pierre-Sassoulas Pierre-Sassoulas added the skip news used on prs to opt out of the changelog requirement label Jul 28, 2026
@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the match-case-isinstance-dispatch branch from 70bdbb1 to 55a83ae Compare July 28, 2026 15:46

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

Comment thread src/_pytest/python.py Outdated
return str(val)
case re.Pattern():
return ascii_escaped(val.pattern)
# NOTSET falls back to the default; it is an ``enum.Enum``, so it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the previous comment better 😀

Comment thread src/_pytest/python.py Outdated
Pierre-Sassoulas and others added 3 commits August 3, 2026 15:12
Replace the ``isinstance`` dispatch on ``obj`` with class patterns. The
falsy check stays an ``if`` with an early return, as truthiness has no
pattern equivalent.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Replace the ``isinstance``/identity dispatch on ``val`` with class
patterns. The ``NOTSET`` identity test becomes a ``NotSetType()`` class
pattern: ``NotSetType`` has a single member, so it is equivalent to the
``val is NOTSET`` test, while the ``NotSetType.token`` value pattern
would not be, as value patterns compare with ``==`` and parameter values
are allowed to have an ``__eq__`` that raises. It still has to be
matched before the ``enum.Enum()`` arm, as ``NOTSET`` is an
``enum.Enum``. The ``__name__`` sniffing arm has no pattern equivalent
and stays a guard.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Replace the ``isinstance`` dispatch on the expected exception with class
patterns; the unreachable ``else`` arm becomes ``case _``.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the match-case-isinstance-dispatch branch from 513e843 to dba164b Compare August 3, 2026 13:14
@Pierre-Sassoulas
Pierre-Sassoulas merged commit d81152a into pytest-dev:main Aug 3, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news used on prs to opt out of the changelog requirement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants