Use match/case for isinstance dispatch - #14799
Merged
Pierre-Sassoulas merged 3 commits intoAug 3, 2026
Merged
Conversation
Pierre-Sassoulas
force-pushed
the
match-case-isinstance-dispatch
branch
from
July 28, 2026 15:46
70bdbb1 to
55a83ae
Compare
bluetech
approved these changes
Aug 3, 2026
| return str(val) | ||
| case re.Pattern(): | ||
| return ascii_escaped(val.pattern) | ||
| # NOTSET falls back to the default; it is an ``enum.Enum``, so it |
Member
There was a problem hiding this comment.
I liked the previous comment better 😀
Pierre-Sassoulas
commented
Aug 3, 2026
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
force-pushed
the
match-case-isinstance-dispatch
branch
from
August 3, 2026 13:14
513e843 to
dba164b
Compare
Pierre-Sassoulas
enabled auto-merge
August 3, 2026 13:14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Same spirit as #13762, but outside assert_repr. Focused on the isinstance chained check to convert them to match case.