避免结构模式匹配中的意外捕获

问题描述

This example 在使用模式匹配时被讨论为可能的“陷阱”:

NOT_FOUND = 400

retcode = 200
match retcode:
    case NOT_FOUND:
        print('not found')  

print(f'Current value of {NOT_FOUND=}')

这是一个使用结构模式匹配的意外捕获示例。它给出了这个意想不到的输出

not found
Current value of NOT_FOUND=200

同样的问题以其他形式出现:

match x:
    case int():
        pass
    case float() | Decimal():
        x = round(x)
    case str:
        x = int(x)

在本例中,str 需要有括号 str()。如果没有它们,它会“捕获”并且 str 内置类型被替换为 x 的值。

是否有defensive programming做法可以帮助避免这些问题并提供早期检测?

解决方法

最佳做法

是否有防御性编程实践可以帮助避免这些问题并提供早期检测?

是的。通过始终包含 PEP 634 描述为 irrefutable case block 的内容,可以轻松检测到意外捕获。

用简单的语言来说,这意味着一个总是匹配的包罗万象的情况。

工作原理

意外捕获始终匹配。不允许超过一个无可辩驳的案例块。因此,当添加了有意的捕获时,会立即检测到意外捕获。

修正第一个例子

只需在末尾添加一个包罗万象的 wildcard pattern

match retcode:
    case NOT_FOUND:
        print('not found')
    case _:
        pass

立即检测到问题并给出以下错误:

SyntaxError: name capture 'NOT_FOUND' makes remaining patterns unreachable

修正第二个例子

在末尾添加一个包罗万象的 wildcard pattern

match x:
    case int():
        pass
    case float() | Decimal():
        x = round(x)
    case str:
        x = int(x)
    case _:
        pass

再次立即检测到问题:

SyntaxError: name capture 'str' makes remaining patterns unreachable