Skip to content

Use match/case for isinstance dispatch - #14799

Open
Pierre-Sassoulas wants to merge 3 commits into
pytest-dev:mainfrom
Pierre-Sassoulas:match-case-isinstance-dispatch
Open

Use match/case for isinstance dispatch#14799
Pierre-Sassoulas wants to merge 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.

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>
@Pierre-Sassoulas Pierre-Sassoulas added the skip news used on prs to opt out of the changelog requirement label Jul 28, 2026
Pierre-Sassoulas and others added 2 commits July 28, 2026 17:41
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 70bdbb1 to 55a83ae Compare July 28, 2026 15:46
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.

1 participant