0

I would like to clean my code from excessive if/else and translate to match/case. However, I am unable to access the value by key. I get the error called match pattern must be a type.

My code:

match message.channel.id: #int type 123123
    case channels.get("EventID") | channels.get("AnnouncmentsID"): #list of values int types {"EventID" : 123213} | {"PetsID : 234234}
        #some code

    case channels.get("DeathID"): #value int type {"DeathID" :123123}
        if message.attachments:
           #some code
    ###Same cases
    case _:
        pass

I tried explicitly changing int/string types, use the dict[key] and dict.get('key') keys directly. Nothing comes out.

  • on what line the error occurs ? – azro Apr 22 '22 at 17:52
  • 2 line and 4 line on 'case' `called match pattern must be a type ` – Mr. Demontino Apr 22 '22 at 18:15
  • Does this answer your question? [how to fix TypeError: called match pattern must be a type in Python 3.10](https://stackoverflow.com/questions/69918623/how-to-fix-typeerror-called-match-pattern-must-be-a-type-in-python-3-10) – Jeremy Apr 22 '22 at 18:18
  • I saw this thread, but unfortunately it does not solve the problem ( For some reason case + dict doesn't work in conjunction – Mr. Demontino Apr 22 '22 at 18:24

1 Answers1

0

You're comparing int to list. Use | for matching multiple literals in match statement:

case channels.get("EventID") | channels.get("AnnouncmentsID") | channels.get("PetsID") | channels.get("Guild MomentsID"):

Described in documentation

Jeremy
  • 661
  • 7
  • 19