user1443098
user1443098

Reputation: 7645

how to fix TypeError: called match pattern must be a type in Python 3.10

Trying to learn Python 3.10 pattern matching. Tried this example after reading 8.6.4.9. Mapping Patterns

>>> match 0.0:
...  case int(0|1):
...   print(1)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: called match pattern must be a type
>>>

Especially the note on built-in types, including int. How should I code to test for an integer value of 0 or 1 (the example in the doc) and not get this error?

Upvotes: 8

Views: 10805

Answers (3)

Raymond Hettinger
Raymond Hettinger

Reputation: 226316

For structural pattern matching, the class pattern requires parentheses after the class name.

For example:

x = 0.0
match x:
    case int():
        print('I')
    case float():
        print('F')
    case _:
        print('Other')

Upvotes: 6

Thingamabobs
Thingamabobs

Reputation: 8037

How should I code to test for an integer value of 0 or 1

The right answer is to use OR-Patterns, described in PEP 622:

Multiple alternative patterns can be combined into one using |. This means the whole pattern matches if at least one alternative matches. Alternatives are tried from left to right and have a short-circuit property, subsequent patterns are not tried if one matched.

x = 1
match x:
    case int(0 | 1):
        print('case of x = int(0) or x = int(1)')
    case _:
        print('x != 1 or 0')

Output: 'case of x = int(0) or x = int(1)'

Type insensitive would be like this:

x = 1.0
match x:
    case 0 | 1:
        print('case of x = 0 or x = 1')
    case _:
        print('x != 1 or 0')

Output: 'case of x = 0 or x = 1'


In case you want to check for each case separately, you would do:

x = 1.0
match x:
    case int(0):
        print('x = 0')
    case int(1):
        print('x = 1')
    case _:
        print('x != 1 or 0')

Output: x != 1 or 0

x = 1.0
match x:
    case 0:
        print('x = 0')
    case 1:
        print('x = 1')
    case _:
        print('x != 1 or 0')

Output: x = 1

Upvotes: 1

user1443098
user1443098

Reputation: 7645

I fell into a gotcha:

match 0.0
  case int:
    print(1)

effectively redefines int, so the next time I tried the match I posted, it failed since my int was shadowing the built in

Upvotes: 7

Related Questions