[issue45791] __instancecheck__ being checked on type(cls) instead of cls

Raymond Hettinger report at bugs.python.org
Fri Nov 12 12:10:57 EST 2021


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

FWIW, I discovered the issue when experimenting with ways to use the class pattern in structural pattern matching.


--- Code that should work but doesn't -----------

class Warm:
    def __instancecheck__(cls, inst):
        return inst in {'red', 'orange', 'blue'}
  
match 'red':
    case Warm():              # This doesn't match but should
        print('Glowing')


--- What you have to do to get it to work -------

class MetaWarm(type):
    def __instancecheck__(cls, inst):
        return inst in {'red', 'orange', 'blue'}

class Warm(metaclass=MetaWarm):
    pass

match 'red':
    case Warm():          # This matches
        print('Hot')

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45791>
_______________________________________


More information about the Python-bugs-list mailing list