Raymond Hettinger
Raymond Hettinger

Reputation: 226221

Capture makes remaining patterns unreachable

Why does this code fail:

OKAY = 200
NOT_FOUND = 404
INTERNAL_SERVER_ERROR = 500

match status:
    case OKAY:
        print('It worked')
    case NOT_FOUND:
        print('Unknown')
    case INTERNAL_SERVER_ERROR:
        print('Out of service')
    case _:
        print('Unknown code')

It generates this error message:

  File "/Users/development/Documents/handler.py", line 66
    case OKAY:
         ^^^^
SyntaxError: name capture 'OKAY' makes remaining patterns unreachable

What does that error message mean and how do I fix the code to make it work?

Upvotes: 47

Views: 21734

Answers (3)

lgekman
lgekman

Reputation: 6874

If you know the type (and you probably do), then cast the variable. I had a similar problem with a global constant:

C = "CONSTANT"
match n['type']:
    case str(C):
        pass

In your case case int(OKAY): will probably work

Upvotes: 0

TRistan
TRistan

Reputation: 1

In my advice, if you are using a question, you have to put the script like this.

match (your variable)
    case Hello:
        print('Hi back!')

to:

match (your variable)
    case 'Hello':
        print('Hi back!')

Upvotes: -1

Raymond Hettinger
Raymond Hettinger

Reputation: 226221

Cause of the problem

A variable name in a case clause is treated as a name capture pattern.

It always matches and tries to make an assignment to the variable name. This is almost certainly not what was intended.

Because the first matching case wins and because case OKAY always matches, the other case clauses will never be checked.

That explains the error message:

SyntaxError: name capture 'OKAY' makes remaining patterns unreachable

Key to solving the problem

We need to replace the name capture pattern with a non-capturing pattern such as a value pattern that uses the . operator for attribute lookup. The dot is the key to matching this a non-capturing pattern.

There are many ways to achieve this. One is to put the names in a class namespace:

class ResponseCode:
    OKAY = 200
    NOT_FOUND = 404
    INTERNAL_SERVER_ERROR = 500

Now, case ResponseCode.NOT_FOUND: ... is a value pattern (because of the dot) and won't capture.

Another way to achieve the same effect is to move the constants into their own module and refer to them using the dot:

import response_code

match status:
   case response_code.OKAY: ...
   case response_code.NOT_FOUND: ...
   case response_code.INTERNAL_SERVER_ERROR: ...

Besides creating a class or a module, it is also possible to create an integer enumeration for the same effect:

from enum import IntEnum

class ResponseCode(IntEnum):
    OKAY = 200
    NOT_FOUND = 404
    INTERNAL_SERVER_ERROR = 500

For HTTP response codes, an integer enumeration has already be created for you in the HTTPStatus class found in the standard library.

Example solution

Here is a worked out solution to the original problem. The presence of the . for the enum attribute lookup is the key to match and case recognizing this as a value pattern:

from http import HTTPStatus

status = 404

match status:
    case HTTPStatus.OK:
        print('It worked')
    case HTTPStatus.NOT_FOUND:
        print('Unknown')
    case HTTPStatus.INTERNAL_SERVER_ERROR:
        print('Out of service')
    case _:
        print('Unknown code')
        

Upvotes: 54

Related Questions